8000 refactor(compiler): Remove strictStyling option for ShadowCss (#48824) · angular/angular@c2bcf0b · GitHub
[go: up one dir, main page]

Skip to content

Commit c2bcf0b

Browse files
JeanMechedylhunn
authored andcommitted
refactor(compiler): Remove strictStyling option for ShadowCss (#48824)
`strictStyling` was an option provided by Polymer but it's not needed by Angular. This commit removes the dead code and updates related comments. PR Close #48824
1 parent 617a010 commit c2bcf0b

File tree

1 file changed

+12
-51
lines changed

1 file changed

+12
-51
lines changed

packages/compiler/src/shadow_css.ts

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ const animationKeywords = new Set([
2727
]);
2828

2929
/**
30-
* The following class is a port of shadowCSS from webcomponents.js to TypeScript.
31-
*
32-
* Please make sure to keep to edits in sync with the source file.
30+
* The following class has its origin from a port of shadowCSS from webcomponents.js to TypeScript.
31+
* It has since diverge in many ways to tailor Angular's needs.
3332
*
3433
* Source:
3534
* https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js
@@ -70,28 +69,8 @@ const animationKeywords = new Set([
7069
}
7170
7271
* encapsulation: Styles defined within ShadowDOM, apply only to
73-
dom inside the ShadowDOM. Polymer uses one of two techniques to implement
74-
this feature.
75-
76-
By default, rules are prefixed with the host element tag name
77-
as a descendant selector. This ensures styling does not leak out of the 'top'
78-
of the element's ShadowDOM. For example,
79-
80-
div {
81-
font-weight: bold;
82-
}
83-
84-
becomes:
85-
86-
x-foo div {
87-
font-weight: bold;
88-
}
89-
90-
becomes:
91-
92-
93-
Alternatively, if WebComponents.ShadowCSS.strictStyling is set to true then
94-
selectors are scoped by adding an attribute selector suffix to each
72+
dom inside the ShadowDOM.
73+
The selectors are scoped by adding an attribute selector suffix to each
9574
simple selector that contains the host element tag name. Each element
9675
in the element's ShadowDOM template is also given the scope attribute.
9776
Thus, these rules match only elements that have the scope attribute.
@@ -152,16 +131,11 @@ const animationKeywords = new Set([
152131
in comments in lieu of the next selector when running under polyfill.
153132
*/
154133
export class ShadowCss {
155-
// TODO: Is never re-assigned, could be removed.
156-
strictStyling: boolean = true;
157-
158134
/*
159-
* Shim some cssText with the given selector. Returns cssText that can
160-
* be included in the document via WebComponents.ShadowCSS.addCssToDocument(css).
135+
* Shim some cssText with the given selector. Returns cssText that can be included in the document
161136
*
162-
* When strictStyling is true:
163-
* - selector is the attribute added to all elements inside the host,
164-
* - hostSelector is the attribute added to the host itself.
137+
* The selector is the attribute added to all elements inside the host,
138+
* The hostSelector is the attribute added to the host itself.
165139
*/
166140
shimCssText(cssText: string, selector: string, hostSelector: string = ''): string {
167141
const commentsWithHash = extractCommentsWithHash(cssText);
@@ -373,7 +347,6 @@ export class ShadowCss {
373347
*
374348
**/
375349
private _insertPolyfillDirectivesInCssText(cssText: string): string {
376-
// Difference with webcomponents.js: does not handle comments
377350
return cssText.replace(_cssContentNextSelectorRe, function(...m: string[]) {
378351
return m[2] + '{';
379352
});
@@ -395,7 +368,6 @@ export class ShadowCss {
395368
*
396369
**/
397370
private _insertPolyfillRulesInCssText(cssText: string): string {
398-
// Difference with webcomponents.js: does not handle comments
399371
return cssText.replace(_cssContentRuleRe, (...m: string[]) => {
400372
const rule = m[0].replace(m[1], '').replace(m[2], '');
401373
return m[4] + rule;
@@ -441,7 +413,6 @@ export class ShadowCss {
441413
*
442414
**/
443415
private _extractUnscopedRulesFromCssText(cssText: string): string {
444-
// Difference with webcomponents.js: does not handle comments
445416
let r = '';
446417
let m: RegExpExecArray|null;
447418
_cssContentUnscopedRuleRe.lastIndex = 0;
@@ -568,8 +539,7 @@ export class ShadowCss {
568539
let selector = rule.selector;
569540
let content = rule.content;
570541
if (rule.selector[0] !== '@') {
571-
selector =
572-
this._scopeSelector(rule.selector, scopeSelector, hostSelector, this.strictStyling);
542+
selector = this._scopeSelector(rule.selector, scopeSelector, hostSelector);
573543
} else if (
574544
rule.selector.startsWith('@media') || rule.selector.startsWith('@supports') ||
575545
rule.selector.startsWith('@document') || rule.selector.startsWith('@layer') ||
@@ -611,17 +581,14 @@ export class ShadowCss {
611581
});
612582
}
613583

614-
private _scopeSelector(
615-
selector: string, scopeSelector: string, hostSelector: string, strict: boolean): string {
584+
private _scopeSelector(selector: string, scopeSelector: string, hostSelector: string): string {
616585
return selector.split(',')
617586
.map(part => part.trim().split(_shadowDeepSelectors))
618587
.map((deepParts) => {
619588
const [shallowPart, ...otherParts] = deepParts;
620589
const applyScope = (shallowPart: string) => {
621590
if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
622-
return strict ?
623-
this._applyStrictSelectorScope(shallowPart, scopeSelector, hostSelector) :
624-
this._applySelectorScope(shallowPart, scopeSelector, hostSelector);
591+
return this._applySelectorScope(shallowPart, scopeSelector, hostSelector);
625592
} else {
626593
return shallowPart;
627594
}
@@ -643,19 +610,13 @@ export class ShadowCss {
643610
return new RegExp('^(' + scopeSelector + ')' + _selectorReSuffix, 'm');
644611
}
645612

646-
private _applySelectorScope(selector: string, scopeSelector: string, hostSelector: string):
647-
string {
648-
// Difference from webcomponents.js: scopeSelector could not be an array
649-
return this._applySimpleSelectorScope(selector, scopeSelector, hostSelector);
650-
}
651-
652613
// scope via name and [is=name]
653614
private _applySimpleSelectorScope(selector: string, scopeSelector: string, hostSelector: string):
654615
string {
655616
// In Android browser, the lastIndex is not reset when the regex is used in String.replace()
656617
_polyfillHostRe.lastIndex = 0;
657618
if (_polyfillHostRe.test(selector)) {
658-
const replaceBy = this.strictStyling ? `[${hostSelector}]` : scopeSelector;
619+
const replaceBy = `[${hostSelector}]`;
659620
return selector
660621
.replace(
661622
_polyfillHostNoCombinatorRe,
@@ -674,7 +635,7 @@ export class ShadowCss {
674635

675636
// return a selector with [name] suffix on each simple selector
676637
// e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */
677-
private _applyStrictSelectorScope(selector: string, scopeSelector: string, hostSelector: string):
638+
private _applySelectorScope(selector: string, scopeSelector: string, hostSelector: string):
678639
string {
679640
const isRe = /\[is=([^\]]*)\]/g;
680641
scopeSelector = scopeSelector.replace(isRe, (_: string, ...parts: string[]) => parts[0]);

0 commit comments

Comments
 (0)
0