node-html-parser
Advanced tools
Comparing version 3.1.5 to 3.1.6
@@ -9,3 +9,2 @@ import he from 'he'; | ||
import CommentNode from './comment'; | ||
import parse from '../parse'; | ||
// const { decode } = he; | ||
@@ -544,3 +543,3 @@ function decode(val) { | ||
if (this.rawAttrs) { | ||
const re = /\b([a-z][a-z0-9-_]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/ig; | ||
const re = /\b([a-z][a-z0-9-_:]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/ig; | ||
let match; | ||
@@ -934,1 +933,40 @@ while ((match = re.exec(this.rawAttrs))) { | ||
} | ||
/** | ||
* Parses HTML and returns a root element | ||
* Parse a chuck of HTML source. | ||
*/ | ||
export function parse(data, options = { lowerCaseTagName: false, comment: false }) { | ||
const stack = base_parse(data, options); | ||
const [root] = stack; | ||
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 root; | ||
} |
@@ -1,41 +0,1 @@ | ||
import arr_back from './back'; | ||
import { base_parse } from './nodes/html'; | ||
/** | ||
* Parses HTML and returns a root element | ||
* Parse a chuck of HTML source. | ||
*/ | ||
export default function parse(data, options = { lowerCaseTagName: false, comment: false }) { | ||
const stack = base_parse(data, options); | ||
const [root] = stack; | ||
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 root; | ||
} | ||
export { parse as default } from './nodes/html'; |
134
dist/main.js
@@ -198,55 +198,6 @@ var __extends = (this && this.__extends) || (function () { | ||
}); | ||
define("parse", ["require", "exports", "back", "nodes/html"], function (require, exports, back_1, html_1) { | ||
define("nodes/html", ["require", "exports", "he", "css-select", "nodes/node", "nodes/type", "nodes/text", "matcher", "back", "nodes/comment"], function (require, exports, he_1, css_select_1, node_2, type_3, text_1, matcher_1, back_1, comment_1) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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", "css-select", "nodes/node", "nodes/type", "nodes/text", "matcher", "back", "nodes/comment", "parse"], function (require, exports, he_1, css_select_1, node_2, 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; | ||
exports.parse = exports.base_parse = void 0; | ||
he_1 = __importDefault(he_1); | ||
@@ -257,5 +208,4 @@ node_2 = __importDefault(node_2); | ||
matcher_1 = __importDefault(matcher_1); | ||
back_2 = __importDefault(back_2); | ||
back_1 = __importDefault(back_1); | ||
comment_1 = __importDefault(comment_1); | ||
parse_1 = __importDefault(parse_1); | ||
// const { decode } = he; | ||
@@ -547,3 +497,3 @@ function decode(val) { | ||
//const r = parse(content, global.options); // TODO global.options ? | ||
var r = parse_1.default(content); | ||
var r = parse(content); | ||
this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)]; | ||
@@ -560,3 +510,3 @@ }, | ||
else if (typeof content == 'string') { | ||
var r = parse_1.default(content, options); | ||
var r = parse(content, options); | ||
content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)]; | ||
@@ -578,3 +528,3 @@ } | ||
// const r = parse(content, global.options); // TODO global.options ? | ||
var r = parse_1.default(node); | ||
var r = parse(node); | ||
return r.childNodes.length ? r.childNodes : [new text_1.default(node, _this)]; | ||
@@ -818,3 +768,3 @@ } | ||
get: function () { | ||
return back_2.default(this.childNodes); | ||
return back_1.default(this.childNodes); | ||
}, | ||
@@ -869,3 +819,3 @@ enumerable: false, | ||
if (this.rawAttrs) { | ||
var re = /\b([a-z][a-z0-9-_]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/ig; | ||
var re = /\b([a-z][a-z0-9-_:]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/ig; | ||
var match = void 0; | ||
@@ -975,3 +925,3 @@ while ((match = re.exec(this.rawAttrs))) { | ||
} | ||
var p = parse_1.default(html); | ||
var p = parse(html); | ||
if (where === 'afterend') { | ||
@@ -1182,3 +1132,3 @@ var idx = this.parentNode.childNodes.findIndex(function (child) { | ||
data = "<" + frameflag + ">" + data + "</" + frameflag + ">"; | ||
var _loop_2 = function () { | ||
var _loop_1 = function () { | ||
if (lastTextPos > -1) { | ||
@@ -1217,3 +1167,3 @@ if (lastTextPos + match[0].length < kMarkupPattern.lastIndex) { | ||
stack.pop(); | ||
currentParent = back_2.default(stack); | ||
currentParent = back_1.default(stack); | ||
} | ||
@@ -1261,3 +1211,3 @@ } | ||
stack.pop(); | ||
currentParent = back_2.default(stack); | ||
currentParent = back_1.default(stack); | ||
break; | ||
@@ -1271,3 +1221,3 @@ } | ||
stack.pop(); | ||
currentParent = back_2.default(stack); | ||
currentParent = back_1.default(stack); | ||
continue; | ||
@@ -1283,3 +1233,3 @@ } | ||
while ((match = kMarkupPattern.exec(data))) { | ||
_loop_2(); | ||
_loop_1(); | ||
} | ||
@@ -1289,2 +1239,46 @@ return stack; | ||
exports.base_parse = base_parse; | ||
/** | ||
* 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 = base_parse(data, options); | ||
var root = stack[0]; | ||
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.childNodes.forEach((node) => { | ||
// if (node instanceof HTMLElement) { | ||
// node.parentNode = null; | ||
// } | ||
// }); | ||
return root; | ||
} | ||
exports.parse = parse; | ||
}); | ||
@@ -1359,2 +1353,8 @@ define("nodes/node", ["require", "exports"], function (require, exports) { | ||
}); | ||
define("parse", ["require", "exports", "nodes/html"], function (require, exports, html_1) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = void 0; | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } }); | ||
}); | ||
define("valid", ["require", "exports", "nodes/html"], function (require, exports, html_2) { | ||
@@ -1374,3 +1374,3 @@ "use strict"; | ||
}); | ||
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_5) { | ||
define("index", ["require", "exports", "nodes/comment", "nodes/html", "parse", "valid", "nodes/node", "nodes/text", "nodes/type"], function (require, exports, comment_2, html_3, parse_1, valid_1, node_4, text_2, type_5) { | ||
"use strict"; | ||
@@ -1381,4 +1381,4 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
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, "parse", { enumerable: true, get: function () { return __importDefault(parse_1).default; } }); | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(parse_1).default; } }); | ||
Object.defineProperty(exports, "valid", { enumerable: true, get: function () { return __importDefault(valid_1).default; } }); | ||
@@ -1385,0 +1385,0 @@ Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return __importDefault(node_4).default; } }); |
@@ -190,2 +190,7 @@ import Node from './node'; | ||
export declare function base_parse(data: string, options?: Partial<Options>): HTMLElement[]; | ||
/** | ||
* Parses HTML and returns a root element | ||
* Parse a chuck of HTML source. | ||
*/ | ||
export declare function parse(data: string, options?: Partial<Options>): HTMLElement; | ||
export {}; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.base_parse = void 0; | ||
exports.parse = exports.base_parse = void 0; | ||
var he_1 = __importDefault(require("he")); | ||
@@ -36,3 +36,2 @@ var css_select_1 = require("css-select"); | ||
var comment_1 = __importDefault(require("./comment")); | ||
var parse_1 = __importDefault(require("../parse")); | ||
// const { decode } = he; | ||
@@ -324,3 +323,3 @@ function decode(val) { | ||
//const r = parse(content, global.options); // TODO global.options ? | ||
var r = parse_1.default(content); | ||
var r = parse(content); | ||
this.childNodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)]; | ||
@@ -337,3 +336,3 @@ }, | ||
else if (typeof content == 'string') { | ||
var r = parse_1.default(content, options); | ||
var r = parse(content, options); | ||
content = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)]; | ||
@@ -355,3 +354,3 @@ } | ||
// const r = parse(content, global.options); // TODO global.options ? | ||
var r = parse_1.default(node); | ||
var r = parse(node); | ||
return r.childNodes.length ? r.childNodes : [new text_1.default(node, _this)]; | ||
@@ -750,3 +749,3 @@ } | ||
} | ||
var p = parse_1.default(html); | ||
var p = parse(html); | ||
if (where === 'afterend') { | ||
@@ -1059,1 +1058,45 @@ var idx = this.parentNode.childNodes.findIndex(function (child) { | ||
exports.base_parse = base_parse; | ||
/** | ||
* 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 = base_parse(data, options); | ||
var root = stack[0]; | ||
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.childNodes.forEach((node) => { | ||
// if (node instanceof HTMLElement) { | ||
// node.parentNode = null; | ||
// } | ||
// }); | ||
return root; | ||
} | ||
exports.parse = parse; |
@@ -1,6 +0,1 @@ | ||
import { Options } from './nodes/html'; | ||
/** | ||
* Parses HTML and returns a root element | ||
* Parse a chuck of HTML source. | ||
*/ | ||
export default function parse(data: string, options?: Partial<Options>): import("./nodes/html").default; | ||
export { parse as default } from './nodes/html'; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var back_1 = __importDefault(require("./back")); | ||
exports.default = void 0; | ||
var html_1 = require("./nodes/html"); | ||
/** | ||
* 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; | ||
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return html_1.parse; } }); |
{ | ||
"name": "node-html-parser", | ||
"version": "3.1.5", | ||
"version": "3.1.6", | ||
"description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
165836
4459