| 735 | // return a selector with [name] suffix on each simple selector |
| 736 | // e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */ |
| 737 | private _applySelectorScope({ |
| 738 | selector, |
| 739 | scopeSelector, |
| 740 | hostSelector, |
| 741 | isParentSelector, |
| 742 | }: { |
| 743 | selector: string; |
| 744 | scopeSelector: string; |
| 745 | hostSelector: string; |
| 746 | isParentSelector?: boolean; |
| 747 | }): string { |
| 748 | const isRe = /\[is=([^\]]*)\]/g; |
| 749 | scopeSelector = scopeSelector.replace(isRe, (_: string, ...parts: string[]) => parts[0]); |
| 750 | |
| 751 | const attrName = `[${scopeSelector}]`; |
| 752 | |
| 753 | const _scopeSelectorPart = (p: string) => { |
| 754 | let scopedP = p.trim(); |
| 755 | |
| 756 | // Comments are replaced with placeholders before scoping. A part that only consists of |
| 757 | // comments is not a selector and must be left untouched. |
| 758 | if (!scopedP || scopedP === '&' || _onlyCommentPlaceholdersRe.test(scopedP)) { |
| 759 | return p; |
| 760 | } |
| 761 | |
| 762 | if (p.includes(_polyfillHostNoCombinator)) { |
| 763 | scopedP = this._applySimpleSelectorScope(p, scopeSelector, hostSelector); |
| 764 | if (!p.match(_polyfillHostNoCombinatorOutsidePseudoFunction)) { |
| 765 | const [_, before, colon, after] = scopedP.match(/([^:]*)(:*)([\s\S]*)/)!; |
| 766 | scopedP = before + attrName + colon + after; |
| 767 | } |
| 768 | } else { |
| 769 | // remove :host since it should be unnecessary |
| 770 | const t = p.replace(_polyfillHostRe, ''); |
| 771 | if (t.length > 0) { |
| 772 | const matches = t.match(/([^:]*)(:*)([\s\S]*)/); |
| 773 | if (matches) { |
| 774 | scopedP = matches[1] + attrName + matches[2] + matches[3]; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return scopedP; |
| 780 | }; |
| 781 | |
| 782 | // Wraps `_scopeSelectorPart()` to not use it directly on selectors with |
| 783 | // pseudo selector functions like `:where()`. Selectors within pseudo selector |
| 784 | // functions are recursively sent to `_scopeSelector()`. |
| 785 | const _pseudoFunctionAwareScopeSelectorPart = (selectorPart: string) => { |
| 786 | let scopedPart = ''; |
| 787 | |
| 788 | // Collect all outer `:where()` and `:is()` selectors, |
| 789 | // counting parenthesis to keep nested selectors intact. |
| 790 | const pseudoSelectorParts = []; |
| 791 | let pseudoSelectorMatch: RegExpExecArray | null; |
| 792 | while ( |
| 793 | (pseudoSelectorMatch = _cssPrefixWithPseudoSelectorFunction.exec(selectorPart)) !== null |
| 794 | ) { |