32 lines
11 KiB
JavaScript
32 lines
11 KiB
JavaScript
|
|
var F={attribute:/\[\s*(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)\s*(?:(?<operator>\W?=)\s*(?<value>.+?)\s*(\s(?<caseSensitive>[iIsS]))?\s*)?\]/gu,id:/#(?<name>[-\w\P{ASCII}]+)/gu,class:/\.(?<name>[-\w\P{ASCII}]+)/gu,comma:/\s*,\s*/g,combinator:/\s*[\s>+~]\s*/g,"pseudo-element":/::(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu,"pseudo-class":/:(?<name>[-\w\P{ASCII}]+)(?:\((?<argument>¶*)\))?/gu,universal:/(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?\*/gu,type:/(?:(?<namespace>\*|[-\w\P{ASCII}]*)\|)?(?<name>[-\w\P{ASCII}]+)/gu},kr=new Set(["combinator","comma"]),Ir=new Set(["not","is","where","has","matches","-moz-any","-webkit-any","nth-child","nth-last-child"]),ar=/(?<index>[\dn+-]+)\s+of\s+(?<subtree>.+)/,Or={"nth-child":ar,"nth-last-child":ar},Tr=r=>{switch(r){case"pseudo-element":case"pseudo-class":return new RegExp(F[r].source.replace("(?<argument>\xB6*)","(?<argument>.*)"),"gu");default:return F[r]}};function Cr(r,e){let t=0,n="";for(;e<r.length;e++){let s=r[e];switch(s){case"(":++t;break;case")":--t}if(n+=s,t===0)return n}return n}function Nr(r,e=F){if(!r)return[];let t=[r];for(let[s,a]of Object.entries(e))for(let i=0;i<t.length;i++){let o=t[i];if(typeof o!="string")continue;a.lastIndex=0;let c=a.exec(o);if(!c)continue;let p=c.index-1,l=[],h=c[0],S=o.slice(0,p+1);S&&l.push(S),l.push({...c.groups,type:s,content:h});let d=o.slice(p+h.length+1);d&&l.push(d),t.splice(i,1,...l)}let n=0;for(let s of t)switch(typeof s){case"string":throw new Error(`Unexpected sequence ${s} found at index ${n}`);case"object":n+=s.content.length,s.pos=[n-s.content.length,n],kr.has(s.type)&&(s.content=s.content.trim()||" ")}return t}var Rr=/(['"])([^\\\n]+?)\1/g,Mr=/\\./g;function $r(r,e=F){if((r=r.trim())==="")return[];let t=[];r=(r=r.replace(Mr,(a,i)=>(t.push({value:a,offset:i}),"\uE000".repeat(a.length)))).replace(Rr,(a,i,o,c)=>(t.push({value:a,offset:c}),`${i}${"\uE001".repeat(o.length)}${i}`));{let a,i=0;for(;(a=r.indexOf("(",i))>-1;){let o=Cr(r,a);t.push({value:o,offset:a}),r=`${r.substring(0,a)}(${"\xB6".repeat(o.length-2)})${r.substring(a+o.length)}`,i=a+o.length}}let n=Nr(r,e),s=new Set;for(let a of t.reverse())for(let i of n){let{offset:o,value:c}=a;if(!(i.pos[0]<=o&&o+c.length<=i.pos[1]))continue;let{content:p}=i,l=o-i.pos[0];i.content=p.slice(0,l)+c+p.slice(l+c.length),i.content!==p&&s.add(i)}for(let a of s){let i=Tr(a.type);if(!i)throw new Error(`Unknown token type: ${a.type}`);i.lastIndex=0;let o=i.exec(a.content);if(!o)throw new Error(`Unable to parse content for ${a.type}: ${a.content}`);Object.assign(a,o.groups)}return n}function L(r,{list:e=!0}={}){if(e&&r.find(t=>t.type==="comma")){let t=[],n=[];for(let s=0;s<r.length;s++)if(r[s].type==="comma"){if(n.length===0)throw new Error("Incorrect comma at "+s);t.push(L(n,{list:!1})),n.length=0}else n.push(r[s]);if(n.length===0)throw new Error("Trailing comma");return t.push(L(n,{list:!1})),{type:"list",list:t}}for(let t=r.length-1;t>=0;t--){let n=r[t];if(n.type==="combinator"){let s=r.slice(0,t),a=r.slice(t+1);return{type:"complex",combinator:n.content,left:L(s),right:L(a)}}}switch(r.length){case 0:throw new Error("Could not build AST.");case 1:return r[0];default:return{type:"compound",list:[...r]}}}function*D(r,e){switch(r.type){case"list":for(let t of r.list)yield*D(t,r);break;case"complex":yield*D(r.left,r),yield*D(r.right,r);break;case"compound":yield*r.list.map(t=>[t,r]);break;default:yield[r,e]}}function J(r,{recursive:e=!0,list:t=!0}={}){let n=$r(r);if(!n)return;let s=L(n,{list:t});if(!e)return s;for(let[a]of D(s)){if(a.type!=="pseudo-class"||!a.argument||!Ir.has(a.name))continue;let i=a.argument,o=Or[a.name];if(o){let c=o.exec(i);if(!c)continue;Object.assign(a,c.groups),i=c.groups.subtree}i&&Object.assign(a,{subtree:J(i,{recursive:!0,list:!0})})}return s}var K="comm",Y="rule",V="decl";var ir="@import";var cr="@keyframes";var ur="@layer";var Q=Math.abs,z=String.fromCharCode;function W(r){return r.trim()}function N(r,e,t){return r.replace(e,t)}function fr(r,e,t){return r.indexOf(e,t)}function T(r,e){return r.charCodeAt(e)|0}function A(r,e,t){return r
|
||
|
|
/**
|
||
|
|
* shorthash - https://github.com/bibig/node-shorthash
|
||
|
|
*
|
||
|
|
* @license
|
||
|
|
*
|
||
|
|
* (The MIT License)
|
||
|
|
*
|
||
|
|
* Copyright (c) 2013 Bibig <bibig@me.com>
|
||
|
|
*
|
||
|
|
* Permission is hereby granted, free of charge, to any person
|
||
|
|
* obtaining a copy of this software and associated documentation
|
||
|
|
* files (the "Software"), to deal in the Software without
|
||
|
|
* restriction, including without limitation the rights to use,
|
||
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
|
* copies of the Software, and to permit persons to whom the
|
||
|
|
* Software is furnished to do so, subject to the following
|
||
|
|
* conditions:
|
||
|
|
*
|
||
|
|
* The above copyright notice and this permission notice shall be
|
||
|
|
* included in all copies or substantial portions of the Software.
|
||
|
|
*
|
||
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||
|
|
*/
|