@asamuzakjp/dom-selector
Advanced tools
Comparing version 4.1.0 to 4.1.1
@@ -1,3 +0,3 @@ | ||
var E=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var y=(s,t)=>{for(var r in t)E(s,r,{get:t[r],enumerable:!0})},T=(s,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of O(t))!x.call(s,n)&&n!==r&&E(s,n,{get:()=>t[n],enumerable:!(i=S(t,n))||i.enumerable});return s};var m=s=>T(E({},"__esModule",{value:!0}),s);var C={};y(C,{filterSelector:()=>L,generateCSS:()=>w.generate,parseAstName:()=>I,parseSelector:()=>d,preprocess:()=>u,sortAST:()=>A,unescapeSelector:()=>_,walkAST:()=>$});module.exports=m(C);var h=require("css-tree"),e=require("./constant.js"),w=require("css-tree");const _=(s="")=>{if(typeof s=="string"&&s.indexOf("\\",0)>=0){const t=s.split("\\"),r=t.length;for(let i=1;i<r;i++){let n=t[i];if(n===""&&i===r-1)n=e.U_FFFD;else{const c=/^([\da-f]{1,6}\s?)/i.exec(n);if(c){const[,a]=c;let l;try{const o=parseInt("D800",e.HEX),p=parseInt("DFFF",e.HEX),g=parseInt(a,e.HEX);g===0||g>=o&&g<=p?l=e.U_FFFD:l=String.fromCodePoint(g)}catch{l=e.U_FFFD}let f="";n.length>a.length&&(f=n.substring(a.length)),n=`${l}${f}`}else/^[\n\r\f]/.test(n)&&(n="\\"+n)}t[i]=n}s=t.join("")}return s},u=(...s)=>{if(!s.length)throw new TypeError("1 argument required, but only 0 present.");let[t]=s;if(typeof t=="string"){let r=0;for(;r>=0&&(r=t.indexOf("#",r),!(r<0));){const i=t.substring(0,r+1);let n=t.substring(r+1);const c=n.codePointAt(0);if(c===e.BIT_HYPHEN){if(/^\d$/.test(n.substring(1,2)))throw new DOMException(`Invalid selector ${t}`,e.SYNTAX_ERR)}else if(c>e.BIT_FFFF){const a=`\\${c.toString(e.HEX)} `;n.length===e.DUO?n=a:n=`${a}${n.substring(e.DUO)}`}t=`${i}${n}`,r++}t=t.replace(/\f|\r\n?/g,` | ||
`).replace(/[\0\uD800-\uDFFF]|\\$/g,e.U_FFFD)}else if(t==null)t=Object.prototype.toString.call(t).slice(e.TYPE_FROM,e.TYPE_TO).toLowerCase();else if(Array.isArray(t))t=t.join(",");else if(Object.prototype.hasOwnProperty.call(t,"toString"))t=t.toString();else throw new DOMException(`Invalid selector ${t}`,e.SYNTAX_ERR);return t},d=s=>{if(s=u(s),/^$|^\s*>|,\s*$/.test(s))throw new DOMException(`Invalid selector ${s}`,e.SYNTAX_ERR);let t;try{const r=(0,h.parse)(s,{context:"selectorList",parseCustomProperty:!0});t=(0,h.toPlainObject)(r)}catch(r){const i=/(:lang\(\s*("[A-Za-z\d\-*]+")\s*\))/;if(r.message==="Identifier is expected"&&i.test(s)){const[,n,c]=i.exec(s),a=c.replaceAll("*","\\*").replace(/^"/,"").replace(/"$/,""),l=n.replace(c,a);t=d(s.replace(n,l))}else if(r.message==='"]" is expected'&&!s.endsWith("]"))t=d(`${s}]`);else if(r.message==='")" is expected'&&!s.endsWith(")"))t=d(`${s})`);else throw new DOMException(r.message,e.SYNTAX_ERR)}return t},$=(s={})=>{const t=new Set;let r;return(0,h.walk)(s,{enter:n=>{n.type===e.SELECTOR?t.add(n.children):(n.type===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(n.name)||n.type===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(n.name))&&(r=!0)}}),r&&(0,h.findAll)(s,(n,c,a)=>{if(a){if(n.type===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(n.name)){const l=a.filter(f=>{const{name:o,type:p}=f;return p===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(o)});for(const{children:f}of l)for(const{children:o}of f)for(const{children:p}of o)t.has(p)&&t.delete(p)}else if(n.type===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(n.name)){const l=a.filter(f=>{const{name:o,type:p}=f;return p===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(o)});for(const{children:f}of l)for(const{children:o}of f)t.has(o)&&t.delete(o)}}}),[...t]},A=s=>{const t=[...s];if(t.length>1){const r=new Map([[e.SELECTOR_PSEUDO_ELEMENT,e.BIT_01],[e.SELECTOR_ID,e.BIT_02],[e.SELECTOR_CLASS,e.BIT_04],[e.SELECTOR_TYPE,e.BIT_08],[e.SELECTOR_ATTR,e.BIT_16],[e.SELECTOR_PSEUDO_CLASS,e.BIT_32]]);t.sort((i,n)=>{const{type:c}=i,{type:a}=n,l=r.get(c),f=r.get(a);let o;return l===f?o=0:l>f?o=1:o=-1,o})}return t},I=s=>{let t,r;if(s&&typeof s=="string")s.indexOf("|")>-1?[t,r]=s.split("|"):(t="*",r=s);else throw new DOMException(`Invalid selector ${s}`,e.SYNTAX_ERR);return{prefix:t,localName:r}},L=s=>!(!s||typeof s!="string"||/\||::|\[\s*[\w=~|^$*-]+(?:\s+[\w=~|^$*-]+)+\s*\]|\[\s*[\w=~|^$*-]+["'][\w=~|^$*\s-]+['"]+\s+[\w=~|^$*-]+\s*\]/.test(s)||s.includes(":")&&/:(?:is|not|where)(?!\(\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[.#:][\w-]+)+)(?:\s*,\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[.#:][\w-]+)+))*\s*\))|:nth-(?:last-)?child\([^()]{1,255}\sof[^()]{1,255}\)|:(?!(?:nth(?:-last)?|first|last|only)-(?:of-type|child)|is|not|where)/.test(s));0&&(module.exports={filterSelector,generateCSS,parseAstName,parseSelector,preprocess,sortAST,unescapeSelector,walkAST}); | ||
var E=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var x=(s,t)=>{for(var r in t)E(s,r,{get:t[r],enumerable:!0})},T=(s,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of y(t))!O.call(s,n)&&n!==r&&E(s,n,{get:()=>t[n],enumerable:!(i=S(t,n))||i.enumerable});return s};var m=s=>T(E({},"__esModule",{value:!0}),s);var C={};x(C,{filterSelector:()=>L,generateCSS:()=>w.generate,parseAstName:()=>I,parseSelector:()=>h,preprocess:()=>u,sortAST:()=>A,unescapeSelector:()=>_,walkAST:()=>$});module.exports=m(C);var d=require("css-tree"),e=require("./constant.js"),w=require("css-tree");const _=(s="")=>{if(typeof s=="string"&&s.indexOf("\\",0)>=0){const t=s.split("\\"),r=t.length;for(let i=1;i<r;i++){let n=t[i];if(n===""&&i===r-1)n=e.U_FFFD;else{const c=/^([\da-f]{1,6}\s?)/i.exec(n);if(c){const[,a]=c;let l;try{const o=parseInt("D800",e.HEX),p=parseInt("DFFF",e.HEX),g=parseInt(a,e.HEX);g===0||g>=o&&g<=p?l=e.U_FFFD:l=String.fromCodePoint(g)}catch{l=e.U_FFFD}let f="";n.length>a.length&&(f=n.substring(a.length)),n=`${l}${f}`}else/^[\n\r\f]/.test(n)&&(n="\\"+n)}t[i]=n}s=t.join("")}return s},u=(...s)=>{if(!s.length)throw new TypeError("1 argument required, but only 0 present.");let[t]=s;if(typeof t=="string"){let r=0;for(;r>=0&&(r=t.indexOf("#",r),!(r<0));){const i=t.substring(0,r+1);let n=t.substring(r+1);const c=n.codePointAt(0);if(c===e.BIT_HYPHEN){if(/^\d$/.test(n.substring(1,2)))throw new DOMException(`Invalid selector ${t}`,e.SYNTAX_ERR)}else if(c>e.BIT_FFFF){const a=`\\${c.toString(e.HEX)} `;n.length===e.DUO?n=a:n=`${a}${n.substring(e.DUO)}`}t=`${i}${n}`,r++}t=t.replace(/\f|\r\n?/g,` | ||
`).replace(/[\0\uD800-\uDFFF]|\\$/g,e.U_FFFD)}else if(t==null)t=Object.prototype.toString.call(t).slice(e.TYPE_FROM,e.TYPE_TO).toLowerCase();else if(Array.isArray(t))t=t.join(",");else if(Object.prototype.hasOwnProperty.call(t,"toString"))t=t.toString();else throw new DOMException(`Invalid selector ${t}`,e.SYNTAX_ERR);return t},h=s=>{if(s=u(s),/^$|^\s*>|,\s*$/.test(s))throw new DOMException(`Invalid selector ${s}`,e.SYNTAX_ERR);let t;try{const r=(0,d.parse)(s,{context:"selectorList",parseCustomProperty:!0});t=(0,d.toPlainObject)(r)}catch(r){const i=/(:lang\(\s*("[A-Za-z\d\-*]+")\s*\))/;if(r.message==="Identifier is expected"&&i.test(s)){const[,n,c]=i.exec(s),a=c.replaceAll("*","\\*").replace(/^"/,"").replace(/"$/,""),l=n.replace(c,a);t=h(s.replace(n,l))}else if(r.message==='"]" is expected'&&!s.endsWith("]"))t=h(`${s}]`);else if(r.message==='")" is expected'&&!s.endsWith(")"))t=h(`${s})`);else throw new DOMException(r.message,e.SYNTAX_ERR)}return t},$=(s={})=>{const t=new Set;let r;return(0,d.walk)(s,{enter:n=>{n.type===e.SELECTOR?t.add(n.children):(n.type===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(n.name)||n.type===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(n.name))&&(r=!0)}}),r&&(0,d.findAll)(s,(n,c,a)=>{if(a){if(n.type===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(n.name)){const l=a.filter(f=>{const{name:o,type:p}=f;return p===e.SELECTOR_PSEUDO_CLASS&&e.REG_LOGICAL_PSEUDO.test(o)});for(const{children:f}of l)for(const{children:o}of f)for(const{children:p}of o)t.has(p)&&t.delete(p)}else if(n.type===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(n.name)){const l=a.filter(f=>{const{name:o,type:p}=f;return p===e.SELECTOR_PSEUDO_ELEMENT&&e.REG_SHADOW_PSEUDO.test(o)});for(const{children:f}of l)for(const{children:o}of f)t.has(o)&&t.delete(o)}}}),[...t]},A=s=>{const t=[...s];if(t.length>1){const r=new Map([[e.SELECTOR_PSEUDO_ELEMENT,e.BIT_01],[e.SELECTOR_ID,e.BIT_02],[e.SELECTOR_CLASS,e.BIT_04],[e.SELECTOR_TYPE,e.BIT_08],[e.SELECTOR_ATTR,e.BIT_16],[e.SELECTOR_PSEUDO_CLASS,e.BIT_32]]);t.sort((i,n)=>{const{type:c}=i,{type:a}=n,l=r.get(c),f=r.get(a);let o;return l===f?o=0:l>f?o=1:o=-1,o})}return t},I=s=>{let t,r;if(s&&typeof s=="string")s.indexOf("|")>-1?[t,r]=s.split("|"):(t="*",r=s);else throw new DOMException(`Invalid selector ${s}`,e.SYNTAX_ERR);return{prefix:t,localName:r}},L=s=>!(!s||typeof s!="string"||/\||::|\[\s*[\w$*=^|~-]+(?:\s+[\w$*=^|~-]+)+\s*\]|\[\s*[\w$*=^|~-]+["'][\w$*=^|~\s-]+['"]+\s+[\w$*=^|~-]+\s*\]/.test(s)||s.includes(":")&&/:(?!(?:first|last|only)-(?:child|of-type)|nth-(?:last-)?(?:child|of-type)\(\s*(?:even|odd|n|[+-]?(?:0|[1-9]\d*)n?|(?:[+-]?(?:0|[1-9]\d*))?n\s*[+-]\s*(?:0|[1-9]\d*))\s*\)|(?:is|not|where)\(\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[#.:][\w-]+)+)(?:\s*,\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[#.:][\w-]+)+))*\s*\))/.test(s));0&&(module.exports={filterSelector,generateCSS,parseAstName,parseSelector,preprocess,sortAST,unescapeSelector,walkAST}); | ||
//# sourceMappingURL=parser.js.map |
@@ -63,3 +63,3 @@ { | ||
}, | ||
"version": "4.1.0" | ||
"version": "4.1.1" | ||
} |
@@ -162,5 +162,5 @@ # DOM Selector | ||
|E:nth‑last‑col(n)|Unsupported| | | ||
|:host|✓| | | ||
|:host(s)|✓| | | ||
|:host‑context(s)|✓| | | ||
|E:host|✓| | | ||
|E:host(s)|✓| | | ||
|E:host‑context(s)|✓| | | ||
@@ -288,4 +288,4 @@ | ||
- [jsdom](https://github.com/jsdom/jsdom) | ||
- [nwsapi](https://github.com/dperini/nwsapi) | ||
--- | ||
@@ -292,0 +292,0 @@ Copyright (c) 2023 [asamuzaK (Kazz)](https://github.com/asamuzaK/) |
@@ -292,3 +292,3 @@ /** | ||
// and attribute selectors with case flag, i.e. [attr i], [attr s] | ||
if (/\||::|\[\s*[\w=~|^$*-]+(?:\s+[\w=~|^$*-]+)+\s*\]|\[\s*[\w=~|^$*-]+["'][\w=~|^$*\s-]+['"]+\s+[\w=~|^$*-]+\s*\]/.test(selector)) { | ||
if (/\||::|\[\s*[\w$*=^|~-]+(?:\s+[\w$*=^|~-]+)+\s*\]|\[\s*[\w$*=^|~-]+["'][\w$*=^|~\s-]+['"]+\s+[\w$*=^|~-]+\s*\]/.test(selector)) { | ||
return false; | ||
@@ -298,15 +298,17 @@ } | ||
if (selector.includes(':')) { | ||
// filter logical combinations with complex selector, e.g. :is(.foo > .bar): | ||
// :(?:is|not|where)(?!\(\s*(?:${compound}(?:\s*,\s*${compound})*)\s*\)) | ||
// compound selector: | ||
// filter pseudos other than child-indexed and logical combination pseudos | ||
// :(?!(?:first|last|only)-(?:child|of-type)|${nth}|${logical}) | ||
// nth: exclude An+B with selector list, e.g. :nth-child(2n+1 of .foo) | ||
// nth-(?:last-)?(?:child|of-type)\(\s*(?:even|odd|${an+b})\s*\) | ||
// an+b: | ||
// n|[+-]?(?:0|[1-9]\d*)n?|(?:[+-]?(?:0|[1-9]\d*))?n\s*[+-]\s*(?:0|[1-9]\d*) | ||
// logical: exclude complex selector, e.g. :is(.foo > .bar) | ||
// (?:is|not|where)\(\s*${compound}(?:\s*,\s*${compound})*\s*\) | ||
// compound: | ||
// (?:${type}|(?:${type})?(?:${subclass})+) | ||
// type selector: *, tag | ||
// type: *, tag | ||
// \*|[A-Za-z][\w-]* | ||
// subclass selector: attr, class, id, pseudo-class | ||
// \[[^\]]+\]|[.#:][\w-]+ | ||
// filter An+B with selector list e.g. :nth-child(an+b of .foo): | ||
// :nth-(?:last-)?child\([^()]{1,255}\sof[^()]{1,255}\) | ||
// filter pseudos other than child-indexed and logical combination pseudos: | ||
// :(?!(?:nth(?:-last)?|first|last|only)-(?:of-type|child)|is|not|where) | ||
if (/:(?:is|not|where)(?!\(\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[.#:][\w-]+)+)(?:\s*,\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[.#:][\w-]+)+))*\s*\))|:nth-(?:last-)?child\([^()]{1,255}\sof[^()]{1,255}\)|:(?!(?:nth(?:-last)?|first|last|only)-(?:of-type|child)|is|not|where)/.test(selector)) { | ||
// subclass: attr, class, id, pseudo-class | ||
// \[[^\]]+\]|[#.:][\w-]+ | ||
if (/:(?!(?:first|last|only)-(?:child|of-type)|nth-(?:last-)?(?:child|of-type)\(\s*(?:even|odd|n|[+-]?(?:0|[1-9]\d*)n?|(?:[+-]?(?:0|[1-9]\d*))?n\s*[+-]\s*(?:0|[1-9]\d*))\s*\)|(?:is|not|where)\(\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[#.:][\w-]+)+)(?:\s*,\s*(?:\*|[A-Za-z][\w-]*|(?:\*|[A-Za-z][\w-]*)?(?:\[[^\]]+\]|[#.:][\w-]+)+))*\s*\))/.test(selector)) { | ||
return false; | ||
@@ -313,0 +315,0 @@ } |
Sorry, the diff of this file is not supported yet
376480
4340