UNPKG

@claromentis/design-system

Version:

Claromentis Design System Component Library

12 lines 4.69 kB
/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license * * This file is a port of shadowCSS from webcomponents.js to TypeScript. * https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js * https://github.com/angular/angular/blob/master/packages/compiler/src/shadow_css.ts */ const t="-shadowcsshost",s="-shadowcssslotted",e="-shadowcsscontext",o=")(?:\\(((?:\\([^)(]*\\)|[^)(]*)+?)\\))?([^,{]*)",n=RegExp("("+t+o,"gim"),r=RegExp("("+e+o,"gim"),c=RegExp("("+s+o,"gim"),g=t+"-no-combinator",l=/-shadowcsshost-no-combinator([^\s]*)/,u=[/::shadow/g,/::content/g],i=/-shadowcsshost/gim,a=/:host/gim,d=/::slotted/gim,h=/:host-context/gim,p=/\/\*\s*[\s\S]*?\*\//g,m=/\/\*\s*#\s*source(Mapping)?URL=[\s\S]+?\*\//g,$=/(\s*)([^;\{\}]+?)(\s*)((?:{%BLOCK%}?\s*;?)|(?:\s*;))/g,_=/([{}])/g,f=/(^.*?[^\\])??((:+)(.*)|$)/,x="%BLOCK%",w=(t,s)=>{const e=S(t);let o=0;return e.escapedString.replace($,((...t)=>{const n=t[2];let r="",c=t[4],g="";c&&c.startsWith("{"+x)&&(r=e.blocks[o++],c=c.substring(8),g="{");const l=s({selector:n,content:r});return`${t[1]}${l.selector}${t[3]}${g}${l.content}${c}`}))},S=t=>{const s=t.split(_),e=[],o=[];let n=0,r=[];for(let c=0;c<s.length;c++){const t=s[c];"}"===t&&n--,n>0?r.push(t):(r.length>0&&(o.push(r.join("")),e.push(x),r=[]),e.push(t)),"{"===t&&n++}return r.length>0&&(o.push(r.join("")),e.push(x)),{escapedString:e.join(""),blocks:o}},b=(t,s,e)=>t.replace(s,((...t)=>{if(t[2]){const s=t[2].split(","),o=[];for(let n=0;n<s.length;n++){const r=s[n].trim();if(!r)break;o.push(e(g,r,t[3]))}return o.join(",")}return g+t[3]})),R=(s,e,o)=>s+e.replace(t,"")+o,E=(s,e,o)=>e.indexOf(t)>-1?R(s,e,o):s+e+o+", "+e+" "+s+o,k=(t,s)=>t.replace(f,((t,e="",o,n="",r="")=>e+s+n+r)),L=(t,s,e,o)=>w(t,(t=>{let n=t.selector,r=t.content;return"@"!==t.selector[0]?n=((t,s,e,o)=>t.split(",").map((t=>o&&t.indexOf("."+o)>-1?t.trim():((t,s)=>!(t=>(t=t.replace(/\[/g,"\\[").replace(/\]/g,"\\]"),RegExp("^("+t+")([>\\s~+[.,{:][\\s\\S]*)?$","m")))(s).test(t))(t,s)?((t,s,e)=>{const o="."+(s=s.replace(/\[is=([^\]]*)\]/g,((t,...s)=>s[0]))),n=t=>{let n=t.trim();if(!n)return"";if(t.indexOf(g)>-1)n=((t,s,e)=>{if(i.lastIndex=0,i.test(t)){const s="."+e;return t.replace(l,((t,e)=>k(e,s))).replace(i,s+" ")}return s+" "+t})(t,s,e);else{const s=t.replace(i,"");s.length>0&&(n=k(s,o))}return n},r=(t=>{const s=[];let e=0;return{content:(t=t.replace(/(\[[^\]]*\])/g,((t,o)=>{const n=`__ph-${e}__`;return s.push(o),e++,n}))).replace(/(:nth-[-\w]+)(\([^)]+\))/g,((t,o,n)=>{const r=`__ph-${e}__`;return s.push(n),e++,o+r})),placeholders:s}})(t);let c,u="",a=0;const d=/( |>|\+|~(?!=))\s*/g;let h=!((t=r.content).indexOf(g)>-1);for(;null!==(c=d.exec(t));){const s=c[1],e=t.slice(a,c.index).trim();h=h||e.indexOf(g)>-1,u+=`${h?n(e):e} ${s} `,a=d.lastIndex}const p=t.substring(a);return h=h||p.indexOf(g)>-1,u+=h?n(p):p,((t,s)=>s.replace(/__ph-(\d+)__/g,((s,e)=>t[+e])))(r.placeholders,u)})(t,s,e).trim():t.trim())).join(", "))(t.selector,s,e,o):(t.selector.startsWith("@media")||t.selector.startsWith("@supports")||t.selector.startsWith("@page")||t.selector.startsWith("@document"))&&(r=L(t.content,s,e,o)),{selector:n.replace(/\s{2,}/g," ").trim(),content:r}})),B=(o,l,i)=>{const $=l+"-h",_=l+"-s",f=(t=>t.match(m)||[])(o);o=(t=>t.replace(p,""))(o);const x=[];if(i){const t=t=>{const s=`/*!@___${x.length}___*/`;return x.push({placeholder:s,comment:`/*!@${t.selector}*/`}),t.selector=s+t.selector,t};o=w(o,(s=>"@"!==s.selector[0]?t(s):s.selector.startsWith("@media")||s.selector.startsWith("@supports")||s.selector.startsWith("@page")||s.selector.startsWith("@document")?(s.content=w(s.content,t),s):s))}const S=((o,l,i,p)=>{const m=((t,s)=>{const e="."+s+" > ",o=[];return t=t.replace(c,((...t)=>{if(t[2]){const s=t[2].trim(),n=e+s+t[3];let r="";for(let e=t[4]-1;e>=0;e--){const s=t[5][e];if("}"===s||","===s)break;r=s+r}const c=r+n,g=`${r.trimRight()}${n.trim()}`;return c.trim()!==g.trim()&&o.push({orgSelector:c,updatedSelector:`${g}, ${c}`}),n}return g+t[3]})),{selectors:o,cssText:t}})(o=(t=>b(t,r,E))(o=(t=>b(t,n,R))(o=(o=>o.replace(h,e).replace(a,t).replace(d,s))(o))),p);return o=(t=>u.reduce(((t,s)=>t.replace(s," ")),t))(o=m.cssText),l&&(o=L(o,l,i,p)),{cssText:(o=(o=o.replace(/-shadowcsshost-no-combinator/g,"."+i)).replace(/>\s*\*\s+([^{, ]+)/gm," $1 ")).trim(),slottedSelectors:m.selectors}})(o,l,$,_);return o=[S.cssText,...f].join("\n"),i&&x.forEach((({placeholder:t,comment:s})=>{o=o.replace(t,s)})),S.slottedSelectors.forEach((t=>{o=o.replace(t.orgSelector,t.updatedSelector)})),o};export{B as scopeCss}