posthtml-parser
Version:
Parse HTML/XML to PostHTMLTree
2 lines (1 loc) • 2.95 kB
JavaScript
;var g=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var O=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var C=(r,n)=>{for(var c in n)g(r,c,{get:n[c],enumerable:!0})},E=(r,n,c,u)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of O(n))!j.call(r,s)&&s!==c&&g(r,s,{get:()=>n[s],enumerable:!(u=w(n,s))||u.enumerable});return r};var L=r=>E(g({},"__esModule",{value:!0}),r);var z={};C(z,{parser:()=>S});module.exports=L(z);var h=require("htmlparser2");var l=class{source;lastPosition;lastIndex;constructor(n){this.source=n,this.lastPosition={line:1,column:1},this.lastIndex=0}getPosition(n){if(n<this.lastIndex)throw new Error("Source indices must be monotonic");for(;this.lastIndex<n;)this.source.charCodeAt(this.lastIndex)===10?(this.lastPosition.line++,this.lastPosition.column=1):this.lastPosition.column++,this.lastIndex++;return{line:this.lastPosition.line,column:this.lastPosition.column}}};var D={lowerCaseTags:!1,lowerCaseAttributeNames:!1,decodeEntities:!1},R=[{name:"!doctype",start:"<",end:">"}],S=(r,n={})=>{let c=new l(r),u=[],s=[],b=0,f={};function d(){return u[u.length-1]}function y(o,t){return o.name instanceof RegExp?new RegExp(o.name.source,"i").test(t):t===o.name}function m(o){let t={};return Object.keys(o).forEach(e=>{let i={};i[e]=String(o[e]).replace(/"/g,'"'),n.recognizeNoValueAttribute&&f[e]&&(i[e]=!0),Object.assign(t,i)}),t}function A(o,t){let e=R.concat(n.directives??[]),i=d();for(let p of e){let x=p.start+t+p.end;if(y(p,o.toLowerCase())){if(i===void 0){s.push(x);return}typeof i=="object"&&(i.content===void 0&&(i.content=[]),Array.isArray(i.content)&&i.content.push(x))}}}function P(o){let t=d(),e=`<!--${o}-->`;if(t===void 0){s.push(e);return}typeof t=="object"&&(t.content===void 0&&(t.content=[]),Array.isArray(t.content)&&t.content.push(e))}function N(o,t,e){e===void 0&&(f[o]=!0)}function I(o,t){let e={tag:o};n.sourceLocations&&(e.location={start:c.getPosition(a.startIndex),end:c.getPosition(a.endIndex)},b=a.endIndex),Object.keys(t).length>0&&(e.attrs=m(t)),f={},u.push(e)}function v(o,t){let e=u.pop();if(e&&typeof e=="object"&&e.location&&a.endIndex!==null&&(t?b<a.startIndex&&(e.location.end=c.getPosition(a.startIndex-1)):e.location.end=c.getPosition(a.endIndex)),e){let i=d();if(u.length<=0){s.push(e);return}typeof i=="object"&&(i.content===void 0&&(i.content=[]),Array.isArray(i.content)&&i.content.push(e))}}function T(o){let t=d();if(t===void 0){s.push(o);return}if(typeof t=="object"){if(t.content&&Array.isArray(t.content)&&t.content.length>0){let e=t.content[t.content.length-1];if(typeof e=="string"&&!e.startsWith("<!--")){t.content[t.content.length-1]=`${e}${o}`;return}}t.content===void 0&&(t.content=[]),Array.isArray(t.content)&&t.content.push(o)}}let a=new h.Parser({onprocessinginstruction:A,oncomment:P,onattribute:N,onopentag:I,onclosetag:v,ontext:T},{...D,...n});return a.write(r),a.end(),s};0&&(module.exports={parser});