You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 lines
1.3 KiB

2 years ago
import e from"postcss-selector-parser";function s(n){if(!n)return{a:0,b:0,c:0};let t=0,c=0,o=0;if("universal"==n.type)return{a:0,b:0,c:0};if("id"===n.type)t+=1;else if("tag"===n.type)o+=1;else if("class"===n.type)c+=1;else if("attribute"===n.type)c+=1;else if(function(s){return e.isPseudoElement(s)}(n))o+=1;else if(e.isPseudoClass(n))switch(n.value){case":-moz-any":case":-webkit-any":case":any":case":has":case":is":case":matches":case":not":if(n.nodes&&n.nodes.length>0){const e=a(n.nodes);t+=e.a,c+=e.b,o+=e.c}break;case":where":break;case":nth-child":case":nth-last-child":if(c+=1,n.nodes&&n.nodes.length>0){const s=n.nodes[0].nodes.findIndex((e=>"tag"===e.type&&"of"===e.value));if(s>-1){const l=[e.selector({nodes:n.nodes[0].nodes.slice(s+1),value:""})];n.nodes.length>1&&l.push(...n.nodes.slice(1));const i=a(l);t+=i.a,c+=i.b,o+=i.c}}break;case":local":case":global":n.nodes&&n.nodes.length>0&&n.nodes.forEach((e=>{const a=s(e);t+=a.a,c+=a.b,o+=a.c}));break;default:c+=1}else e.isContainer(n)&&n.nodes.length>0&&n.nodes.forEach((e=>{const a=s(e);t+=a.a,c+=a.b,o+=a.c}));return{a:t,b:c,c:o}}function a(e){let a={a:0,b:0,c:0};return e.forEach((e=>{const n=s(e);n.a>a.a?a=n:n.a<a.a||(n.b>a.b?a=n:n.b<a.b||n.c>a.c&&(a=n))})),a}function n(e,s){return e.a===s.a?e.b===s.b?e.c-s.c:e.b-s.b:e.a-s.a}export{n as compare,s as selectorSpecificity};