@@ -59,21 +59,19 @@ const visitors = {
5959
6060/**
6161 * @param {import('#compiler').Css.RelativeSelector[] } relative_selectors
62- * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement | null } node
62+ * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement | null } element
6363 * @param {import('#compiler').Css.StyleSheet } stylesheet
6464 * @returns {boolean }
6565 */
66- function apply_selector ( relative_selectors , node , stylesheet ) {
66+ function apply_selector ( relative_selectors , element , stylesheet ) {
67+ if ( ! element ) {
68+ return relative_selectors . every ( ( { metadata } ) => metadata . is_global || metadata . is_host ) ;
69+ }
70+
6771 const relative_selector = relative_selectors . pop ( ) ;
6872 if ( ! relative_selector ) return false ;
69- if ( ! node ) {
70- return (
71- ( relative_selector . metadata . is_global &&
72- relative_selectors . every ( ( relative_selector ) => relative_selector . metadata . is_global ) ) ||
73- ( relative_selector . metadata . is_host && relative_selectors . length === 0 )
74- ) ;
75- }
76- const applies = block_might_apply_to_node ( relative_selector , node ) ;
73+
74+ const applies = block_might_apply_to_node ( relative_selector , element ) ;
7775
7876 if ( applies === NO_MATCH ) {
7977 return false ;
@@ -86,13 +84,13 @@ function apply_selector(relative_selectors, node, stylesheet) {
8684 * @param {import('#compiler').RegularElement | import('#compiler').SvelteElement } element
8785 */
8886 function mark ( relative_selector , element ) {
89- relative_selector . metadata . should_encapsulate = true ;
87+ relative_selector . metadata . scoped = true ;
9088 element . metadata . scoped = true ;
9189 return true ;
9290 }
9391
9492 if ( applies === UNKNOWN_SELECTOR ) {
95- return mark ( relative_selector , node ) ;
93+ return mark ( relative_selector , element ) ;
9694 }
9795
9896 if ( relative_selector . combinator ) {
@@ -104,72 +102,84 @@ function apply_selector(relative_selectors, node, stylesheet) {
104102 if ( ancestor_block . metadata . is_global ) {
105103 continue ;
106104 }
105+
107106 if ( ancestor_block . metadata . is_host ) {
108- return mark ( relative_selector , node ) ;
107+ return mark ( relative_selector , element ) ;
109108 }
109+
110110 /** @type {import('#compiler').RegularElement | import('#compiler').SvelteElement | null } */
111- let parent = node ;
111+ let parent = element ;
112112 let matched = false ;
113113 while ( ( parent = get_element_parent ( parent ) ) ) {
114114 if ( block_might_apply_to_node ( ancestor_block , parent ) !== NO_MATCH ) {
115115 mark ( ancestor_block , parent ) ;
116116 matched = true ;
117117 }
118118 }
119+
119120 if ( matched ) {
120- return mark ( relative_selector , node ) ;
121+ return mark ( relative_selector , element ) ;
121122 }
122123 }
124+
123125 if ( relative_selectors . every ( ( relative_selector ) => relative_selector . metadata . is_global ) ) {
124- return mark ( relative_selector , node ) ;
126+ return mark ( relative_selector , element ) ;
125127 }
128+
126129 return false ;
127- } else if ( relative_selector . combinator . name === '>' ) {
130+ }
131+
132+ if ( relative_selector . combinator . name === '>' ) {
128133 const has_global_parent = relative_selectors . every (
129134 ( relative_selector ) => relative_selector . metadata . is_global
130135 ) ;
136+
131137 if (
132138 has_global_parent ||
133- apply_selector ( relative_selectors , get_element_parent ( node ) , stylesheet )
139+ apply_selector ( relative_selectors , get_element_parent ( element ) , stylesheet )
134140 ) {
135- return mark ( relative_selector , node ) ;
141+ return mark ( relative_selector , element ) ;
136142 }
143+
137144 return false ;
138- } else if (
139- relative_selector . combinator . name === '+' ||
140- relative_selector . combinator . name === '~'
141- ) {
145+ }
146+
147+ if ( relative_selector . combinator . name === '+' || relative_selector . combinator . name === '~' ) {
142148 const siblings = get_possible_element_siblings (
143- node ,
149+ element ,
144150 relative_selector . combinator . name === '+'
145151 ) ;
152+
146153 let has_match = false ;
147154 // NOTE: if we have :global(), we couldn't figure out what is selected within `:global` due to the
148155 // css-tree limitation that does not parse the inner selector of :global
149156 // so unless we are sure there will be no sibling to match, we will consider it as matched
150157 const has_global = relative_selectors . some (
151158 ( relative_selector ) => relative_selector . metadata . is_global
152159 ) ;
160+
153161 if ( has_global ) {
154- if ( siblings . size === 0 && get_element_parent ( node ) !== null ) {
162+ if ( siblings . size === 0 && get_element_parent ( element ) !== null ) {
155163 return false ;
156164 }
157- return mark ( relative_selector , node ) ;
165+ return mark ( relative_selector , element ) ;
158166 }
167+
159168 for ( const possible_sibling of siblings . keys ( ) ) {
160169 if ( apply_selector ( relative_selectors . slice ( ) , possible_sibling , stylesheet ) ) {
161- mark ( relative_selector , node ) ;
170+ mark ( relative_selector , element ) ;
162171 has_match = true ;
163172 }
164173 }
174+
165175 return has_match ;
166176 }
167177
168178 // TODO other combinators
169- return mark ( relative_selector , node ) ;
179+ return mark ( relative_selector , element ) ;
170180 }
171181
172- return mark ( relative_selector , node ) ;
182+ return mark ( relative_selector , element ) ;
173183}
174184
175185const regex_backslash_and_following_character = / \\ ( .) / g;
0 commit comments