🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

mini-program-parser

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mini-program-parser - npm Package Compare versions

Comparing version
0.3.10
to
0.3.11
+1
-1
dist/index.js

@@ -1,1 +0,1 @@

const t={containerWidth:0},e="area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr".split(","),s="area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr".split(","),n="address,article,aside,blockquote,dd,div,dl,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,ol,p,pre,section,table,ul".split(","),i=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,r=/^<\/([-A-Za-z0-9_]+)[^>]*>/,o=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/)>/,l=/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,a=[[/<\?xml.*\?>\n/,""],[/<.*!doctype.*\>\n/,""],[/<.*!DOCTYPE.*\>\n/,""],[/<!--.*?-->/gi,""],[/\/\*.*?\*\//gi,""]],c=/(\d*\.?\d*)(.*)/,u={lt:"<",gt:">",quot:'"',apos:"'",ensp:" ",emsp:" ",nbsp:" ",semi:";",ndash:"–",mdash:"—",middot:"·",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",bull:"•",hellip:"…",amp:"&"};class h{config;extraData;isPureText;memoryLeakTimer;constructor({html:e,config:s,extraData:n}){return this.config=s||{},this.extraData=n||t,this.memoryLeakTimer=null,e?this.steps(e):[]}steps(t){const e=this.cleanHtml(t);this.isPureText=!/<\s*img/g.test(e);const s=this.htmlToJson(e);return console.log("@log",s),this.jsonToSkeleton(s)}cleanHtml(t){return a.forEach((e=>{const[s,n]=e;t=t.replace(s,n)})),t}decodeHtml(t){if(!t)return"";let e=t.indexOf("&");for(;-1!==e;){const s=t.indexOf(";",e+3);let n;if(-1===s)break;"#"===t[e+1]?(n=parseInt(("x"===t[e+2]?"0":"")+t.substring(e+2,s)),isNaN(n)||(t=t.substr(0,e)+String.fromCharCode(n)+t.substr(s+1))):(n=t.substring(e+1,s),u[n]&&(t=t.substr(0,e)+(u[n]||"&")+t.substr(s+1))),e=t.indexOf("&",e+1)}return t}isInvalidElement(t){const{ignoredElement:s=e}=this.config;return s.includes(t)}isSelfClosingElement(t,e){return o.test(t)||s.includes(e)}attributeProcessor(t,e){const{format:s={}}=this.config;if(s[e]){const n=s[e];Object.keys(n).forEach((e=>{const s=n[e];t[e]="function"==typeof s?s(t[e],t):s}))}return t}styleProcessor(t){const e=t.split(";"),s={},{adaptive:n=!0}=this.config,{containerWidth:i}=this.extraData;let r=0;e.forEach((t=>{if(!t)return;const[e,o=""]=t.split(":");if(e){const t=e.trim(),l=o.trim();if(n&&i){let e="";l.replace(c,(function(s,n,o){return n&&!isNaN(n)&&"px"===o&&("width"===t&&+n>i?(r=i/+n,e=`${i}${o}`):"height"===t&&r>0&&(e=`${n*r}${o}`)),""})),s[t]=e||l}else s[t]=l}}));return{styleStr:Object.keys(s).map((t=>`${t}:${s[t]}`)).join(";"),styleObj:s}}formatAttributes(t,e){if(!t)return{};const s=this;let n={};return t.replace(l,(function(t,e,i){const r=Array.prototype.slice.call(arguments);if(r.length>=3){const t=i?s.decodeHtml(i).replace(/(^|[^\\])"/g,'$1\\"'):"",{styleObj:r,styleStr:o}=s.styleProcessor(t);"style"===e?(n.styleObj=r,n[e]=o):n[e]=t}return""})),this.attributeProcessor(n,e)}updateHtmlStr(t,e){return t.substring(e.length)}htmlToJson(t){const e=[];for(this.memoryLeakTimer=Date.now();t&&!(Date.now()-this.memoryLeakTimer>=5e3);){if(0===t.indexOf("</")){const n=t.match(r);if(!n)continue;const[i,o]=n,l=this.updateHtmlStr(t,i);if(this.isInvalidElement(o)){t=l;continue}t=l;const a=s.includes(o);e.push({type:a?"selfClosing":"end",name:o});continue}if(0===t.indexOf("<")){const s=t.match(i);if(!s)continue;const[r,o,l=""]=s,a=this.updateHtmlStr(t,r);if(this.isInvalidElement(o)){t=a;continue}t=a;const c=this.isSelfClosingElement(r,o),u=this.formatAttributes(l.replace(/ /g,""),o);let h=n.includes(o)?"block":"inline";const d=u.styleObj;if(d){const{display:t}=d;t&&(h=t)}e.push({type:c?"selfClosing":"start",name:o,attrs:u,display:h});continue}const o=t.indexOf("<"),l=o<0;let a=l?t:t.substring(0,o);a=this.decodeHtml(a),t=l?"":t.substring(o),e.push({type:"text",name:"text",display:"inline",attrs:this.attributeProcessor({content:a},"text")})}return e}transToBlock(t){const{name:e,display:s,attrs:n,children:i=[]}=t;if("img"===e)return!0;if(i.length<=0)return!1;const r=!!i.find((t=>this.transToBlock(t)));return r&&"block"!==s&&n&&n.styleObj&&!("width"in n.styleObj)&&(t.display="block",n.styleObj.display="block",n.style+=";display:block"),r}autoFixer(t){this.isPureText||t.forEach((t=>{this.transToBlock(t)}))}skeletonGenerator(t,e=0){if(t.length<=0)return[];const{autoFix:s=!0}=this.config;let n=0;const i=[];for(;n<t.length;){const{genKey:s,type:r,...o}=t[n],{name:l}=o,a=`${e}_${n}_${l}`,c=["start","end"].includes(r)?"default":r;if("start"===r){const e=t.findIndex((({type:t,genKey:e})=>"end"===t&&e===s));if(-1===e)break;i.push({id:a,...o,type:c,children:this.skeletonGenerator(t.slice(n+1,e),n)}),n=e+1}else i.push({id:a,...o,type:c}),n++}return s&&this.autoFixer(i),i}jsonToSkeleton(t){const e=[];return t.forEach(((t,s)=>{const{type:n}=t;switch(n){case"start":t.genKey=s,e.push(s);break;case"end":const n=e.splice(e.length-1,1)[0];t.genKey=n}})),this.skeletonGenerator(t)}}export{h as MiniParser,e as defaultIgnoreElements};
const t={containerWidth:0},e="area,base,canvas,embed,frame,head,iframe,input,link,map,meta,param,rp,script,source,style,textarea,title,track,wbr".split(","),s="area,base,br,col,circle,ellipse,embed,frame,hr,img,input,line,link,meta,param,path,polygon,rect,source,track,use,wbr".split(","),n="address,article,aside,blockquote,dd,div,dl,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,hr,ol,p,pre,section,table,ul".split(","),i=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,r=/^<\/([-A-Za-z0-9_]+)[^>]*>/,o=/^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/)>/,l=/([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,a=[[/<\?xml.*\?>\n/,""],[/<.*!doctype.*\>\n/,""],[/<.*!DOCTYPE.*\>\n/,""],[/<!--.*?-->/gi,""],[/\/\*.*?\*\//gi,""]],c=/(\d*\.?\d*)(.*)/,u={lt:"<",gt:">",quot:'"',apos:"'",ensp:" ",emsp:" ",nbsp:" ",semi:";",ndash:"–",mdash:"—",middot:"·",lsquo:"‘",rsquo:"’",ldquo:"“",rdquo:"”",bull:"•",hellip:"…",amp:"&"};class h{config;extraData;isPureText;memoryLeakTimer;constructor({html:e,config:s,extraData:n}){return this.config=s||{},this.extraData=n||t,this.memoryLeakTimer=null,e?this.steps(e):[]}steps(t){const e=this.cleanHtml(t);this.isPureText=!/<\s*img/g.test(e);const s=this.htmlToJson(e);return this.jsonToSkeleton(s)}cleanHtml(t){return a.forEach((e=>{const[s,n]=e;t=t.replace(s,n)})),t}decodeHtml(t){if(!t)return"";let e=t.indexOf("&");for(;-1!==e;){const s=t.indexOf(";",e+3);let n;if(-1===s)break;"#"===t[e+1]?(n=parseInt(("x"===t[e+2]?"0":"")+t.substring(e+2,s)),isNaN(n)||(t=t.substr(0,e)+String.fromCharCode(n)+t.substr(s+1))):(n=t.substring(e+1,s),u[n]&&(t=t.substr(0,e)+(u[n]||"&")+t.substr(s+1))),e=t.indexOf("&",e+1)}return t}isInvalidElement(t){const{ignoredElement:s=e}=this.config;return s.includes(t)}isSelfClosingElement(t,e){return o.test(t)||s.includes(e)}attributeProcessor(t,e){const{format:s={}}=this.config;if(s[e]){const n=s[e];Object.keys(n).forEach((e=>{const s=n[e];t[e]="function"==typeof s?s(t[e],t):s}))}return t}styleProcessor(t){const e=t.split(";"),s={},{adaptive:n=!0}=this.config,{containerWidth:i}=this.extraData;let r=0;e.forEach((t=>{if(!t)return;const[e,o=""]=t.split(":");if(e){const t=e.trim(),l=o.trim();if(n&&i){let e="";l.replace(c,(function(s,n,o){return n&&!isNaN(n)&&"px"===o&&("width"===t&&+n>i?(r=i/+n,e=`${i}${o}`):"height"===t&&r>0&&(e=`${n*r}${o}`)),""})),s[t]=e||l}else s[t]=l}}));return{styleStr:Object.keys(s).map((t=>`${t}:${s[t]}`)).join(";"),styleObj:s}}formatAttributes(t,e){if(!t)return{};const s=this;let n={};return t.replace(l,(function(t,e,i){const r=Array.prototype.slice.call(arguments);if(r.length>=3){const t=i?s.decodeHtml(i).replace(/(^|[^\\])"/g,'$1\\"'):"",{styleObj:r,styleStr:o}=s.styleProcessor(t);"style"===e?(n.styleObj=r,n[e]=o):n[e]=t}return""})),this.attributeProcessor(n,e)}updateHtmlStr(t,e){return t.substring(e.length)}htmlToJson(t){const e=[];this.memoryLeakTimer=Date.now();let o="";for(;t;){if(t===o)return console.error("invalid html string"),[];if(o=t,Date.now()-this.memoryLeakTimer>=5e3)return console.error("processing timeout"),[];if(0===t.indexOf("</")){const n=t.match(r);if(!n)continue;const[i,o]=n,l=this.updateHtmlStr(t,i);if(this.isInvalidElement(o)){t=l;continue}t=l;const a=s.includes(o);e.push({type:a?"selfClosing":"end",name:o});continue}if(0===t.indexOf("<")){const s=t.match(i);if(!s)continue;const[r,o,l=""]=s,a=this.updateHtmlStr(t,r);if(this.isInvalidElement(o)){t=a;continue}t=a;const c=this.isSelfClosingElement(r,o),u=this.formatAttributes(l.replace(/ /g,""),o);let h=n.includes(o)?"block":"inline";const d=u.styleObj;if(d){const{display:t}=d;t&&(h=t)}e.push({type:c?"selfClosing":"start",name:o,attrs:u,display:h});continue}const l=t.indexOf("<"),a=l<0;let c=a?t:t.substring(0,l);c=this.decodeHtml(c),t=a?"":t.substring(l),e.push({type:"text",name:"text",display:"inline",attrs:this.attributeProcessor({content:c},"text")})}return e}transToBlock(t){const{name:e,display:s,attrs:n,children:i=[]}=t;if("img"===e)return!0;if(i.length<=0)return!1;const r=!!i.find((t=>this.transToBlock(t)));return r&&"block"!==s&&n&&n.styleObj&&!("width"in n.styleObj)&&(t.display="block",n.styleObj.display="block",n.style+=";display:block"),r}autoFixer(t){this.isPureText||t.forEach((t=>{this.transToBlock(t)}))}skeletonGenerator(t,e=0){if(t.length<=0)return[];const{autoFix:s=!0}=this.config;let n=0;const i=[];for(;n<t.length;){const{genKey:s,type:r,...o}=t[n],{name:l}=o,a=`${e}_${n}_${l}`,c=["start","end"].includes(r)?"default":r;if("start"===r){const e=t.findIndex((({type:t,genKey:e})=>"end"===t&&e===s));if(-1===e)break;i.push({id:a,...o,type:c,children:this.skeletonGenerator(t.slice(n+1,e),n)}),n=e+1}else i.push({id:a,...o,type:c}),n++}return s&&this.autoFixer(i),i}jsonToSkeleton(t){const e=[];return t.forEach(((t,s)=>{const{type:n}=t;switch(n){case"start":t.genKey=s,e.push(s);break;case"end":const n=e.splice(e.length-1,1)[0];t.genKey=n}})),this.skeletonGenerator(t)}}export{h as MiniParser,e as defaultIgnoreElements};
{
"name": "mini-program-parser",
"version": "0.3.10",
"version": "0.3.11",
"scripts": {

@@ -5,0 +5,0 @@ "dev": "rollup -c ./rollup.dev.config.js --watch",

@@ -40,3 +40,2 @@ import {

const jsonData = this.htmlToJson(cleanHtml);
console.log("@log", jsonData);
return this.jsonToSkeleton(jsonData);

@@ -214,5 +213,18 @@ }

this.memoryLeakTimer = Date.now();
// html字符串历史
let prevDecodedHtml = "";
while (decodedHtml) {
if (Date.now() - this.memoryLeakTimer >= 5000) break;
// 如果处理后的html字符串和上一次的html字符串一致,则说明进入了死循环
if (decodedHtml === prevDecodedHtml) {
console.error("invalid html string");
return [];
}else{
prevDecodedHtml = decodedHtml;
}
// 处理超时,直接
if (Date.now() - this.memoryLeakTimer >= 5000) {
console.error("processing timeout");
return [];
}
// 如果是结束标签

@@ -219,0 +231,0 @@ if (decodedHtml.indexOf("</") === 0) {