-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemmet-compress.js
2 lines (2 loc) · 2.26 KB
/
emmet-compress.js
1
2
let t;const e=()=>t||(t=import("htmlparser2").then((({Parser:t})=>t)).catch((()=>class{constructor(t){this._buf="",this._config=t}write(t){this._buf+=t}end(){const t=(new DOMParser).parseFromString(this._buf,"text/xml"),e=t.getElementsByTagName("parsererror");if(0!=e.length)throw Object.assign(Error("XML Parse Error"),{errors:e});const s=t=>{for(const e of t){if(3===e.nodeType||4===e.nodeType){const t=e.nodeValue;t&&this._config.ontext(t)}if(1==e.nodeType){const t=e,i=t.nodeName,r={};for(const e of t.attributes)r[e.nodeName]=e.nodeValue||"";this._config.onopentag(i,r),s(t.childNodes),this._config.onclosetag(i)}}};s([t.documentElement])}})),t.catch((()=>{})),t);class s{constructor(){this._agg="",this._text="",this._hasChild=!1,this._deep=0,this._up=0}onopentag(t,e){if(console.log("onopentag",t,e),this._text.trim())throw Error("Cannot have mixed text nodes and element nodes: "+JSON.stringify(this._text));if(this._deep&&!this._up)this._agg+=">";else if(1==this._up)this._agg+="+",this._up=0;else for(;0!=this._up;)this._agg+="^",this._up-=1;this._agg+=t;const s=Object.keys(e||{});s.length&&(this._agg+=`[${s.map((t=>{const s=e[t];return""===s?t:`${t}=${JSON.stringify(s)}`})).join(" ")}]`),this._text="",this._hasChild=!1,this._up=0,this._deep+=1}ontext(t){if(this._hasChild){if(t.trim())throw Error("Cannot have mixed text nodes and element nodes: "+JSON.stringify(this._text+t))}else{if(this._text+=t,-1!=t.indexOf("}"))throw Error("Cannot have the character `}` in text: "+JSON.stringify(this._text));this._up=0}}onclosetag(t){!this._hasChild&&this._text&&(this._agg+=`{${this._text}}`),this._text="",this._hasChild=!0,this._up+=1,this._deep-=1}toString(){return this._agg}}class i{constructor(t){this._parseCons=t||e()}write(t){if(t)if(this._parser)this._parser.write(t);else{if(this._parseCons instanceof Promise)return this._parseCons.then((e=>{this._parser||(this._parseCons=e,this._walker=new s,this._parser=new e(this._walker)),this._parser.write(t)}));this._walker=new s,this._parser=new this._parseCons(this._walker),this._parser.write(t)}}end(){if(!this._parser)return"";this._parser.end();const t=this._walker.toString();return this._parser=void 0,this._walker=void 0,t}toString(){return this._walker?this._walker.toString():""}}export{i as default};
//# sourceMappingURL=emmet-compress.js.map