UNPKG

@stories-js/core

Version:

Stories web components to build stories

16 lines 5.56 kB
/*! * (C) StoriesJS https://storiesjs.org - GPL-2.0 License */ /** * @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=t=>{const s=[];let n=0;let o;t=t.replace(/(\[[^\]]*\])/g,((t,o)=>{const c=`__ph-${n}__`;s.push(o);n++;return c}));o=t.replace(/(:nth-[-\w]+)(\([^)]+\))/g,((t,o,c)=>{const e=`__ph-${n}__`;s.push(c);n++;return o+e}));const c={content:o,placeholders:s};return c};const s=(t,s)=>s.replace(/__ph-(\d+)__/g,((s,n)=>t[+n]));const n="-shadowcsshost";const o="-shadowcssslotted";const c="-shadowcsscontext";const e=")(?:\\(("+"(?:\\([^)(]*\\)|[^)(]*)+?"+")\\))?([^,{]*)";const r=new RegExp("("+n+e,"gim");const l=new RegExp("("+c+e,"gim");const i=new RegExp("("+o+e,"gim");const u=n+"-no-combinator";const g=/-shadowcsshost-no-combinator([^\s]*)/;const f=[/::shadow/g,/::content/g];const a="([>\\s~+[.,{:][\\s\\S]*)?$";const h=/-shadowcsshost/gim;const d=/:host/gim;const p=/::slotted/gim;const $=/:host-context/gim;const m=/\/\*\s*[\s\S]*?\*\//g;const _=t=>t.replace(m,"");const w=/\/\*\s*#\s*source(Mapping)?URL=[\s\S]+?\*\//g;const x=t=>t.match(w)||[];const S=/(\s*)([^;\{\}]+?)(\s*)((?:{%BLOCK%}?\s*;?)|(?:\s*;))/g;const b=/([{}])/g;const R=/(^.*?[^\\])??((:+)(.*)|$)/;const E="{";const k="}";const L="%BLOCK%";const B=(t,s)=>{const n=C(t);let o=0;return n.escapedString.replace(S,((...t)=>{const c=t[2];let e="";let r=t[4];let l="";if(r&&r.startsWith("{"+L)){e=n.blocks[o++];r=r.substring(L.length+1);l="{"}const i={selector:c,content:e};const u=s(i);return`${t[1]}${u.selector}${t[3]}${l}${u.content}${r}`}))};const C=t=>{const s=t.split(b);const n=[];const o=[];let c=0;let e=[];for(let l=0;l<s.length;l++){const t=s[l];if(t===k){c--}if(c>0){e.push(t)}else{if(e.length>0){o.push(e.join(""));n.push(L);e=[]}n.push(t)}if(t===E){c++}}if(e.length>0){o.push(e.join(""));n.push(L)}const r={escapedString:n.join(""),blocks:o};return r};const K=t=>{t=t.replace($,c).replace(d,n).replace(p,o);return t};const O=(t,s,n)=>t.replace(s,((...t)=>{if(t[2]){const s=t[2].split(",");const o=[];for(let c=0;c<s.length;c++){const e=s[c].trim();if(!e)break;o.push(n(u,e,t[3]))}return o.join(",")}else{return u+t[3]}}));const T=(t,s,o)=>t+s.replace(n,"")+o;const M=t=>O(t,r,T);const U=(t,s,o)=>{if(s.indexOf(n)>-1){return T(t,s,o)}else{return t+s+o+", "+s+" "+t+o}};const j=(t,s)=>{const n="."+s+" > ";const o=[];t=t.replace(i,((...t)=>{if(t[2]){const s=t[2].trim();const c=t[3];const e=n+s+c;let r="";for(let n=t[4]-1;n>=0;n--){const s=t[5][n];if(s==="}"||s===","){break}r=s+r}const l=r+e;const i=`${r.trimRight()}${e.trim()}`;if(l.trim()!==i.trim()){const t=`${i}, ${l}`;o.push({orgSelector:l,updatedSelector:t})}return e}else{return u+t[3]}}));return{selectors:o,cssText:t}};const q=t=>O(t,l,U);const v=t=>f.reduce(((t,s)=>t.replace(s," ")),t);const y=t=>{const s=/\[/g;const n=/\]/g;t=t.replace(s,"\\[").replace(n,"\\]");return new RegExp("^("+t+")"+a,"m")};const z=(t,s)=>{const n=y(s);return!n.test(t)};const A=(t,s)=>t.replace(R,((t,n="",o,c="",e="")=>n+s+c+e));const D=(t,s,n)=>{h.lastIndex=0;if(h.test(t)){const s=`.${n}`;return t.replace(g,((t,n)=>A(n,s))).replace(h,s+" ")}return s+" "+t};const F=(n,o,c)=>{const e=/\[is=([^\]]*)\]/g;o=o.replace(e,((t,...s)=>s[0]));const r="."+o;const l=t=>{let s=t.trim();if(!s){return""}if(t.indexOf(u)>-1){s=D(t,o,c)}else{const n=t.replace(h,"");if(n.length>0){s=A(n,r)}}return s};const i=t(n);n=i.content;let g="";let f=0;let a;const d=/( |>|\+|~(?!=))\s*/g;const p=n.indexOf(u)>-1;let $=!p;while((a=d.exec(n))!==null){const t=a[1];const s=n.slice(f,a.index).trim();$=$||s.indexOf(u)>-1;const o=$?l(s):s;g+=`${o} ${t} `;f=d.lastIndex}const m=n.substring(f);$=$||m.indexOf(u)>-1;g+=$?l(m):m;return s(i.placeholders,g)};const G=(t,s,n,o)=>t.split(",").map((t=>{if(o&&t.indexOf("."+o)>-1){return t.trim()}if(z(t,s)){return F(t,s,n).trim()}else{return t.trim()}})).join(", ");const H=(t,s,n,o,c)=>B(t,(t=>{let c=t.selector;let e=t.content;if(t.selector[0]!=="@"){c=G(t.selector,s,n,o)}else if(t.selector.startsWith("@media")||t.selector.startsWith("@supports")||t.selector.startsWith("@page")||t.selector.startsWith("@document")){e=H(t.content,s,n,o)}const r={selector:c.replace(/\s{2,}/g," ").trim(),content:e};return r}));const I=(t,s,n,o,c)=>{t=K(t);t=M(t);t=q(t);const e=j(t,o);t=e.cssText;t=v(t);if(s){t=H(t,s,n,o)}t=t.replace(/-shadowcsshost-no-combinator/g,`.${n}`);t=t.replace(/>\s*\*\s+([^{, ]+)/gm," $1 ");return{cssText:t.trim(),slottedSelectors:e.selectors}};const J=(t,s,n)=>{const o=s+"-h";const c=s+"-s";const e=x(t);t=_(t);const r=[];if(n){const s=t=>{const s=`/*!@___${r.length}___*/`;const n=`/*!@${t.selector}*/`;r.push({placeholder:s,comment:n});t.selector=s+t.selector;return t};t=B(t,(t=>{if(t.selector[0]!=="@"){return s(t)}else if(t.selector.startsWith("@media")||t.selector.startsWith("@supports")||t.selector.startsWith("@page")||t.selector.startsWith("@document")){t.content=B(t.content,s);return t}return t}))}const l=I(t,s,o,c);t=[l.cssText,...e].join("\n");if(n){r.forEach((({placeholder:s,comment:n})=>{t=t.replace(s,n)}))}l.slottedSelectors.forEach((s=>{t=t.replace(s.orgSelector,s.updatedSelector)}));return t};export{J as scopeCss}; //# sourceMappingURL=p-23b4efe0.js.map