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

node-html-parser

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-html-parser - npm Package Compare versions

Comparing version 1.4.9 to 2.0.0

dist/esm/parse.js

4

dist/esm/index.js
export { default as CommentNode } from './nodes/comment';
export { default as HTMLElement, parse, parse as default } from './nodes/html';
export { default as HTMLElement } from './nodes/html';
export { default as parse, default } from './parse';
export { default as valid } from './valid';
export { default as Node } from './nodes/node';
export { default as TextNode } from './nodes/text';
export { default as NodeType } from './nodes/type';

@@ -8,2 +8,3 @@ import { decode } from 'he';

import CommentNode from './comment';
import parse from '../parse';
const kBlockElements = new Map();

@@ -54,3 +55,2 @@ kBlockElements.set('DIV', true);

this.rawAttrs = rawAttrs || '';
this.parentNode = parentNode || null;
this.childNodes = [];

@@ -671,3 +671,9 @@ if (keyAttrs.id) {

const frameflag = 'documentfragmentcontainer';
export function parse(data, options = { lowerCaseTagName: false, comment: false }) {
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
* @param {string} data html
* @return {HTMLElement} root element
*/
export function base_parse(data, options = { lowerCaseTagName: false, comment: false }) {
const elements = options.blockTextElements || {

@@ -801,41 +807,3 @@ script: true,

}
const valid = Boolean(stack.length === 1);
if (!options.noFix) {
const response = root;
response.valid = valid;
while (stack.length > 1) {
// Handle each error elements.
const last = stack.pop();
const oneBefore = arr_back(stack);
if (last.parentNode && last.parentNode.parentNode) {
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
oneBefore.removeChild(last);
last.childNodes.forEach((child) => {
oneBefore.parentNode.appendChild(child);
});
stack.pop();
}
else {
// Single error <div> <h3> </div> handle: Just removes <h3>
oneBefore.removeChild(last);
last.childNodes.forEach((child) => {
oneBefore.appendChild(child);
});
}
}
else {
// If it's final element just skip.
}
}
response.childNodes.forEach((node) => {
if (node instanceof HTMLElement) {
node.parentNode = null;
}
});
return response;
}
const response = new TextNode(data);
response.valid = valid;
return response;
return stack;
}
export { default as CommentNode } from './nodes/comment';
export { default as HTMLElement, parse, parse as default, Options } from './nodes/html';
export { default as HTMLElement, Options } from './nodes/html';
export { default as parse, default } from './parse';
export { default as valid } from './valid';
export { default as Node } from './nodes/node';
export { default as TextNode } from './nodes/text';
export { default as NodeType } from './nodes/type';

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeType = exports.TextNode = exports.Node = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
exports.NodeType = exports.TextNode = exports.Node = exports.valid = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
var comment_1 = require("./nodes/comment");

@@ -12,4 +12,7 @@ Object.defineProperty(exports, "CommentNode", { enumerable: true, get: function () { return __importDefault(comment_1).default; } });

Object.defineProperty(exports, "HTMLElement", { enumerable: true, get: function () { return __importDefault(html_1).default; } });
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return html_1.parse; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } });
var parse_1 = require("./parse");
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(parse_1).default; } });
var valid_1 = require("./valid");
Object.defineProperty(exports, "valid", { enumerable: true, get: function () { return __importDefault(valid_1).default; } });
var node_1 = require("./nodes/node");

@@ -16,0 +19,0 @@ Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return __importDefault(node_1).default; } });

