-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-selector.js
2 lines (2 loc) · 4.1 KB
/
query-selector.js
1
2
function t(t,i){return s(t,function(t){t=t.trim();const i=[];let r=null;do{let e="";if(r){const n=t.match(/^\s*(>|\+|~|,| )\s*/);if(!n)throw Error("Unable to parse joiner at "+JSON.stringify(t));e=n[1],t=t.substring(n[0].length),","==e&&(i.push(r),r=null,e="")}if(!t)throw Error("unable to parse the empty string as a selector");const s=n(t),o=s[0];r&&(o.p=[e,r]),r=o,t=s[1]}while(t);return i.push(r),i}(i))}function n(t){const n={};{const i=t.match(/^[\p{L}_][0-9\p{L}_-]*/u);i?(n.t=i[0],t=t.substring(i[0].length)):t.startsWith("*")&&(n.t=void 0,t=t.substring(1))}for(;t;){if(t.startsWith("#")){const i=t.match(/^#[\p{L}_][0-9\p{L}_-]*/u);if(i){const r=i[0].substring(1);if(n.i&&n.i!=r)throw Error("selector will always fail, elements can't contain multiple ids");n.i=r,t=t.substring(i[0].length);continue}throw Error("invalid id at "+JSON.stringify(t))}if(t.startsWith(".")){const i=t.match(/^\.[\p{L}_][0-9\p{L}_-]*/u);if(i){n.c||(n.c=new Set),n.c.add(i[0].substring(1)),t=t.substring(i[0].length);continue}throw Error("invalid class at "+JSON.stringify(t))}if(t.startsWith("[")){const r=i(t=t.substring(1)),e=r[0].n;if(n.a){const t=n.a.get(e);n.a.set(e,o(t,r[0].v))}else{const t=new Map,i=r[0].v;t.set(e,void 0===i?null:"string"==typeof i?i:[i]),n.a=t}if((t=r[1]).startsWith("]")){t=t.substring(1);continue}throw Error("unexpected characters in attribute section: "+JSON.stringify(t))}}if(Object.keys(n).length)return[n,t];throw Error("unexpected part at "+JSON.stringify(t))}function i(t){const n=t.match(/^[\p{L}_][0-9\p{L}._-]*/u);if(!n)throw Error("expected attribute name, found "+JSON.stringify(t));const i={n:n[0]};let e;if((t=t.substring(n[0].length)).startsWith("=")?(e="=",t=t.substring(1)):t.startsWith("*=")?(e="*",t=t.substring(2)):t.startsWith("~=")?(e="~",t=t.substring(2)):t.startsWith("|=")?(e="|",t=t.substring(2)):t.startsWith("$=")?(e="$",t=t.substring(2)):t.startsWith("^=")&&(e="^",t=t.substring(2)),e){const n=function(t){if(t.startsWith('"')){const n=t.indexOf('"',1);if(-1!=n)return[t.substring(1,n),t.substring(n+1)];throw Error("no end of string at "+JSON.stringify(t))}const n=t.match(/^[\d\p{L}._-]+/u);if(n)return[n[0],t.substring(n[0].length)];throw Error("unexpected unquoted data at "+JSON.stringify(t))}(t);t=n[1];const s=n[0];"="==e?i.v=s:"*"==e?i.v=t=>t.includes(s):"|"==e?i.v=t=>t==s||t.startsWith(s+"-"):"^"==e?i.v=t=>t.startsWith(s):"$"==e?i.v=t=>t.endsWith(s):"~"==e&&(i.v=t=>r(t,s))}if(t.startsWith("]"))return[i,t.substring(1)];throw Error("expected end of attribute section but found "+JSON.stringify(t))}function r(t,n){let i=0;for(;i!=t.length;){const r=t.indexOf(" ",i+1),e=-1==r?t.length:r;if(e-i==n.length&&t.substring(i,e)==n)return!0;i=e}return!1}function e(t,n,i){t:for(const s of n)if(!s.t||s.t==i.localName){if(s.i){if("function"!=typeof i.getAttribute)continue t;const t=i.getAttribute("id");if(!t||t!=s.i)continue t}if(s.c){if("function"!=typeof i.getAttribute)continue t;const t=i.getAttribute("class");if(!t)continue t;for(const n of s.c)if(!r(t,n))continue t}if(s.a){if("function"!=typeof i.getAttribute)continue t;for(const[t,n]of s.a){const r=i.getAttribute(t);if(!r)continue t;if("string"==typeof n){if(r==n)continue;continue t}if(n)for(const t of n)if(!t(r))continue t}}if(s.p){const n=s.p[0];if("+"==n){const n=i.previousSibling;if(!n)continue t;if(!e(t,[s.p[1]],n))continue t}else if("~"==n){let n=i.previousSibling,r=!1;const o=[s.p[1]];for(;!r&&n;)r=e(t,o,n),n=n.previousSibling;if(!r)continue t}else if(">"==n){const n=i.parentNode;if(!n)continue t;if(!e(t,[s.p[1]],n))continue t}else if(" "==n){let n=i.parentNode,r=!1;const o=[s.p[1]];for(;!r&&n;)r=e(t,o,n),n=n.parentNode;if(!r)continue t}}return!0}return!1}function*s(t,n){const i=t.childNodes;if(i)for(const r of i)1==r.nodeType&&(e(t,n,r)&&(yield r),yield*s(r,n))}function o(t,n){if(void 0===n)return void 0!==t?t:null;if("string"==typeof n){if(null==t)return n;if("string"==typeof t){if(t==n)return t}else{let i=!1;for(const r of t)r(n)||(i=!0);if(!i)return n}}else{if(null==t)return[n];if("string"!=typeof t)return t.push(n),t;if(n(t))return t}throw Error("no possible attribute could ever match")}export{t as querySel};
//# sourceMappingURL=query-selector.js.map