@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 13.8 kB
JavaScript
import*as TextUtils from"../../models/text_utils/text_utils.js";import{cssMetadata,VariableRegex}from"./CSSMetadata.js";import{CSSKeyframesRule,CSSPositionFallbackRule,CSSPropertyRule,CSSStyleRule}from"./CSSRule.js";import{CSSStyleDeclaration,Type}from"./CSSStyleDeclaration.js";export function parseCSSVariableNameAndFallback(e){const t=e.match(/var\(\s*(--(?:[\s\w\P{ASCII}-]|\\.)+),?\s*(.*)\s*\)/u);return{variableName:t&&t[1].trim(),fallback:t&&t[2]}}export class CSSRegisteredProperty{#e;#t;#s;constructor(e,t){this.#t=e,this.#e=t}isAtProperty(){return this.#e instanceof CSSPropertyRule}propertyName(){return this.#e instanceof CSSPropertyRule?this.#e.propertyName().text:this.#e.propertyName}initialValue(){return this.#e instanceof CSSPropertyRule?this.#e.initialValue():this.#e.initialValue?.text??null}inherits(){return this.#e instanceof CSSPropertyRule?this.#e.inherits():this.#e.inherits}syntax(){return this.#e instanceof CSSPropertyRule?this.#e.syntax():`"${this.#e.syntax}"`}#r(){if(this.#e instanceof CSSPropertyRule)return[];const{inherits:e,initialValue:t,syntax:s}=this.#e,r=[{name:"inherits",value:`${e}`},{name:"syntax",value:`"${s}"`}];return void 0!==t&&r.push({name:"initial-value",value:t.text}),r}style(){return this.#s||(this.#s=this.#e instanceof CSSPropertyRule?this.#e.style:new CSSStyleDeclaration(this.#t,null,{cssProperties:this.#r(),shorthandEntries:[]},Type.Pseudo)),this.#s}}export class CSSMatchedStyles{#i;#a;#n;#o;#l;#c;#h=new Map;#d;#u;#p;#S;#y;#g;#f;#m;constructor({cssModel:e,node:t,inlinePayload:s,attributesPayload:r,matchedPayload:i,pseudoPayload:a,inheritedPayload:n,inheritedPseudoPayload:o,animationsPayload:l,parentLayoutNodeId:c,positionFallbackRules:h,propertyRules:d,cssPropertyRegistrations:u}){this.#i=e,this.#a=t,this.#n=new Map,this.#o=new Map,this.#c=[...d.map((t=>new CSSPropertyRule(e,t))),...u].map((t=>new CSSRegisteredProperty(e,t))),this.#l=[],l&&(this.#l=l.map((t=>new CSSKeyframesRule(e,t)))),this.#m=h.map((t=>new CSSPositionFallbackRule(e,t))),this.#f=c,this.#d=new Map,this.#u=new Set,i=p(i);for(const e of n)e.matchedCSSRules=p(e.matchedCSSRules);this.#p=this.buildMainCascade(s,r,i,n),[this.#S,this.#y]=this.buildPseudoCascades(a,o),this.#g=new Map;for(const e of Array.from(this.#y.values()).concat(Array.from(this.#S.values())).concat(this.#p))for(const t of e.styles())this.#g.set(t,e);for(const e of this.#c)this.#h.set(e.propertyName(),e);function p(e){for(const t of e)i(t);const t=[];for(const i of e){const e=t[t.length-1];e&&"user-agent"===i.rule.origin&&"user-agent"===e.rule.origin&&i.rule.selectorList.text===e.rule.selectorList.text&&r(i)===r(e)?s(i,e):t.push(i)}return t;function s(e,t){const s=new Map,r=new Map;for(const e of t.rule.style.shorthandEntries)s.set(e.name,e.value);for(const e of t.rule.style.cssProperties)r.set(e.name,e.value);for(const t of e.rule.style.shorthandEntries)s.set(t.name,t.value);for(const t of e.rule.style.cssProperties)r.set(t.name,t.value);t.rule.style.shorthandEntries=[...s.entries()].map((([e,t])=>({name:e,value:t}))),t.rule.style.cssProperties=[...r.entries()].map((([e,t])=>({name:e,value:t})))}function r(e){return e.rule.media?e.rule.media.map((e=>e.text)).join(", "):null}function i(e){const{matchingSelectors:t,rule:s}=e;"user-agent"===s.origin&&t.length&&(s.selectorList.selectors=s.selectorList.selectors.filter(((e,s)=>t.includes(s))),s.selectorList.text=s.selectorList.selectors.map((e=>e.text)).join(", "),e.matchingSelectors=t.map(((e,t)=>t)))}}}buildMainCascade(e,t,s,r){const i=[],a=[];function n(){if(!t)return;const e=new CSSStyleDeclaration(this.#i,null,t,Type.Attributes);this.#d.set(e,this.#a),a.push(e)}if(e&&this.#a.nodeType()===Node.ELEMENT_NODE){const t=new CSSStyleDeclaration(this.#i,null,e,Type.Inline);this.#d.set(t,this.#a),a.push(t)}let o;for(let e=s.length-1;e>=0;--e){const t=new CSSStyleRule(this.#i,s[e].rule);!t.isInjected()&&!t.isUserAgent()||o||(o=!0,n.call(this)),this.#d.set(t.style,this.#a),a.push(t.style),this.addMatchingSelectors(this.#a,t,s[e].matchingSelectors)}o||n.call(this),i.push(new NodeCascade(this,a,!1));let l=this.#a.parentNode;for(let e=0;l&&r&&e<r.length;++e){const t=[],s=r[e],n=s.inlineStyle?new CSSStyleDeclaration(this.#i,null,s.inlineStyle,Type.Inline):null;n&&this.containsInherited(n)&&(this.#d.set(n,l),t.push(n),this.#u.add(n));const o=s.matchedCSSRules||[];for(let e=o.length-1;e>=0;--e){const s=new CSSStyleRule(this.#i,o[e].rule);this.addMatchingSelectors(l,s,o[e].matchingSelectors),this.containsInherited(s.style)&&(c(a,s.style)||c(this.#u,s.style)||(this.#d.set(s.style,l),t.push(s.style),this.#u.add(s.style)))}l=l.parentNode,i.push(new NodeCascade(this,t,!0))}return new DOMInheritanceCascade(i,this.#c);function c(e,t){if(!t.styleSheetId||!t.range)return!1;for(const s of e)if(t.styleSheetId===s.styleSheetId&&s.range&&t.range.equal(s.range))return!0;return!1}}buildSplitCustomHighlightCascades(e,t,s,r){const i=new Map;for(let r=e.length-1;r>=0;--r){const a=this.customHighlightNamesToMatchingSelectorIndices(e[r]);for(const[n,o]of a){const a=new CSSStyleRule(this.#i,e[r].rule);this.#d.set(a.style,t),s&&this.#u.add(a.style),this.addMatchingSelectors(t,a,o);const l=i.get(n);l?l.push(a.style):i.set(n,[a.style])}}for(const[e,t]of i){const i=new NodeCascade(this,t,s,!0),a=r.get(e);a?a.push(i):r.set(e,[i])}}customHighlightNamesToMatchingSelectorIndices(e){const t=new Map;for(let s=0;s<e.matchingSelectors.length;s++){const r=e.matchingSelectors[s],i=e.rule.selectorList.selectors[r].text.match(/::highlight\((.*)\)/);if(i){const e=i[1],s=t.get(e);s?s.push(r):t.set(e,[r])}}return t}buildPseudoCascades(e,t){const s=new Map,r=new Map;if(!e)return[s,r];const i=new Map,a=new Map;for(let t=0;t<e.length;++t){const s=e[t],r=this.#a.pseudoElements().get(s.pseudoType)?.at(-1)||null,n=[],o=s.matches||[];if("highlight"===s.pseudoType)this.buildSplitCustomHighlightCascades(o,this.#a,!1,a);else{for(let e=o.length-1;e>=0;--e){const t=new CSSStyleRule(this.#i,o[e].rule);n.push(t.style);const i=cssMetadata().isHighlightPseudoType(s.pseudoType)?this.#a:r;this.#d.set(t.style,i),i&&this.addMatchingSelectors(i,t,o[e].matchingSelectors)}const e=cssMetadata().isHighlightPseudoType(s.pseudoType),t=new NodeCascade(this,n,!1,e);i.set(s.pseudoType,[t])}}if(t){let e=this.#a.parentNode;for(let s=0;e&&s<t.length;++s){const r=t[s].pseudoElements;for(let t=0;t<r.length;++t){const s=r[t],n=s.matches||[];if("highlight"===s.pseudoType)this.buildSplitCustomHighlightCascades(n,e,!0,a);else{const t=[];for(let s=n.length-1;s>=0;--s){const r=new CSSStyleRule(this.#i,n[s].rule);t.push(r.style),this.#d.set(r.style,e),this.#u.add(r.style),this.addMatchingSelectors(e,r,n[s].matchingSelectors)}const r=cssMetadata().isHighlightPseudoType(s.pseudoType),a=new NodeCascade(this,t,!0,r),o=i.get(s.pseudoType);o?o.push(a):i.set(s.pseudoType,[a])}}e=e.parentNode}}for(const[e,t]of i.entries())s.set(e,new DOMInheritanceCascade(t,this.#c));for(const[e,t]of a.entries())r.set(e,new DOMInheritanceCascade(t,this.#c));return[s,r]}addMatchingSelectors(e,t,s){for(const r of s){const s=t.selectors[r];s&&this.setSelectorMatches(e,s.text,!0)}}node(){return this.#a}cssModel(){return this.#i}hasMatchingSelectors(e){return this.getMatchingSelectors(e).length>0&&this.queryMatches(e.style)}getParentLayoutNodeId(){return this.#f}getMatchingSelectors(e){const t=this.nodeForStyle(e.style);if(!t||"number"!=typeof t.id)return[];const s=this.#o.get(t.id);if(!s)return[];const r=[];for(let t=0;t<e.selectors.length;++t)s.get(e.selectors[t].text)&&r.push(t);return r}async recomputeMatchingSelectors(e){const t=this.nodeForStyle(e.style);if(!t)return;const s=[];for(const i of e.selectors)s.push(r.call(this,t,i.text));async function r(e,t){const s=e.ownerDocument;if(!s)return;if("number"==typeof e.id){const s=this.#o.get(e.id);if(s&&s.has(t))return}if("number"!=typeof s.id)return;const r=await this.#a.domModel().querySelectorAll(s.id,t);r&&("number"==typeof e.id?this.setSelectorMatches(e,t,-1!==r.indexOf(e.id)):this.setSelectorMatches(e,t,!1))}await Promise.all(s)}addNewRule(e,t){return this.#n.set(e.style,t),this.recomputeMatchingSelectors(e)}setSelectorMatches(e,t,s){if("number"!=typeof e.id)return;let r=this.#o.get(e.id);r||(r=new Map,this.#o.set(e.id,r)),r.set(t,s)}queryMatches(e){if(!e.parentRule)return!0;const t=e.parentRule,s=[...t.media,...t.containerQueries,...t.supports,...t.scopes];for(const e of s)if(!e.active())return!1;return!0}nodeStyles(){return this.#p.styles()}registeredProperties(){return this.#c}getRegisteredProperty(e){return this.#h.get(e)}keyframes(){return this.#l}positionFallbackRules(){return this.#m}pseudoStyles(e){const t=this.#S.get(e);return t?t.styles():[]}pseudoTypes(){return new Set(this.#S.keys())}customHighlightPseudoStyles(e){const t=this.#y.get(e);return t?t.styles():[]}customHighlightPseudoNames(){return new Set(this.#y.keys())}containsInherited(e){const t=e.allProperties();for(let e=0;e<t.length;++e){const s=t[e];if(s.activeInStyle()&&cssMetadata().isPropertyInherited(s.name))return!0}return!1}nodeForStyle(e){return this.#n.get(e)||this.#d.get(e)||null}availableCSSVariables(e){const t=this.#g.get(e)||null;return t?t.findAvailableCSSVariables(e):[]}computeCSSVariable(e,t){const s=this.#g.get(e)||null;return s?s.computeCSSVariable(e,t):null}computeValue(e,t){const s=this.#g.get(e)||null;return s?s.computeValue(e,t):null}computeSingleVariableValue(e,t){const s=this.#g.get(e)||null;return s?s.computeSingleVariableValue(e,t):null}isInherited(e){return this.#u.has(e)}propertyState(e){const t=this.#g.get(e.ownerStyle);return t?t.propertyState(e):null}resetActiveProperties(){this.#p.reset();for(const e of this.#S.values())e.reset();for(const e of this.#y.values())e.reset()}}class NodeCascade{#C;styles;#M;#P;propertiesState;activeProperties;constructor(e,t,s,r=!1){this.#C=e,this.styles=t,this.#M=s,this.#P=r,this.propertiesState=new Map,this.activeProperties=new Map}computeActiveProperties(){this.propertiesState.clear(),this.activeProperties.clear();for(let e=this.styles.length-1;e>=0;e--){const t=this.styles[e],s=t.parentRule;if((!s||s instanceof CSSStyleRule)&&(!s||this.#C.hasMatchingSelectors(s)))for(const e of t.allProperties()){const s=cssMetadata();if(this.#M&&!this.#P&&!s.isPropertyInherited(e.name))continue;if(t.range&&!e.range)continue;if(!e.activeInStyle()){this.propertiesState.set(e,PropertyState.Overloaded);continue}const r=s.canonicalPropertyName(e.name);this.updatePropertyState(e,r);for(const t of e.getLonghandProperties())s.isCSSPropertyName(t.name)&&this.updatePropertyState(t,t.name)}}}updatePropertyState(e,t){const s=this.activeProperties.get(t);!s?.important||e.important?(s&&this.propertiesState.set(s,PropertyState.Overloaded),this.propertiesState.set(e,PropertyState.Active),this.activeProperties.set(t,e)):this.propertiesState.set(e,PropertyState.Overloaded)}}class DOMInheritanceCascade{#I;#b;#v;#w;#V;#N;#c;constructor(e,t){this.#I=e,this.#b=new Map,this.#v=new Map,this.#w=new Map,this.#V=!1,this.#c=t,this.#N=new Map;for(const t of e)for(const e of t.styles)this.#N.set(e,t)}findAvailableCSSVariables(e){const t=this.#N.get(e);if(!t)return[];this.ensureInitialized();const s=this.#v.get(t);return s?Array.from(s.keys()):[]}computeCSSVariable(e,t){const s=this.#N.get(e);if(!s)return null;this.ensureInitialized();const r=this.#v.get(s),i=this.#w.get(s);return r&&i?this.innerComputeCSSVariable(r,i,t):null}computeValue(e,t){const s=this.#N.get(e);if(!s)return null;this.ensureInitialized();const r=this.#v.get(s),i=this.#w.get(s);return r&&i?this.innerComputeValue(r,i,t):null}computeSingleVariableValue(e,t){const s=this.#N.get(e);if(!s)return null;this.ensureInitialized();const r=this.#v.get(s),i=this.#w.get(s);if(!r||!i)return null;const a=this.innerComputeValue(r,i,t),{variableName:n}=parseCSSVariableNameAndFallback(t);return{computedValue:a,fromFallback:null!==n&&!r.has(n)}}innerComputeCSSVariable(e,t,s){if(!e.has(s))return null;if(t.has(s))return t.get(s)||null;t.set(s,null);const r=e.get(s);if(null==r)return null;const i=this.innerComputeValue(e,t,r);return t.set(s,i),i}innerComputeValue(e,t,s){const r=TextUtils.TextUtils.Utils.splitStringByRegexes(s,[VariableRegex]),i=[];for(const s of r){if(-1===s.regexIndex){i.push(s.value);continue}const{variableName:r,fallback:a}=parseCSSVariableNameAndFallback(s.value);if(!r)return null;const n=this.innerComputeCSSVariable(e,t,r);if(null===n&&!a)return null;null===n?i.push(a):i.push(n)}return i.map((e=>e?e.trim():"")).join(" ")}styles(){return Array.from(this.#N.keys())}propertyState(e){return this.ensureInitialized(),this.#b.get(e)||null}reset(){this.#V=!1,this.#b.clear(),this.#v.clear(),this.#w.clear()}ensureInitialized(){if(this.#V)return;this.#V=!0;const e=new Map;for(const t of this.#I){t.computeActiveProperties();for(const[s,r]of t.propertiesState){if(r===PropertyState.Overloaded){this.#b.set(s,PropertyState.Overloaded);continue}const t=cssMetadata().canonicalPropertyName(s.name);e.has(t)?this.#b.set(s,PropertyState.Overloaded):(e.set(t,s),this.#b.set(s,PropertyState.Active))}}for(const[t,s]of e){const r=s.ownerStyle,i=s.getLonghandProperties();if(!i.length)continue;let a=!1;for(const t of i){const s=cssMetadata().canonicalPropertyName(t.name),i=e.get(s);if(i&&i.ownerStyle===r){a=!0;break}}a||(e.delete(t),this.#b.set(s,PropertyState.Overloaded))}const t=new Map;this.#c.forEach((e=>t.set(e.propertyName(),e.initialValue())));for(let e=this.#I.length-1;e>=0;--e){const s=this.#I[e],r=[];for(const e of s.activeProperties.entries()){const s=e[0],i=e[1];s.startsWith("--")&&(t.set(s,i.value),r.push(s))}const i=new Map(t),a=new Map;this.#v.set(s,i),this.#w.set(s,a);for(const e of r)t.delete(e),t.set(e,this.innerComputeCSSVariable(i,a,e))}}}export var PropertyState;!function(e){e.Active="Active",e.Overloaded="Overloaded"}(PropertyState||(PropertyState={}));