@@ -400,6 +400,55 @@ var __extends = (this && this.__extends) || (function () {

});
define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "nodes/text", "matcher", "back", "nodes/comment"], function (require, exports, he_1, node_3, type_3, text_1, matcher_1, back_1, comment_1) {
define("parse", ["require", "exports", "back", "nodes/html"], function (require, exports, back_1, html_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
back_1 = __importDefault(back_1);
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
function parse(data, options) {
if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
var stack = html_1.base_parse(data, options);
var root = stack[0];
var _loop_1 = function () {
// Handle each error elements.
var last = stack.pop();
var oneBefore = back_1.default(stack);
if (last.parentNode && last.parentNode.parentNode) {
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.parentNode.appendChild(child);
});
stack.pop();
}
else {
// Single error <div> <h3> </div> handle: Just removes <h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.appendChild(child);
});
}
}
else {
// If it's final element just skip.
}
};
while (stack.length > 1) {
_loop_1();
}
// response.childNodes.forEach((node) => {
// if (node instanceof HTMLElement) {
// node.parentNode = null;
// }
// });
return root;
}
exports.default = parse;
});
define("nodes/html", ["require", "exports", "he", "nodes/node", "nodes/type", "nodes/text", "matcher", "back", "nodes/comment", "parse"], function (require, exports, he_1, node_3, type_3, text_1, matcher_1, back_2, comment_1, parse_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.base_parse = void 0;
node_3 = __importDefault(node_3);

@@ -409,4 +458,5 @@ type_3 = __importDefault(type_3);

matcher_1 = __importDefault(matcher_1);
back_1 = __importDefault(back_1);
back_2 = __importDefault(back_2);
comment_1 = __importDefault(comment_1);
parse_1 = __importDefault(parse_1);
var kBlockElements = new Map();

@@ -460,3 +510,2 @@ kBlockElements.set('DIV', true);

_this.rawAttrs = rawAttrs || '';
_this.parentNode = parentNode || null;
_this.childNodes = [];

@@ -624,3 +673,3 @@ if (keyAttrs.id) {

else if (typeof content == 'string') {
var r = parse(content, options);
var r = parse_1.default(content, options);
content = r.childNodes.length ? r.childNodes : [new text_1.default(content)];

@@ -744,3 +793,3 @@ }

while (stack.length) {
var state = back_1.default(stack); // get last element
var state = back_2.default(stack); // get last element
var el = state[0];

@@ -799,3 +848,3 @@ if (state[1] === 0) {

while (stack.length) {
var state = back_1.default(stack);
var state = back_2.default(stack);
var el = state[0];

@@ -858,3 +907,3 @@ if (state[1] === 0) {

get: function () {
return back_1.default(this.childNodes);
return back_2.default(this.childNodes);
},

@@ -984,3 +1033,3 @@ enumerable: false,

}
var p = parse(html);
var p = parse_1.default(html);
if (where === 'afterend') {

@@ -1144,3 +1193,9 @@ var idx = this.parentNode.childNodes.findIndex(function (child) {

var frameflag = 'documentfragmentcontainer';
function parse(data, options) {
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
* @param {string} data html
* @return {HTMLElement} root element
*/
function base_parse(data, options) {
if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }

@@ -1179,3 +1234,3 @@ var elements = options.blockTextElements || {

data = "<" + frameflag + ">" + data + "</" + frameflag + ">";
var _loop_1 = function () {
var _loop_2 = function () {
if (lastTextPos > -1) {

@@ -1214,3 +1269,3 @@ if (lastTextPos + match[0].length < kMarkupPattern.lastIndex) {

stack.pop();
currentParent = back_1.default(stack);
currentParent = back_2.default(stack);
}

@@ -1258,3 +1313,3 @@ }

stack.pop();
currentParent = back_1.default(stack);
currentParent = back_2.default(stack);
break;

@@ -1268,3 +1323,3 @@ }

stack.pop();
currentParent = back_1.default(stack);
currentParent = back_2.default(stack);
continue;

@@ -1280,57 +1335,31 @@ }

while ((match = kMarkupPattern.exec(data))) {
_loop_1();
_loop_2();
}
var valid = Boolean(stack.length === 1);
if (!options.noFix) {
var response_1 = root;
response_1.valid = valid;
var _loop_2 = function () {
// Handle each error elements.
var last = stack.pop();
var oneBefore = back_1.default(stack);
if (last.parentNode && last.parentNode.parentNode) {
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.parentNode.appendChild(child);
});
stack.pop();
}
else {
// Single error <div> <h3> </div> handle: Just removes <h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.appendChild(child);
});
}
}
else {
// If it's final element just skip.
}
};
while (stack.length > 1) {
_loop_2();
}
response_1.childNodes.forEach(function (node) {
if (node instanceof HTMLElement) {
node.parentNode = null;
}
});
return response_1;
}
var response = new text_1.default(data);
response.valid = valid;
return response;
return stack;
}
exports.parse = parse;
exports.base_parse = base_parse;
});
define("index", ["require", "exports", "nodes/comment", "nodes/html", "nodes/node", "nodes/text", "nodes/type"], function (require, exports, comment_2, html_1, node_4, text_2, type_4) {
define("valid", ["require", "exports", "nodes/html"], function (require, exports, html_2) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeType = exports.TextNode = exports.Node = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
*/
function valid(data, options) {
if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }
var stack = html_2.base_parse(data, options);
return Boolean(stack.length === 1);
}
exports.default = valid;
});
define("index", ["require", "exports", "nodes/comment", "nodes/html", "parse", "valid", "nodes/node", "nodes/text", "nodes/type"], function (require, exports, comment_2, html_3, parse_2, valid_1, node_4, text_2, type_4) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeType = exports.TextNode = exports.Node = exports.valid = exports.default = exports.parse = exports.HTMLElement = exports.CommentNode = void 0;
Object.defineProperty(exports, "CommentNode", { enumerable: true, get: function () { return __importDefault(comment_2).default; } });
Object.defineProperty(exports, "HTMLElement", { enumerable: true, get: function () { return __importDefault(html_1).default; } });
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return html_1.parse; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } });
Object.defineProperty(exports, "HTMLElement", { enumerable: true, get: function () { return __importDefault(html_3).default; } });
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return __importDefault(parse_2).default; } });
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(parse_2).default; } });
Object.defineProperty(exports, "valid", { enumerable: true, get: function () { return __importDefault(valid_1).default; } });
Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return __importDefault(node_4).default; } });

