@ -1,6 +1,7 @@
import { walk } from 'zimmerframe' ;
import { walk } from 'zimmerframe' ;
import { get _possible _values } from './utils.js' ;
import { get _possible _values } from './utils.js' ;
import { regex _ends _with _whitespace , regex _starts _with _whitespace } from '../../patterns.js' ;
import { regex _ends _with _whitespace , regex _starts _with _whitespace } from '../../patterns.js' ;
import { error } from '../../../errors.js' ;
/ * *
/ * *
* @ typedef { {
* @ typedef { {
@ -8,254 +9,379 @@ import { regex_ends_with_whitespace, regex_starts_with_whitespace } from '../../
* element : import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement ;
* element : import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement ;
* } } State
* } } State
* /
* /
/** @typedef { typeof NodeExist[keyof typeof NodeExist] } NodeExistsValue */
/** @typedef { NODE_PROBABLY_EXISTS | NODE_DEFINITELY_EXISTS } NodeExistsValue */
const NO _MATCH = 'NO_MATCH' ;
const NODE _PROBABLY _EXISTS = 0 ;
const POSSIBLE _MATCH = 'POSSIBLE_MATCH' ;
const NODE _DEFINITELY _EXISTS = 1 ;
const UNKNOWN _SELECTOR = 'UNKNOWN_SELECTOR' ;
const NodeExist = /** @type {const} */ ( {
Probably : 0 ,
Definitely : 1
} ) ;
const whitelist _attribute _selector = new Map ( [
const whitelist _attribute _selector = new Map ( [
[ 'details' , [ 'open' ] ] ,
[ 'details' , [ 'open' ] ] ,
[ 'dialog' , [ 'open' ] ]
[ 'dialog' , [ 'open' ] ]
] ) ;
] ) ;
/** @type {import('#compiler').Css.Combinator} */
const descendant _combinator = {
type : 'Combinator' ,
name : ' ' ,
start : - 1 ,
end : - 1
} ;
/** @type {import('#compiler').Css.RelativeSelector} */
const nesting _selector = {
type : 'RelativeSelector' ,
start : - 1 ,
end : - 1 ,
combinator : null ,
selectors : [
{
type : 'NestingSelector' ,
name : '&' ,
start : - 1 ,
end : - 1
}
] ,
metadata : {
is _global : false ,
is _host : false ,
is _root : false ,
scoped : false
}
} ;
/ * *
/ * *
*
*
* @ param { import ( '#compiler' ) . Css . StyleSheet } stylesheet
* @ param { import ( '#compiler' ) . Css . StyleSheet } stylesheet
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
* /
* /
export function prune ( stylesheet , element ) {
export function prune ( stylesheet , element ) {
/** @type {State} */
walk ( stylesheet , { stylesheet , element } , visitors ) ;
const state = { stylesheet , element } ;
walk ( stylesheet , state , visitors ) ;
}
}
/** @type {import('zimmerframe').Visitors<import('#compiler').Css.Node, State>} */
/** @type {import('zimmerframe').Visitors<import('#compiler').Css.Node, State>} */
const visitors = {
const visitors = {
ComplexSelector ( node , context ) {
ComplexSelector ( node , context ) {
context . next ( ) ;
const selectors = truncate ( node ) ;
const inner = selectors [ selectors . length - 1 ] ;
const i = node . children . findLastIndex ( ( child ) => {
if ( node . metadata . rule ? . metadata . parent _rule ) {
return ! child . metadata . is _global && ! child . metadata . is _host && ! child . metadata . is _root ;
const has _explicit _nesting _selector = selectors . some ( ( selector ) =>
} ) ;
selector . selectors . some ( ( s ) => s . type === 'NestingSelector' )
) ;
const relative _selectors = node . children . slice ( 0 , i + 1 ) ;
if ( ! has _explicit _nesting _selector ) {
selectors [ 0 ] = {
... selectors [ 0 ] ,
combinator : descendant _combinator
} ;
if ( apply _selector ( relative _selectors , context . state . element , context . state . stylesheet ) ) {
selectors . unshift ( nesting _selector ) ;
}
}
if (
apply _selector (
selectors ,
/** @type {import('#compiler').Css.Rule} */ ( node . metadata . rule ) ,
context . state . element ,
context . state . stylesheet
)
) {
mark ( inner , context . state . element ) ;
node . metadata . used = true ;
node . metadata . used = true ;
}
}
} ,
RelativeSelector ( node , context ) {
// note: we don't call context.next() here, we only recurse into
// for now, don't visit children (i.e. inside `:foo(...)`)
// selectors that don't belong to rules (i.e. inside `:is(...)` etc )
// this will likely change when we implement `:is(...)` etc
// when we encounter them below
}
}
} ;
} ;
/ * *
* Discard trailing ` :global(...) ` selectors , these are unused for scoping purposes
* @ param { import ( '#compiler' ) . Css . ComplexSelector } node
* /
function truncate ( node ) {
const i = node . children . findLastIndex ( ( { metadata } ) => {
return ! metadata . is _global && ! metadata . is _host && ! metadata . is _root ;
} ) ;
return node . children . slice ( 0 , i + 1 ) ;
}
/ * *
/ * *
* @ param { import ( '#compiler' ) . Css . RelativeSelector [ ] } relative _selectors
* @ param { import ( '#compiler' ) . Css . RelativeSelector [ ] } relative _selectors
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement | null } element
* @ param { import ( '#compiler' ) . Css . Rule } rule
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
* @ param { import ( '#compiler' ) . Css . StyleSheet } stylesheet
* @ param { import ( '#compiler' ) . Css . StyleSheet } stylesheet
* @ returns { boolean }
* @ returns { boolean }
* /
* /
function apply _selector ( relative _selectors , element , stylesheet ) {
function apply _selector ( relative _selectors , rule , element , stylesheet ) {
if ( ! element ) {
const parent _selectors = relative _selectors . slice ( ) ;
return relative _selectors . every ( ( { metadata } ) => metadata . is _global || metadata . is _host ) ;
const relative _selector = parent _selectors . pop ( ) ;
}
const relative _selector = relative _selectors . pop ( ) ;
if ( ! relative _selector ) return false ;
if ( ! relative _selector ) return false ;
const applies = relative _selector _might _apply _to _node ( relative _selector , element ) ;
const possible _match = relative _selector _might _apply _to _node (
relative _selector ,
rule ,
element ,
stylesheet
) ;
if ( applies === NO _MATCH ) {
if ( ! possible _match ) {
return false ;
return false ;
}
}
/ * *
if ( relative _selector . combinator ) {
* Mark both the compound selector and the node it selects as encapsulated ,
const name = relative _selector . combinator . name ;
* for transformation in a later step
* @ param { import ( '#compiler' ) . Css . RelativeSelector } relative _selector
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
* /
function mark ( relative _selector , element ) {
relative _selector . metadata . scoped = true ;
element . metadata . scoped = true ;
return true ;
}
if ( applies === UNKNOWN _SELECTOR ) {
switch ( name ) {
return mark ( relative _selector , element ) ;
case ' ' :
}
case '>' : {
let parent = /** @type {import('#compiler').TemplateNode | null} */ ( element . parent ) ;
if ( relative _selector . combinator ) {
let parent _matched = false ;
if (
let crossed _component _boundary = false ;
relative _selector . combinator . type === 'Combinator' &&
relative _selector . combinator . name === ' '
) {
for ( const ancestor _selector of relative _selectors ) {
if ( ancestor _selector . metadata . is _global ) {
continue ;
}
if ( ancestor _selector . metadata . is _host ) {
while ( parent ) {
return mark ( relative _selector , element ) ;
if ( parent . type === 'Component' || parent . type === 'SvelteComponent' ) {
}
crossed _component _boundary = true ;
}
/** @type {import('#compiler').RegularElement | import('#compiler').SvelteElement | null} */
if ( parent . type === 'RegularElement' || parent . type === 'SvelteElement' ) {
let parent = element ;
if ( apply _selector ( parent _selectors , rule , parent , stylesheet ) ) {
let matched = false ;
// TODO the `name === ' '` causes false positives, but removing it causes false negatives...
while ( ( parent = get _element _parent ( parent ) ) ) {
if ( name === ' ' || crossed _component _boundary ) {
if ( relative _selector _might _apply _to _node ( ancestor _selector , parent ) !== NO _MATCH ) {
mark ( parent _selectors [ parent _selectors . length - 1 ] , parent ) ;
mark ( ancestor _selector , parent ) ;
}
matched = true ;
parent _matched = true ;
}
if ( name === '>' ) return parent _matched ;
}
}
}
if ( matched ) {
parent = /** @type {import('#compiler').TemplateNode | null} */ ( parent . parent ) ;
return mark ( relative _selector , element ) ;
}
}
}
if ( relative _selectors . every ( ( relative _selector ) => relative _selector . metadata . is _global ) ) {
return parent _matched || parent _selectors . every ( ( selector ) => is _global ( selector , rule ) ) ;
return mark ( relative _selector , element ) ;
}
}
return false ;
case '+' :
}
case '~' : {
const siblings = get _possible _element _siblings ( element , name === '+' ) ;
if ( relative _selector . combinator . name === '>' ) {
let sibling _matched = false ;
const has _global _parent = relative _selectors . every (
( relative _selector ) => relative _selector . metadata . is _global
) ;
if (
for ( const possible _sibling of siblings . keys ( ) ) {
has _global _parent ||
if ( apply _selector ( parent _selectors , rule , possible _sibling , stylesheet ) ) {
apply _selector ( relative _selectors , get _element _parent ( element ) , stylesheet )
mark ( relative _selector , element ) ;
) {
sibling _matched = true ;
return mark ( relative _selector , element ) ;
}
}
return (
sibling _matched ||
( get _element _parent ( element ) === null &&
parent _selectors . every ( ( selector ) => is _global ( selector , rule ) ) )
) ;
}
}
return false ;
default :
// TODO other combinators
return true ;
}
}
}
if ( relative _selector . combinator . name === '+' || relative _selector . combinator . name === '~' ) {
// if this is the left-most non-global selector, mark it — we want
const siblings = get _possible _element _siblings (
// `x y z {...}` to become `x.blah y z.blah {...}`
element ,
const parent = parent _selectors [ parent _selectors . length - 1 ] ;
relative _selector . combinator . name === '+'
if ( ! parent || is _global ( parent , rule ) ) {
) ;
mark ( relative _selector , element ) ;
}
let has _match = false ;
return true ;
// NOTE: if we have :global(), we couldn't figure out what is selected within `:global` due to the
}
// css-tree limitation that does not parse the inner selector of :global
// so unless we are sure there will be no sibling to match, we will consider it as matched
const has _global = relative _selectors . some (
( relative _selector ) => relative _selector . metadata . is _global
) ;
if ( has _global ) {
/ * *
if ( siblings . size === 0 && get _element _parent ( element ) !== null ) {
* Mark both the compound selector and the node it selects as encapsulated ,
return false ;
* for transformation in a later step
}
* @ param { import ( '#compiler' ) . Css . RelativeSelector } relative _selector
return mark ( relative _selector , element ) ;
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
}
* /
function mark ( relative _selector , element ) {
relative _selector . metadata . scoped = true ;
element . metadata . scoped = true ;
}
for ( const possible _sibling of siblings . keys ( ) ) {
/ * *
if ( apply _selector ( relative _selectors . slice ( ) , possible _sibling , stylesheet ) ) {
* Returns ` true ` if the relative selector is global , meaning
mark ( relative _selector , element ) ;
* it ' s a ` :global(...) ` or ` :host ` or ` :root ` selector , or
has _match = true ;
* is an ` :is(...) ` or ` :where(...) ` selector that contains
}
* a global selector
* @ param { import ( '#compiler' ) . Css . RelativeSelector } selector
* @ param { import ( '#compiler' ) . Css . Rule } rule
* /
function is _global ( selector , rule ) {
if ( selector . metadata . is _global || selector . metadata . is _host || selector . metadata . is _root ) {
return true ;
}
for ( const s of selector . selectors ) {
/** @type {import('#compiler').Css.SelectorList | null} */
let selector _list = null ;
let owner = rule ;
if ( s . type === 'PseudoClassSelector' ) {
if ( ( s . name === 'is' || s . name === 'where' ) && s . args ) {
selector _list = s . args ;
}
}
}
return has _match ;
if ( s . type === 'NestingSelector' ) {
owner = /** @type {import('#compiler').Css.Rule} */ ( rule . metadata . parent _rule ) ;
selector _list = owner . prelude ;
}
}
// TODO other combinators
const has _global _selectors = selector _list ? . children . some ( ( complex _selector ) => {
return mark ( relative _selector , element ) ;
return complex _selector . children . every ( ( relative _selector ) =>
is _global ( relative _selector , owner )
) ;
} ) ;
if ( ! has _global _selectors ) {
return false ;
}
}
}
return mark ( relative _selector , element ) ;
return true ;
}
}
const regex _backslash _and _following _character = /\\(.)/g ;
const regex _backslash _and _following _character = /\\(.)/g ;
/ * *
/ * *
* Ensure that ` element ` satisfies each simple selector in ` relative_selector `
*
* @ param { import ( '#compiler' ) . Css . RelativeSelector } relative _selector
* @ param { import ( '#compiler' ) . Css . RelativeSelector } relative _selector
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } node
* @ param { import ( '#compiler' ) . Css . Rule } rule
* @ returns { NO _MATCH | POSSIBLE _MATCH | UNKNOWN _SELECTOR }
* @ param { import ( '#compiler' ) . RegularElement | import ( '#compiler' ) . SvelteElement } element
* @ param { import ( '#compiler' ) . Css . StyleSheet } stylesheet
* @ returns { boolean }
* /
* /
function relative _selector _might _apply _to _node ( relative _selector , node ) {
function relative _selector _might _apply _to _node ( relative _selector , rule , element , stylesheet ) {
if ( relative _selector . metadata . is _host || relative _selector . metadata . is _root ) return NO _MATCH ;
for ( const selector of relative _selector . selectors ) {
let i = relative _selector . selectors . length ;
while ( i -- ) {
const selector = relative _selector . selectors [ i ] ;
if ( selector . type === 'Percentage' || selector . type === 'Nth' ) continue ;
if ( selector . type === 'Percentage' || selector . type === 'Nth' ) continue ;
const name = selector . name . replace ( regex _backslash _and _following _character , '$1' ) ;
const name = selector . name . replace ( regex _backslash _and _following _character , '$1' ) ;
if ( selector . type === 'PseudoClassSelector' && ( name === 'host' || name === 'root' ) ) {
switch ( selector . type ) {
return NO _MATCH ;
case 'PseudoClassSelector' : {
}
if ( name === 'host' || name === 'root' ) {
if (
return false ;
relative _selector . selectors . length === 1 &&
}
selector . type === 'PseudoClassSelector' &&
name === 'global'
) {
return NO _MATCH ;
}
if ( selector . type === 'PseudoClassSelector' || selector . type === 'PseudoElementSelector' ) {
if ( name === 'global' && relative _selector . selectors . length === 1 ) {
continue ;
const args = /** @type {import('#compiler').Css.SelectorList} */ ( selector . args ) ;
}
const complex _selector = args . children [ 0 ] ;
return apply _selector ( complex _selector . children , rule , element , stylesheet ) ;
}
if ( selector . type === 'AttributeSelector' ) {
if ( ( name === 'is' || name === 'where' ) && selector . args ) {
const whitelisted = whitelist _attribute _selector . get ( node . name . toLowerCase ( ) ) ;
let matched = false ;
if (
! whitelisted ? . includes ( selector . name . toLowerCase ( ) ) &&
for ( const complex _selector of selector . args . children ) {
! attribute _matches (
if ( apply _selector ( truncate ( complex _selector ) , rule , element , stylesheet ) ) {
node ,
complex _selector . metadata . used = true ;
selector . name ,
matched = true ;
selector . value && unquote ( selector . value ) ,
}
selector . matcher ,
}
selector . flags ? . includes ( 'i' ) ? ? false
)
if ( ! matched ) {
) {
return false ;
return NO _MATCH ;
}
}
break ;
}
case 'PseudoElementSelector' : {
break ;
}
}
} else {
if ( selector . type === 'ClassSelector' ) {
case 'AttributeSelector' : {
const whitelisted = whitelist _attribute _selector . get ( element . name . toLowerCase ( ) ) ;
if (
! whitelisted ? . includes ( selector . name . toLowerCase ( ) ) &&
! attribute _matches (
element ,
selector . name ,
selector . value && unquote ( selector . value ) ,
selector . matcher ,
selector . flags ? . includes ( 'i' ) ? ? false
)
) {
return false ;
}
break ;
}
case 'ClassSelector' : {
if (
if (
! attribute _matches ( node , 'class' , name , '~=' , false ) &&
! attribute _matches ( element , 'class' , name , '~=' , false ) &&
! node . attributes . some (
! element . attributes . some (
( attribute ) => attribute . type === 'ClassDirective' && attribute . name === name
( attribute ) => attribute . type === 'ClassDirective' && attribute . name === name
)
)
) {
) {
return NO _MATCH ;
return false ;
}
}
} else if ( selector . type === 'IdSelector' ) {
if ( ! attribute _matches ( node , 'id' , name , '=' , false ) ) return NO _MATCH ;
break ;
} else if ( selector . type === 'TypeSelector' ) {
}
case 'IdSelector' : {
if ( ! attribute _matches ( element , 'id' , name , '=' , false ) ) {
return false ;
}
break ;
}
case 'TypeSelector' : {
if (
if (
node . name . toLowerCase ( ) !== name . toLowerCase ( ) &&
element . name . toLowerCase ( ) !== name . toLowerCase ( ) &&
name !== '*' &&
name !== '*' &&
node . type !== 'SvelteElement'
element . type !== 'SvelteElement'
) {
) {
return NO _MATCH ;
return false ;
}
break ;
}
case 'NestingSelector' : {
let matched = false ;
const parent = /** @type {import('#compiler').Css.Rule} */ ( rule . metadata . parent _rule ) ;
for ( const complex _selector of parent . prelude . children ) {
if ( apply _selector ( truncate ( complex _selector ) , parent , element , stylesheet ) ) {
complex _selector . metadata . used = true ;
matched = true ;
}
}
}
} else {
return UNKNOWN _SELECTOR ;
if ( ! matched ) {
return false ;
}
break ;
}
}
}
}
}
}
return POSSIBLE _MATCH ;
// possible match
return true ;
}
}
/ * *
/ * *
@ -481,7 +607,7 @@ function get_possible_element_siblings(node, adjacent_only) {
( attr ) => attr . type === 'Attribute' && attr . name . toLowerCase ( ) === 'slot'
( attr ) => attr . type === 'Attribute' && attr . name . toLowerCase ( ) === 'slot'
)
)
) {
) {
result . set ( prev , N odeExist. Definitely ) ;
result . set ( prev , N ODE_DEFINITELY _EXISTS ) ;
}
}
if ( adjacent _only ) {
if ( adjacent _only ) {
break ;
break ;
@ -600,7 +726,7 @@ function get_possible_last_child(relative_selector, adjacent_only) {
function has _definite _elements ( result ) {
function has _definite _elements ( result ) {
if ( result . size === 0 ) return false ;
if ( result . size === 0 ) return false ;
for ( const exist of result . values ( ) ) {
for ( const exist of result . values ( ) ) {
if ( exist === N odeExist. Definitely ) {
if ( exist === N ODE_DEFINITELY _EXISTS ) {
return true ;
return true ;
}
}
}
}
@ -632,7 +758,7 @@ function higher_existence(exist1, exist2) {
/** @param {Map<import('#compiler').RegularElement, NodeExistsValue>} result */
/** @param {Map<import('#compiler').RegularElement, NodeExistsValue>} result */
function mark _as _probably ( result ) {
function mark _as _probably ( result ) {
for ( const key of result . keys ( ) ) {
for ( const key of result . keys ( ) ) {
result . set ( key , N odeExist. Probably ) ;
result . set ( key , N ODE_PROBABLY _EXISTS ) ;
}
}
}
}
@ -646,7 +772,7 @@ function loop_child(children, adjacent_only) {
for ( let i = children . length - 1 ; i >= 0 ; i -- ) {
for ( let i = children . length - 1 ; i >= 0 ; i -- ) {
const child = children [ i ] ;
const child = children [ i ] ;
if ( child . type === 'RegularElement' ) {
if ( child . type === 'RegularElement' ) {
result . set ( child , N odeExist. Definitely ) ;
result . set ( child , N ODE_DEFINITELY _EXISTS ) ;
if ( adjacent _only ) {
if ( adjacent _only ) {
break ;
break ;
}
}