Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dom-parser-mini

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dom-parser-mini - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

2

dist/parser.min.js

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

"use strict";var TokenType;Object.defineProperty(exports,"__esModule",{value:!0}),function(e){e[e.TEXT=0]="TEXT",e[e.TAG_OPEN=1]="TAG_OPEN",e[e.TAG_CLOSE=2]="TAG_CLOSE",e[e.ATTRIBUTE_NAME=3]="ATTRIBUTE_NAME",e[e.ATTRIBUTE_VALUE=4]="ATTRIBUTE_VALUE",e[e.SELF_CLOSING_TAG=5]="SELF_CLOSING_TAG"}(TokenType||(TokenType={}));const selfClosingTags=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]);class HTMLNode{tagName;attributes;children;content;isRemoved;isSelfClosing;constructor(e,t,s,i){this.tagName=e,this.attributes=t,this.children=s,this.content=i,this.isRemoved=!1,this.isSelfClosing=!1}html(){if(this.isRemoved)return"";let e=this.content||"";for(const t of this.children)e+=t.html();return this.isSelfClosing?`<${this.tagName}${this.getAttributesString()} />`:`<${this.tagName}${this.getAttributesString()}>${e}</${this.tagName}>`}text(){return this.isRemoved?"":this.content||""}getElementById(e){if(this.isRemoved)return null;if(this.attributes.id===e)return this;for(const t of this.children){const s=t.getElementById(e);if(s)return s}return null}getElementsByClass(e){if(this.isRemoved)return[];const t=[];this.attributes.class&&this.attributes.class.split(" ").includes(e)&&t.push(this);for(const s of this.children)t.push(...s.getElementsByClass(e));return t}remove(){this.isRemoved=!0}unRemove(){this.isRemoved=!1}hidden(){this.isRemoved||(this.attributes.style=`${this.attributes.style||""}; display: none;`)}show(){this.isRemoved||this.attributes.style&&(this.attributes.style=this.attributes.style.replace(/display:\s*none;?/,"").trim(),this.attributes.style||delete this.attributes.style)}filterAttributes(e){if(e.includes("*"))return;const t={};for(const[s,i]of Object.entries(this.attributes))e.includes(s)&&(t[s]=i);this.attributes=t;for(const t of this.children)t.filterAttributes(e)}getAttributesString(){return Object.entries(this.attributes).map((([e,t])=>` ${e}="${t}"`)).join("")}static create(e){const t=HTMLNode.tokenize(e);console.log(t);const s=[],i=[];let n=null,r={},o="";for(const e of t)switch(e.type){case TokenType.TAG_OPEN:n&&(0===Object.keys(n.attributes).length&&(n.attributes=r),n.content=o.trim(),r={},o="",i.push(n)),n=new HTMLNode(e.value,{},[]);break;case TokenType.ATTRIBUTE_NAME:r[e.value]="";break;case TokenType.ATTRIBUTE_VALUE:r[Object.keys(r).pop()]=e.value;break;case TokenType.TAG_CLOSE:case TokenType.SELF_CLOSING_TAG:if(!n)break;n.isSelfClosing=e.type===TokenType.SELF_CLOSING_TAG,0===Object.keys(n.attributes).length&&(n.attributes=r),n.content?n.content+=" "+o:n.content=o,o="",r={},i.length>0?i[i.length-1].children.push(n):s.push(n),n=i.pop()||null;break;case TokenType.TEXT:n&&(o+=e.value)}return s}static tokenize(e){const t=[];let s=0;for(;s<e.length;)if("<"===e[s])if("/"===e[s+1]){let i=s+2;for(;i<e.length&&">"!==e[i];)i++;t.push({type:TokenType.TAG_CLOSE,value:e.slice(s+2,i).trim()}),s=i+1}else{let i=s+1;for(;i<e.length&&" "!==e[i]&&">"!==e[i]&&"/"!==e[i];)i++;const n=e.slice(s+1,i).trim();for(t.push({type:TokenType.TAG_OPEN,value:e.slice(s+1,i).trim()});i<e.length&&">"!==e[i];)if(" "===e[i]){i++;let s="";for(;i<e.length&&"="!==e[i]&&" "!==e[i]&&">"!==e[i]&&"/"!==e[i];)s+=e[i],i++;if(""!==s&&t.push({type:TokenType.ATTRIBUTE_NAME,value:s.trim().toLowerCase()}),"="===e[i]){i++;const s=e[i];i++;let n="";for(;i<e.length&&e[i]!==s;)n+=e[i],i++;t.push({type:TokenType.ATTRIBUTE_VALUE,value:n}),i++}}else i++;selfClosingTags.has(n.toLowerCase())&&">"===e[i]&&t.push({type:TokenType.SELF_CLOSING_TAG,value:n}),">"===e[i]&&i++,s=i}else{let i=s;for(;i<e.length&&"<"!==e[i];)i++;t.push({type:TokenType.TEXT,value:e.slice(s,i)}),s=i}return t}}exports.default=HTMLNode;
"use strict";var TokenType;Object.defineProperty(exports,"__esModule",{value:!0}),function(e){e[e.TEXT=0]="TEXT",e[e.TAG_OPEN=1]="TAG_OPEN",e[e.TAG_CLOSE=2]="TAG_CLOSE",e[e.ATTRIBUTE_NAME=3]="ATTRIBUTE_NAME",e[e.ATTRIBUTE_VALUE=4]="ATTRIBUTE_VALUE",e[e.SELF_CLOSING_TAG=5]="SELF_CLOSING_TAG"}(TokenType||(TokenType={}));const selfClosingTags=new Set(["area","base","br","col","embed","hr","img","input","link","meta","source","track","wbr"]);class HTMLNode{tagName;attributes;children;content;isRemoved;isSelfClosing;constructor(e,t,s,i){this.tagName=e,this.attributes=t,this.children=s,this.content=i,this.isRemoved=!1,this.isSelfClosing=!1}html(){if(this.isRemoved)return"";let e=this.content||"";for(const t of this.children)e+=t.html();return this.isSelfClosing?`<${this.tagName}${this.getAttributesString()} />`:`<${this.tagName}${this.getAttributesString()}>${e}</${this.tagName}>`}text(){return this.isRemoved?"":this.content||""}getElementById(e){if(this.isRemoved)return null;if(this.attributes.id===e)return this;for(const t of this.children){const s=t.getElementById(e);if(s)return s}return null}getElementsByClass(e){if(this.isRemoved)return[];const t=[];this.attributes.class&&this.attributes.class.split(" ").includes(e)&&t.push(this);for(const s of this.children)t.push(...s.getElementsByClass(e));return t}remove(){this.isRemoved=!0}unRemove(){this.isRemoved=!1}hidden(){this.isRemoved||(this.attributes.style=`${this.attributes.style||""}; display: none;`)}show(){this.isRemoved||this.attributes.style&&(this.attributes.style=this.attributes.style.replace(/display:\s*none;?/,"").trim(),this.attributes.style||delete this.attributes.style)}filterAttributes(e){if(e.includes("*"))return;const t={};for(const[s,i]of Object.entries(this.attributes))e.includes(s)&&(t[s]=i);this.attributes=t;for(const t of this.children)t.filterAttributes(e)}getAttributesString(){return Object.entries(this.attributes).map((([e,t])=>` ${e}="${t}"`)).join("")}static create(e){const t=HTMLNode.tokenize(e),s=[],i=[];let n=null,r={},o="";for(const e of t)switch(e.type){case TokenType.TAG_OPEN:n&&(0===Object.keys(n.attributes).length&&(n.attributes=r),n.content=o.trim(),r={},o="",i.push(n)),n=new HTMLNode(e.value,{},[]);break;case TokenType.ATTRIBUTE_NAME:r[e.value]="";break;case TokenType.ATTRIBUTE_VALUE:r[Object.keys(r).pop()]=e.value;break;case TokenType.TAG_CLOSE:case TokenType.SELF_CLOSING_TAG:if(!n)break;n.isSelfClosing=e.type===TokenType.SELF_CLOSING_TAG,0===Object.keys(n.attributes).length&&(n.attributes=r),n.content?n.content+=" "+o:n.content=o,o="",r={},i.length>0?i[i.length-1].children.push(n):s.push(n),n=i.pop()||null;break;case TokenType.TEXT:n&&(o+=e.value)}return s}static tokenize(e){const t=[];let s=0;for(;s<e.length;)if("<"===e[s])if("/"===e[s+1]){let i=s+2;for(;i<e.length&&">"!==e[i];)i++;t.push({type:TokenType.TAG_CLOSE,value:e.slice(s+2,i).trim()}),s=i+1}else{let i=s+1;for(;i<e.length&&" "!==e[i]&&">"!==e[i]&&"/"!==e[i];)i++;const n=e.slice(s+1,i).trim();for(t.push({type:TokenType.TAG_OPEN,value:e.slice(s+1,i).trim()});i<e.length&&">"!==e[i];)if(" "===e[i]){i++;let s="";for(;i<e.length&&"="!==e[i]&&" "!==e[i]&&">"!==e[i]&&"/"!==e[i];)s+=e[i],i++;if(""!==s&&t.push({type:TokenType.ATTRIBUTE_NAME,value:s.trim().toLowerCase()}),"="===e[i]){i++;const s=e[i];i++;let n="";for(;i<e.length&&e[i]!==s;)n+=e[i],i++;t.push({type:TokenType.ATTRIBUTE_VALUE,value:n}),i++}}else i++;selfClosingTags.has(n.toLowerCase())&&">"===e[i]&&t.push({type:TokenType.SELF_CLOSING_TAG,value:n}),">"===e[i]&&i++,s=i}else{let i=s;for(;i<e.length&&"<"!==e[i];)i++;t.push({type:TokenType.TEXT,value:e.slice(s,i)}),s=i}return t}}exports.default=HTMLNode;
{
"name": "dom-parser-mini",
"version": "2.0.0",
"version": "2.0.1",
"description": "A lightweight library for parsing HTML elements.",

@@ -5,0 +5,0 @@ "main": "dist/parser.min.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc