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