UNPKG

lifecss

Version:

Create style rules in situ

13 lines (12 loc) 8.08 kB
var{IS:N,maybe:J,$Wrap:K,xProxy:Q,isNothing:V}=C();function C(){Symbol.proxy=Symbol.for("toa.proxy"),Symbol.is=Symbol.for("toa.is"),Symbol.type=Symbol.for("toa.type"),Symbol.isSymbol=Symbol.for("toa.isASymbol"),l();let n=h(),[i,f]=[u(),p()];return f.custom(),{IS:s,maybe:n,$Wrap:i,isNothing:b,xProxy:f};function s(e,...t){if(n({trial:c=>"isTypes"in(t?.[0]??{})})){let c=t[0];return"defaultValue"in c?y(e,c):"notTypes"in c?r(e,c):s(e,...[c.isTypes].flat())}let o=typeof e=="symbol"?Symbol.isSymbol:e;return t.length>1?P(o,...t):$(e,...t)}function S(e){return e?.[Symbol.proxy]??s(e)}function $(e,...t){let{noInput:o,noShouldbe:c,compareTo:g,inputCTOR:R,isNaN:j,isInfinity:F,shouldBeFirstElementIsNothing:A}=O(e,...t);switch(t=t.length&&t[0],!0){case A:return String(e)===String(g);case(e?.[Symbol.proxy]&&c):return e[Symbol.proxy];case j:return c?"NaN":n({trial:E=>String(g)})===String(e);case F:return c?"Infinity":n({trial:E=>String(g)})===String(e);case o:return c?String(e):String(g)===String(e);case R===Boolean:return t?R===t:"Boolean";default:return d(e,t,c,x(e,R))}}function x(e,t){return e===0?Number:e===""?String:e?t:{name:String(e)}}function O(e,...t){let o=t.length<1,c=!o&&t[0],g=!o&&b(t[0]),R=e==null,j=!R&&Object.getPrototypeOf(e)?.constructor,F=n({trial:E=>String(e)})==="NaN",A=n({trial:E=>String(e)})==="Infinity";return{noInput:R,noShouldbe:o,compareTo:c,inputCTOR:j,isNaN:F,isInfinity:A,shouldBeFirstElementIsNothing:g}}function d(e,t,o,c){switch(!0){case(!o&&t===e||e?.[Symbol.proxy]&&t===Proxy):return!0;case n({trial:g=>String(t)})==="NaN":return String(e)==="NaN";case(e?.[Symbol.toStringTag]&&s(t,String)):return String(t)===e[Symbol.toStringTag];default:return t?n({trial:g=>e instanceof t})||t===c||t===Object.getPrototypeOf(c)||`${t?.name}`===c?.name:e?.[Symbol.toStringTag]&&`[object ${e?.[Symbol.toStringTag]}]`||c?.name||String(c)}}function P(e,...t){return t.some(o=>s(e,o))}function b(e,t=!1){let o=e==null;return o=t?o||s(e,1/0)||s(e,NaN):o,o}function h(){let e=(t,o)=>t?.constructor===Function?t(o):void 0;return function({trial:t,whenError:o=()=>{}}={}){try{return e(t)}catch(c){return e(o,c)}}}function u(){return function(e){return Object.freeze({get value(){return e},get[Symbol.type](){return S(e)},get type(){return S(e)},[Symbol.is](...t){return s(e,...t)},is(...t){return s(e,...t)}})}}function y(e,{defaultValue:t,isTypes:o=[void 0],notTypes:c}={}){return o=o?.constructor!==Array?[o]:o,c=c&&c?.constructor!==Array?[c]:[],c.length<1?s(e,...o)?e:t:r(e,{isTypes:o,notTypes:c})?e:t}function r(e,{isTypes:t=[void 0],notTypes:o=[void 0]}={}){return t=t?.constructor!==Array?[t]:t,o=o?.constructor!==Array?[o]:o,s(e,...t)&&!s(e,...o)}function l(){Object.getOwnPropertyDescriptors(Object.prototype)[Symbol.is]||(Object.defineProperties(Object.prototype,{[Symbol.type]:{get(){return S(this)},enumerable:!1,configurable:!1},[Symbol.is]:{value:function(...e){return s(this,...e)},enumerable:!1,configurable:!1}}),Object.defineProperties(Object,{[Symbol.type]:{value(e){return S(e)},enumerable:!1,configurable:!1},[Symbol.is]:{value:function(e,...t){return s(e,...t)},enumerable:!1,configurable:!1}}))}function a(e){let t=String(Object.getPrototypeOf(e)?.constructor);return t.slice(t.indexOf("ion")+3,t.indexOf("(")).trim()}function m(e){let t=e.set;return e.set=(o,c,g)=>c===Symbol.proxy?o[c]=g:t(o,c,g),e}function p(){let e=Proxy;return{native(){Proxy=e},custom(){Proxy=new e(e,{construct(t,o){for(let g of o)g.set&&(g=m(g));let c=new t(...o);return c[Symbol.proxy]=`Proxy (${a(o[0])})`,c}})}}}}var M=k;function k({styleSheet:n,createWithId:i}={}){let{tryParseAtOrNestedRules:f,ruleExists:s,checkParams:S,sheet:$,removeRules:x,consider:O,currentSheetID:d}=L({styleSheet:n,createWithId:i});function P(r,l){if(r&&l.removeProperties){Object.keys(l.removeProperties).forEach(a=>r.style.removeProperty(I(a)));return}Object.entries(l).forEach(([a,m])=>{a=I(a.trim()),m=m.trim();let p;if(/!important/.test(m)&&(m=m.slice(0,m.indexOf("!important")).trim(),p="important"),!CSS.supports(a,m))return console.error(`StylingFactory ${d} error: '${a}' with value '${m}' not supported (yet)`);v(()=>r.style.setProperty(a,m,p),`StylingFactory ${d} (setRule4Selector) failed`)})}function b(r,l,a=$){if(r=r?.trim?.(),!N(r,String)||!r.length||/[;,]$/g.test(r))return console.error(`StylingFactory ${d} (setRules): [${r||"[no selector given]"}] is not a valid selector`);if(l.removeRule)return x(r);let m=s(r,!0),p=m||a.cssRules[a.insertRule(`${r} {}`,a.cssRules.length||0)];return O(()=>P(p,l),r,m)}function h(r){let l=r.trim().split(/{/,2),a=l.shift().trim();if(!N(a,String)||!a?.trim()?.length)return console.error(`StylingFactory ${d} (doParse): no (valid) selector could be extracted from rule ${w(r)}`);let m=G(l.shift());return v(()=>b(a,m),`StylingFactory ${d} (setRules) failed`)}function u(r){let l=f(r);return l.done?l.existing:h(r)}function y(r,l){return r.trim().startsWith("@media")?u(T(r,l)):b(r,l)}return function(r,l={}){return S(r,l)&&(Object.keys(l).length?y(r,l):u(r))}}function L({styleSheet:n,createWithId:i}){let f="Note: The rule or some of its properties may not be supported by your browser (yet)",s=`for style#${i}`;n=i?S(i):n;function S(u){let y=document.querySelector(`#${u}`)?.sheet;if(y)return y;let r=Object.assign(document.createElement("style"),{id:u});return document.head.insertAdjacentElement("beforeend",r),r.sheet}function $(u){return console.error(`StylingFactory ${s} [rule: ${u}] => @charset, @namespace and @import are not supported here`),{done:!0}}function x(u,y){return[...n.rules].find(r=>y?_(r.selectorText||"",u):H`${z(u)}${[..."gim"]}`.test(r.cssText))}function O(u){return/^@charset|@import|namespace/i.test(u.trim())?$(u):u.match(/}/g)?.length>1?{existing:b(u,1),done:!0}:{done:!1}}function d(u){let y=[...n.cssRules].reduce((l,a,m)=>_(a.selectorText||"",u)&&l.concat(m)||l,[]),r=y.length;return y.forEach(l=>n.deleteRule(l)),r>0?console.info(`\u2714 Removed ${r} instance${r>1?"s":""} of selector ${u} from ${s.slice(4)}`):console.info(`\u2714 Remove rule: selector ${u} does not exist in ${s.slice(4)}`)}function P(u,y){return u&&N(u,String)&&u.trim().length>0&&N(y,Object)||(console.error(`StylingFactory ${s} called with invalid parameters`),!1)}function b(u){u=u.trim();let y=u.slice(0,u.indexOf("{")).trim(),r=!!x(y);try{return n.insertRule(`${u}`,n.cssRules.length),r}catch(l){return console.error(`StylingFactory ${s} (tryParse) ${l.name} Error: ${l.message} Rule: ${w(u)} ${f}`),r}}function h(u,y,r){try{return u(),r}catch(l){return console.error(`StylingFactory ${s} (tryAddOrModify) ${l.name} Error: ${l.message} Rule: ${w(y)} ${f}`),r}}return{sheet:n,removeRules:d,tryParseAtOrNestedRules:O,ruleExists:x,checkParams:P,tryParse:b,consider:h,currentSheetID:s}}function T(n,i){return`${n.trim()} ${Object.entries(i).map(([f,s])=>`${f}: { ${q(s)}`)}`}function w(n){let i=(n||"NO RULE").trim().slice(0,50).replace(/\n/g,"\\n").replace(/\s{2,}/g," ");return n.length>i.length?`${i.trim()}...truncated`:i}function q(n){return Object.entries(n).map(([i,f])=>`${i}: ${f.trim()}`).join(`; `)}function z(n){return n.replace(/([*\[\]()-+{}.$?\\])/g,i=>`\\${i}`)}function H(n,...i){let f=i.length&&Array.isArray(i.slice(-1))?i.pop().join(""):"";return new RegExp((i.length&&n.raw.reduce((s,S,$)=>s.concat(i[$-1]||"").concat(S),"")||n.raw.join("")).split(` `).map(s=>s.replace(/\s|\/\/.*$/g,"").trim().replace(/(@s!)/g," ")).join(""),f)}function I(n){return n.replace(/[A-Z]/g,i=>`-${i.toLowerCase()}`).replace(/[^--]^-|-$/,"")}function v(n,i){try{return n()}catch(f){console.error(`${i||"an error occured"}: ${f.message}`)}}function U(n){return n.replace(/\/\*.+?\*\//gm,"").replace(/[}{\r\n]/g,"").replace(/(data:.+?);/g,(i,f)=>`${f}\\3b`).split(";").map(i=>i.trim()).join(`; `).replaceAll("\\3b",";").split(` `)}function Z(n){return n.reduce((i,f)=>{let[s,S]=[f.slice(0,f.indexOf(":")).trim(),f.slice(f.indexOf(":")+1).trim().replace(/;$|;.+(?=\/*).+\/$/,"")];return s&&S?{...i,[s]:S}:i},{})}function G(n){return Z(U(n))}function _(n,i){return n?.replace("::",":")===i?.replace("::",":")}export{M as default};