@@ -1337,0 +1366,0 @@ Object.defineProperty(exports, "TextNode", { enumerable: true, get: function () { return __importDefault(text_2).default; } });

import Node from './node';
import NodeType from './type';
import TextNode from './text';
import Matcher from '../matcher';

@@ -27,3 +26,3 @@ export interface KeyAttributes {

private rawAttrs;
parentNode: Node;
parentNode: HTMLElement;
private _attrs;

@@ -45,3 +44,3 @@ private _rawAttrs;

*/
constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs?: string, parentNode?: Node);
constructor(tagName: string, keyAttrs: KeyAttributes, rawAttrs?: string, parentNode?: HTMLElement);
/**

@@ -173,14 +172,2 @@ * Remove current element

*/
export declare function parse(data: string, options?: Partial<Options>): HTMLElement & {
valid: boolean;
};
export declare function parse(data: string, options?: Partial<Options> & {
noFix: false;
}): HTMLElement & {
valid: boolean;
};
export declare function parse(data: string, options?: Partial<Options> & {
noFix: true;
}): (HTMLElement | TextNode) & {
valid: boolean;
};
export declare function base_parse(data: string, options?: Partial<Options>): HTMLElement[];

@@ -26,3 +26,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
exports.base_parse = void 0;
var he_1 = require("he");

@@ -35,2 +35,3 @@ var node_1 = __importDefault(require("./node"));

var comment_1 = __importDefault(require("./comment"));
var parse_1 = __importDefault(require("../parse"));
var kBlockElements = new Map();

@@ -84,3 +85,2 @@ kBlockElements.set('DIV', true);

_this.rawAttrs = rawAttrs || '';
_this.parentNode = parentNode || null;
_this.childNodes = [];

@@ -248,3 +248,3 @@ if (keyAttrs.id) {

else if (typeof content == 'string') {
var r = parse(content, options);
var r = parse_1.default(content, options);
content = r.childNodes.length ? r.childNodes : [new text_1.default(content)];

@@ -605,3 +605,3 @@ }

}
var p = parse(html);
var p = parse_1.default(html);
if (where === 'afterend') {

@@ -765,3 +765,9 @@ var idx = this.parentNode.childNodes.findIndex(function (child) {

var frameflag = 'documentfragmentcontainer';
function parse(data, options) {
/**
* Parses HTML and returns a root element
* Parse a chuck of HTML source.
* @param {string} data html
* @return {HTMLElement} root element
*/
function base_parse(data, options) {
if (options === void 0) { options = { lowerCaseTagName: false, comment: false }; }

@@ -899,45 +905,4 @@ var elements = options.blockTextElements || {

}
var valid = Boolean(stack.length === 1);
if (!options.noFix) {
var response_1 = root;
response_1.valid = valid;
var _loop_2 = function () {
// Handle each error elements.
var last = stack.pop();
var oneBefore = back_1.default(stack);
if (last.parentNode && last.parentNode.parentNode) {
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.parentNode.appendChild(child);
});
stack.pop();
}
else {
// Single error <div> <h3> </div> handle: Just removes <h3>
oneBefore.removeChild(last);
last.childNodes.forEach(function (child) {
oneBefore.appendChild(child);
});
}
}
else {
// If it's final element just skip.
}
};
while (stack.length > 1) {
_loop_2();
}
response_1.childNodes.forEach(function (node) {
if (node instanceof HTMLElement) {
node.parentNode = null;
}
});
return response_1;
}
var response = new text_1.default(data);
response.valid = valid;
return response;
return stack;
}
exports.parse = parse;
exports.base_parse = base_parse;
{
"name": "node-html-parser",
"version": "1.4.9",
"version": "2.0.0",
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.",

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

@@ -87,2 +87,6 @@ # Fast HTML Parser [![NPM version](https://badge.fury.io/js/node-html-parser.png)](http://badge.fury.io/js/node-html-parser) [![Build Status](https://travis-ci.org/taoqf/node-html-parser.svg?branch=master)](https://travis-ci.org/taoqf/node-html-parser)

### valid(data[, options])
Parse given data, return the root element if the givent data is valid, and return false if not valid.
## HTMLElement Methods

@@ -89,0 +93,0 @@

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