@@ -1739,4 +1739,241 @@ describe('parse_selector()', () => {
17391739 expect ( result . has_children ) . toBe ( true )
17401740 expect ( result . children . length ) . toBeGreaterThan ( 0 )
17411741 } )
1742+
1743+ describe ( 'Namespace selectors' , ( ) => {
1744+ test ( 'should parse ns|* (namespace with universal selector)' , ( ) => {
1745+ const result = parse_selector ( 'ns|*' )
1746+
1747+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1748+ expect ( result . text ) . toBe ( 'ns|*' )
1749+
1750+ const selector = result . first_child
1751+ expect ( selector ?. type ) . toBe ( NODE_SELECTOR )
1752+ expect ( selector ?. text ) . toBe ( 'ns|*' )
1753+
1754+ const universal = selector ?. first_child
1755+ expect ( universal ?. type ) . toBe ( NODE_SELECTOR_UNIVERSAL )
1756+ expect ( universal ?. text ) . toBe ( 'ns|*' )
1757+ expect ( universal ?. name ) . toBe ( 'ns' )
1758+ } )
1759+
1760+ test ( 'should parse ns|div (namespace with type selector)' , ( ) => {
1761+ const result = parse_selector ( 'ns|div' )
1762+
1763+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1764+ expect ( result . text ) . toBe ( 'ns|div' )
1765+
1766+ const selector = result . first_child
1767+ expect ( selector ?. type ) . toBe ( NODE_SELECTOR )
1768+
1769+ const typeSelector = selector ?. first_child
1770+ expect ( typeSelector ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1771+ expect ( typeSelector ?. text ) . toBe ( 'ns|div' )
1772+ expect ( typeSelector ?. name ) . toBe ( 'ns' )
1773+ } )
1774+
1775+ test ( 'should parse *|* (any namespace with universal selector)' , ( ) => {
1776+ const result = parse_selector ( '*|*' )
1777+
1778+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1779+ expect ( result . text ) . toBe ( '*|*' )
1780+
1781+ const selector = result . first_child
1782+ const universal = selector ?. first_child
1783+ expect ( universal ?. type ) . toBe ( NODE_SELECTOR_UNIVERSAL )
1784+ expect ( universal ?. text ) . toBe ( '*|*' )
1785+ expect ( universal ?. name ) . toBe ( '*' )
1786+ } )
1787+
1788+ test ( 'should parse *|div (any namespace with type selector)' , ( ) => {
1789+ const result = parse_selector ( '*|div' )
1790+
1791+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1792+ expect ( result . text ) . toBe ( '*|div' )
1793+
1794+ const selector = result . first_child
1795+ const typeSelector = selector ?. first_child
1796+ expect ( typeSelector ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1797+ expect ( typeSelector ?. text ) . toBe ( '*|div' )
1798+ expect ( typeSelector ?. name ) . toBe ( '*' )
1799+ } )
1800+
1801+ test ( 'should parse |* (empty namespace with universal selector)' , ( ) => {
1802+ const result = parse_selector ( '|*' )
1803+
1804+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1805+ expect ( result . text ) . toBe ( '|*' )
1806+
1807+ const selector = result . first_child
1808+ const universal = selector ?. first_child
1809+ expect ( universal ?. type ) . toBe ( NODE_SELECTOR_UNIVERSAL )
1810+ expect ( universal ?. text ) . toBe ( '|*' )
1811+ // Empty namespace should result in empty name
1812+ expect ( universal ?. name ) . toBe ( '|' )
1813+ } )
1814+
1815+ test ( 'should parse |div (empty namespace with type selector)' , ( ) => {
1816+ const result = parse_selector ( '|div' )
1817+
1818+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1819+ expect ( result . text ) . toBe ( '|div' )
1820+
1821+ const selector = result . first_child
1822+ const typeSelector = selector ?. first_child
1823+ expect ( typeSelector ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1824+ expect ( typeSelector ?. text ) . toBe ( '|div' )
1825+ // Empty namespace should result in empty name
1826+ expect ( typeSelector ?. name ) . toBe ( '|' )
1827+ } )
1828+
1829+ test ( 'should parse namespace selector with class' , ( ) => {
1830+ const result = parse_selector ( 'ns|div.class' )
1831+
1832+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1833+ expect ( result . text ) . toBe ( 'ns|div.class' )
1834+
1835+ const selector = result . first_child
1836+ const children = selector ?. children || [ ]
1837+ expect ( children . length ) . toBe ( 2 )
1838+ expect ( children [ 0 ] . type ) . toBe ( NODE_SELECTOR_TYPE )
1839+ expect ( children [ 0 ] . text ) . toBe ( 'ns|div' )
1840+ expect ( children [ 0 ] . name ) . toBe ( 'ns' )
1841+ expect ( children [ 1 ] . type ) . toBe ( NODE_SELECTOR_CLASS )
1842+ } )
1843+
1844+ test ( 'should parse namespace selector with ID' , ( ) => {
1845+ const result = parse_selector ( 'ns|*#id' )
1846+
1847+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1848+ expect ( result . text ) . toBe ( 'ns|*#id' )
1849+
1850+ const selector = result . first_child
1851+ const children = selector ?. children || [ ]
1852+ expect ( children . length ) . toBe ( 2 )
1853+ expect ( children [ 0 ] . type ) . toBe ( NODE_SELECTOR_UNIVERSAL )
1854+ expect ( children [ 0 ] . text ) . toBe ( 'ns|*' )
1855+ expect ( children [ 1 ] . type ) . toBe ( NODE_SELECTOR_ID )
1856+ } )
1857+
1858+ test ( 'should parse namespace selector in complex selector' , ( ) => {
1859+ const result = parse_selector ( 'ns|div > *|span' )
1860+
1861+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1862+ expect ( result . text ) . toBe ( 'ns|div > *|span' )
1863+
1864+ const selector = result . first_child
1865+ const children = selector ?. children || [ ]
1866+ expect ( children . length ) . toBe ( 3 ) // div, >, span
1867+ expect ( children [ 0 ] . type ) . toBe ( NODE_SELECTOR_TYPE )
1868+ expect ( children [ 0 ] . text ) . toBe ( 'ns|div' )
1869+ expect ( children [ 1 ] . type ) . toBe ( NODE_SELECTOR_COMBINATOR )
1870+ expect ( children [ 2 ] . type ) . toBe ( NODE_SELECTOR_TYPE )
1871+ expect ( children [ 2 ] . text ) . toBe ( '*|span' )
1872+ } )
1873+
1874+ test ( 'should parse namespace selector in selector list' , ( ) => {
1875+ const result = parse_selector ( 'ns|div, |span, *|p' )
1876+
1877+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1878+ expect ( result . text ) . toBe ( 'ns|div, |span, *|p' )
1879+
1880+ const selectors = result . children
1881+ expect ( selectors . length ) . toBe ( 3 )
1882+
1883+ const firstType = selectors [ 0 ] . first_child
1884+ expect ( firstType ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1885+ expect ( firstType ?. text ) . toBe ( 'ns|div' )
1886+ expect ( firstType ?. name ) . toBe ( 'ns' )
1887+
1888+ const secondType = selectors [ 1 ] . first_child
1889+ expect ( secondType ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1890+ expect ( secondType ?. text ) . toBe ( '|span' )
1891+ expect ( secondType ?. name ) . toBe ( '|' )
1892+
1893+ const thirdType = selectors [ 2 ] . first_child
1894+ expect ( thirdType ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1895+ expect ( thirdType ?. text ) . toBe ( '*|p' )
1896+ expect ( thirdType ?. name ) . toBe ( '*' )
1897+ } )
1898+
1899+ test ( 'should parse namespace selector with attribute' , ( ) => {
1900+ const result = parse_selector ( 'ns|div[attr="value"]' )
1901+
1902+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1903+ expect ( result . text ) . toBe ( 'ns|div[attr="value"]' )
1904+
1905+ const selector = result . first_child
1906+ const children = selector ?. children || [ ]
1907+ expect ( children . length ) . toBe ( 2 )
1908+ expect ( children [ 0 ] . type ) . toBe ( NODE_SELECTOR_TYPE )
1909+ expect ( children [ 0 ] . name ) . toBe ( 'ns' )
1910+ expect ( children [ 1 ] . type ) . toBe ( NODE_SELECTOR_ATTRIBUTE )
1911+ } )
1912+
1913+ test ( 'should parse namespace selector with pseudo-class' , ( ) => {
1914+ const result = parse_selector ( 'ns|a:hover' )
1915+
1916+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1917+ expect ( result . text ) . toBe ( 'ns|a:hover' )
1918+
1919+ const selector = result . first_child
1920+ const children = selector ?. children || [ ]
1921+ expect ( children . length ) . toBe ( 2 )
1922+ expect ( children [ 0 ] . type ) . toBe ( NODE_SELECTOR_TYPE )
1923+ expect ( children [ 0 ] . name ) . toBe ( 'ns' )
1924+ expect ( children [ 1 ] . type ) . toBe ( NODE_SELECTOR_PSEUDO_CLASS )
1925+ } )
1926+
1927+ test ( 'should parse namespace with various identifiers' , ( ) => {
1928+ const result = parse_selector ( 'svg|rect' )
1929+
1930+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1931+ expect ( result . text ) . toBe ( 'svg|rect' )
1932+
1933+ const selector = result . first_child
1934+ const typeSelector = selector ?. first_child
1935+ expect ( typeSelector ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1936+ expect ( typeSelector ?. text ) . toBe ( 'svg|rect' )
1937+ expect ( typeSelector ?. name ) . toBe ( 'svg' )
1938+ } )
1939+
1940+ test ( 'should parse long namespace identifier' , ( ) => {
1941+ const result = parse_selector ( 'myNamespace|element' )
1942+
1943+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1944+ expect ( result . text ) . toBe ( 'myNamespace|element' )
1945+
1946+ const selector = result . first_child
1947+ const typeSelector = selector ?. first_child
1948+ expect ( typeSelector ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1949+ expect ( typeSelector ?. name ) . toBe ( 'myNamespace' )
1950+ } )
1951+
1952+ test ( 'should handle namespace in nested pseudo-class' , ( ) => {
1953+ const result = parse_selector ( ':is(ns|div, *|span)' )
1954+
1955+ expect ( result . type ) . toBe ( NODE_SELECTOR_LIST )
1956+ expect ( result . text ) . toBe ( ':is(ns|div, *|span)' )
1957+
1958+ const selector = result . first_child
1959+ const pseudo = selector ?. first_child
1960+ expect ( pseudo ?. type ) . toBe ( NODE_SELECTOR_PSEUDO_CLASS )
1961+ expect ( pseudo ?. name ) . toBe ( 'is' )
1962+
1963+ // The content should contain namespace selectors
1964+ const nestedList = pseudo ?. first_child
1965+ expect ( nestedList ?. type ) . toBe ( NODE_SELECTOR_LIST )
1966+
1967+ const nestedSelectors = nestedList ?. children || [ ]
1968+ expect ( nestedSelectors . length ) . toBe ( 2 )
1969+
1970+ const firstNestedType = nestedSelectors [ 0 ] . first_child
1971+ expect ( firstNestedType ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1972+ expect ( firstNestedType ?. text ) . toBe ( 'ns|div' )
1973+
1974+ const secondNestedType = nestedSelectors [ 1 ] . first_child
1975+ expect ( secondNestedType ?. type ) . toBe ( NODE_SELECTOR_TYPE )
1976+ expect ( secondNestedType ?. text ) . toBe ( '*|span' )
1977+ } )
1978+ } )
17421979} )
0 commit comments