@@ -1313,7 +1313,40 @@ const compareStyles = async ({ pairs, types }) => {
13131313 return out ;
13141314 } ;
13151315
1316- /** @typedef {{ combinator: string, type: string, classes: string[], id: string, attributes: [string, string][] } } Compound */
1316+ /** @typedef {{ combinator: string, type: string, classes: string[], id: string, attributes: [string, string][], before: number, after: number, typed: boolean, counted: string, has: { combinator: string, chain: Compound[] }[] } } Compound */
1317+
1318+ /**
1319+ * The position among its siblings an `An+B` argument picks for an element to
1320+ * stand at: the second it matches, so `odd` is not also `:first-child`.
1321+ * @param {string } argument the argument, an `of S` part included
1322+ * @returns {number } the position, counted from 1, or 0 where none is built
1323+ */
1324+ const nthPosition = ( argument ) => {
1325+ const text = argument
1326+ . replace ( / \s + o f \s [ \s \S ] * $ / i, "" )
1327+ . replace ( / \s + / g, "" )
1328+ . toLowerCase ( ) ;
1329+ const spelled = text === "odd" ? "2n+1" : text === "even" ? "2n" : text ;
1330+ const match = / ^ (?: ( [ + - ] ? \d * ) n ) ? ( [ + - ] ? \d + ) ? $ / . exec ( spelled ) ;
1331+ if ( match === null || spelled === "" ) return 0 ;
1332+ const step =
1333+ match [ 1 ] === undefined
1334+ ? 0
1335+ : match [ 1 ] === "" || match [ 1 ] === "+"
1336+ ? 1
1337+ : match [ 1 ] === "-"
1338+ ? - 1
1339+ : Number ( match [ 1 ] ) ;
1340+ const offset = match [ 2 ] === undefined ? 0 : Number ( match [ 2 ] ) ;
1341+ let position = offset ;
1342+ if ( step > 0 ) {
1343+ const first =
1344+ offset >= 1 ? offset : offset + step * Math . ceil ( ( 1 - offset ) / step ) ;
1345+ position = first + step ;
1346+ }
1347+ // A sibling list is built for it, so a far position is left unbuilt.
1348+ return position >= 1 && position <= 12 ? position : 0 ;
1349+ } ;
13171350
13181351 /**
13191352 * A selector as the compounds an element chain has to carry. A pseudo-class is
@@ -1336,8 +1369,35 @@ const compareStyles = async ({ pairs, types }) => {
13361369 type : "" ,
13371370 classes : [ ] ,
13381371 id : "" ,
1339- attributes : [ ]
1372+ attributes : [ ] ,
1373+ before : 0 ,
1374+ after : 0 ,
1375+ typed : false ,
1376+ counted : "" ,
1377+ has : [ ]
13401378 } ) ;
1379+ /**
1380+ * Takes in the first selector of a list when it is one compound, since the
1381+ * element has to match it too.
1382+ * @param {Compound } into the compound to take it into
1383+ * @param {string } list the selector list
1384+ * @returns {void }
1385+ */
1386+ const absorb = ( into , list ) => {
1387+ const [ first ] = splitList ( list ) ;
1388+ const inner = parseSelector ( first ) ;
1389+ if ( inner === null || inner . length !== 1 ) return ;
1390+ const [ one ] = inner ;
1391+ if ( one . type !== "" ) into . type = one . type ;
1392+ if ( one . id !== "" ) into . id = one . id ;
1393+ into . classes . push ( ...one . classes ) ;
1394+ into . attributes . push ( ...one . attributes ) ;
1395+ into . before = Math . max ( into . before , one . before ) ;
1396+ into . after = Math . max ( into . after , one . after ) ;
1397+ into . typed = into . typed || one . typed ;
1398+ if ( one . counted !== "" ) into . counted = one . counted ;
1399+ into . has . push ( ...one . has ) ;
1400+ } ;
13411401 /** @type {Compound[] } */
13421402 const chain = [ ] ;
13431403 let current = fresh ( "" ) ;
@@ -1386,18 +1446,34 @@ const compareStyles = async ({ pairs, types }) => {
13861446 else if ( selector [ at ] === ")" && -- depth === 0 ) break ;
13871447 }
13881448 at ++ ;
1389- if ( / ^ : (?: i s | w h e r e ) $ / i. test ( name [ 0 ] ) ) {
1390- const [ first ] = splitList ( selector . slice ( open + 1 , at - 1 ) ) ;
1391- const inner = parseSelector ( first ) ;
1392- // One compound only: a chain inside cannot stand in this one's place.
1393- if ( inner !== null && inner . length === 1 ) {
1394- const [ one ] = inner ;
1395- if ( one . type !== "" ) current . type = one . type ;
1396- if ( one . id !== "" ) current . id = one . id ;
1397- current . classes . push ( ...one . classes ) ;
1398- current . attributes . push ( ...one . attributes ) ;
1449+ const argument = selector . slice ( open + 1 , at - 1 ) ;
1450+ const pseudo = name [ 0 ] . toLowerCase ( ) ;
1451+ if ( / ^ : n t h - (?: l a s t - ) ? (?: c h i l d | o f - t y p e ) $ / . test ( pseudo ) ) {
1452+ const position = nthPosition ( argument ) - 1 ;
1453+ if ( pseudo . startsWith ( ":nth-last" ) ) {
1454+ current . after = Math . max ( current . after , position ) ;
1455+ } else {
1456+ current . before = Math . max ( current . before , position ) ;
1457+ }
1458+ if ( pseudo . endsWith ( "of-type" ) ) current . typed = true ;
1459+ // `of S` counts only siblings matching S, which it matches too.
1460+ const of = / \s o f \s ( [ \s \S ] * ) $ / i. exec ( argument ) ;
1461+ if ( of !== null ) {
1462+ current . counted = of [ 1 ] . trim ( ) ;
1463+ absorb ( current , of [ 1 ] ) ;
1464+ }
1465+ } else if ( pseudo === ":has" ) {
1466+ const [ relative ] = splitList ( argument ) ;
1467+ const leading = / ^ \s * ( [ > + ~ ] ? ) \s * / . exec ( relative ) ;
1468+ const combinator = leading === null ? "" : leading [ 1 ] ;
1469+ const inner = parseSelector (
1470+ relative . slice ( leading === null ? 0 : leading [ 0 ] . length )
1471+ ) ;
1472+ if ( inner !== null ) {
1473+ current . has . push ( { combinator : combinator || " " , chain : inner } ) ;
13991474 }
14001475 }
1476+ if ( / ^ : (?: i s | w h e r e ) $ / i. test ( name [ 0 ] ) ) absorb ( current , argument ) ;
14011477 }
14021478 } else {
14031479 const name = IDENTIFIER . exec ( rest ) ;
@@ -1438,6 +1514,85 @@ const compareStyles = async ({ pairs, types }) => {
14381514 return element ;
14391515 } ;
14401516
1517+ /**
1518+ * Counts the siblings on one side of an element that its `:nth-*()` counts.
1519+ * @param {Element } element the element
1520+ * @param {Compound } compound what it carries
1521+ * @param {boolean } following whether the ones after it, not before
1522+ * @returns {number } how many there are
1523+ */
1524+ const countSiblings = ( element , compound , following ) => {
1525+ let count = 0 ;
1526+ for (
1527+ let at = following
1528+ ? element . nextElementSibling
1529+ : element . previousElementSibling ;
1530+ at !== null ;
1531+ at = following ? at . nextElementSibling : at . previousElementSibling
1532+ ) {
1533+ if ( compound . typed && at . localName !== element . localName ) continue ;
1534+ if ( compound . counted !== "" ) {
1535+ try {
1536+ if ( ! at . matches ( compound . counted ) ) continue ;
1537+ } catch ( _err ) {
1538+ // A selector the engine will not read counts no sibling.
1539+ continue ;
1540+ }
1541+ }
1542+ count ++ ;
1543+ }
1544+ return count ;
1545+ } ;
1546+
1547+ /**
1548+ * Builds a chain where its selector reaches its last element: each compound
1549+ * at the position among its siblings it names, holding what its `:has()` asks.
1550+ * @param {Document } doc the document to build in
1551+ * @param {Compound[] } chain the compounds
1552+ * @param {Element } anchor what the first compound joins
1553+ * @param {string } combinator how it joins it — a relative selector's own
1554+ * @returns {Element | null } the last element, or null where none was built
1555+ */
1556+ const buildChain = ( doc , chain , anchor , combinator ) => {
1557+ /** @type {Element | null } */
1558+ let last = null ;
1559+ for ( const compound of chain ) {
1560+ // A document has one of each, and the root already stands inside them.
1561+ if ( DOCUMENT_ELEMENTS . has ( compound . type ) ) {
1562+ if ( last === null ) continue ;
1563+ break ;
1564+ }
1565+ const element = build ( doc , compound ) ;
1566+ const joined = last === null ? combinator : compound . combinator ;
1567+ const target = last === null ? anchor : last ;
1568+ if ( joined === "+" || joined === "~" ) target . after ( element ) ;
1569+ else target . append ( element ) ;
1570+ const sibling = ( ) =>
1571+ build ( doc , { ...compound , id : "" , before : 0 , after : 0 , has : [ ] } ) ;
1572+ // In front of the pair `+` joins, which cannot be parted.
1573+ const front = joined === "+" ? target : element ;
1574+ for (
1575+ let i = compound . before - countSiblings ( element , compound , false ) ;
1576+ i > 0 ;
1577+ i --
1578+ ) {
1579+ front . before ( sibling ( ) ) ;
1580+ }
1581+ for (
1582+ let i = compound . after - countSiblings ( element , compound , true ) ;
1583+ i > 0 ;
1584+ i --
1585+ ) {
1586+ element . after ( sibling ( ) ) ;
1587+ }
1588+ for ( const held of compound . has ) {
1589+ buildChain ( doc , held . chain , element , held . combinator ) ;
1590+ }
1591+ last = element ;
1592+ }
1593+ return last ;
1594+ } ;
1595+
14411596 /**
14421597 * @returns {Promise<HTMLIFrameElement> } a frame holding an empty standards-mode document
14431598 */
@@ -1943,26 +2098,7 @@ const compareStyles = async ({ pairs, types }) => {
19432098 for ( const selector of selectors ) {
19442099 const chain = chains . get ( selector ) ;
19452100 if ( chain === undefined ) continue ;
1946- const inside = box ( ) ;
1947- /** @type {Element | null } */
1948- let last = null ;
1949- for ( const compound of chain ) {
1950- // A document has one of each, and the root already stands inside them.
1951- if ( DOCUMENT_ELEMENTS . has ( compound . type ) ) {
1952- if ( last === null ) continue ;
1953- break ;
1954- }
1955- const element = build ( doc , compound ) ;
1956- if (
1957- last !== null &&
1958- ( compound . combinator === "+" || compound . combinator === "~" )
1959- ) {
1960- last . after ( element ) ;
1961- } else {
1962- ( last === null ? inside : last ) . append ( element ) ;
1963- }
1964- last = element ;
1965- }
2101+ const last = buildChain ( doc , chain , box ( ) , "" ) ;
19662102 // The element the selector reaches; the ones on the way are reached by
19672103 // selectors of their own.
19682104 if ( last !== null ) {
@@ -1985,12 +2121,18 @@ const compareStyles = async ({ pairs, types }) => {
19852121 }
19862122 }
19872123 }
2124+ /** @type {Compound } */
19882125 const everything = {
19892126 combinator : "" ,
19902127 type : "" ,
19912128 classes : [ ...classes ] ,
19922129 id : "" ,
1993- attributes : [ ...attributes ]
2130+ attributes : [ ...attributes ] ,
2131+ before : 0 ,
2132+ after : 0 ,
2133+ typed : false ,
2134+ counted : "" ,
2135+ has : [ ]
19942136 } ;
19952137 for ( const type of named ) {
19962138 const outer = build ( doc , { ...everything , type } ) ;
0 commit comments