🎩 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.9
to
0.3.10
+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;constructor({html:e,config:s,extraData:n}){return this.config=s||{},this.extraData=n||t,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=[];for(;t;){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 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};
{
"name": "mini-program-parser",
"version": "0.3.9",
"version": "0.3.10",
"scripts": {

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

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

private isPureText: boolean | undefined; // 纯文本无图片
private memoryLeakTimer: null | number; // 防止内存溢出

@@ -31,2 +32,3 @@ constructor({ html, config, extraData }: ConstructorType) {

this.extraData = extraData || defaultExtraData;
this.memoryLeakTimer = null;
return html ? this.steps(html) : [];

@@ -40,2 +42,3 @@ }

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

@@ -211,4 +214,7 @@ }

const jsonData = [];
// 记录当前时间戳
this.memoryLeakTimer = Date.now();
while (decodedHtml) {
if (Date.now() - this.memoryLeakTimer >= 5000) break;
// 如果是结束标签

@@ -301,3 +307,3 @@ if (decodedHtml.indexOf("</") === 0) {

const hasImage = !!children.find((item) => this.transToBlock(item));
// 如果存在子元素且当前元素非块级元素,且不存在宽度时
// 如果存在图片子元素且当前元素是未设置宽度的非块级元素时
if (

@@ -304,0 +310,0 @@ hasImage &&