Skip to content

Commit ec5673e

Browse files
build(package): upgrade domhandler to v4 and htmlparser2 to v6
BREAKING CHANGE: upgrade `domhandler` to v4 and `htmlparser2` to v6 domhandler 3.3.0 β†’ 4.0.0 htmlparser2 4.1.0 β†’ 6.0.0 domhandler: * https://github.com/fb55/domhandler/releases/tag/v4.0.0 htmlparser2: * https://github.com/fb55/htmlparser2/releases/tag/v5.0.0 * https://github.com/fb55/htmlparser2/releases/tag/v5.0.1 * https://github.com/fb55/htmlparser2/releases/tag/v6.0.0 `decodeEntities` option now defaults to true. `<title>` is parsed correctly. Remove root parent node to keep parser backwards compatible.
1 parent f8b0749 commit ec5673e

File tree

7 files changed

+43
-15
lines changed

7 files changed

+43
-15
lines changed

β€Žkarma.conf.jsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = config => {
2020
],
2121

2222
// list of files / patterns to exclude
23-
exclude: ['lib/server/**/*.js'],
23+
exclude: ['lib/server/html-to-dom.js'],
2424

2525
// preprocess matching files before serving them to the browser
2626
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor

β€Žlib/server/html-to-dom.d.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import {
1212
* Parses HTML string to DOM nodes in Node.js.
1313
*
1414
* This is the same method as `require('htmlparser2').parseDOM`
15-
* https://github.com/fb55/htmlparser2/blob/v4.1.0/src/index.ts#L18-L22
15+
* https://github.com/fb55/htmlparser2/blob/v6.0.0/src/index.ts#L29-L41
1616
*
1717
* @param html - HTML markup.
18-
* @param options - Parser options (https://github.com/fb55/domhandler/tree/v3.3.0#readme).
18+
* @param options - Parser options (https://github.com/fb55/domhandler/tree/v4.0.0#readme).
1919
* @return - DOM nodes.
2020
*/
2121
export default function HTMLDOMParser(

β€Žlib/server/html-to-dom.jsβ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
var Parser = require('htmlparser2/lib/Parser').Parser;
22
var DomHandler = require('domhandler').DomHandler;
33

4+
var unsetRootParent = require('./utilities').unsetRootParent;
5+
46
/**
57
* Parses HTML string to DOM nodes in Node.js.
68
*
79
* This is the same method as `require('htmlparser2').parseDOM`
8-
* https://github.com/fb55/htmlparser2/blob/v4.1.0/src/index.ts#L18-L22
10+
* https://github.com/fb55/htmlparser2/blob/v6.0.0/src/index.ts#L29-L41
911
*
1012
* @param {string} html - HTML markup.
11-
* @param {DomHandlerOptions} [options] - Parser options (https://github.com/fb55/domhandler/tree/v3.3.0#readme).
13+
* @param {DomHandlerOptions} [options] - Parser options (https://github.com/fb55/domhandler/tree/v4.0.0#readme).
1214
* @return {Array<Comment|Element|ProcessingInstruction|Text>} - DOM nodes.
1315
*/
1416
function HTMLDOMParser(html, options) {
1517
if (typeof html !== 'string') {
1618
throw new TypeError('First argument must be a string.');
1719
}
1820

19-
if (!html) {
21+
if (html === '') {
2022
return [];
2123
}
2224

2325
var handler = new DomHandler(undefined, options);
2426
new Parser(handler, options).end(html);
25-
return handler.dom;
27+
return unsetRootParent(handler.dom);
2628
}
2729

2830
module.exports = HTMLDOMParser;

β€Žlib/server/utilities.d.tsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// TypeScript Version: 4.1
2+
3+
type Nodes = Array<Comment | Element | ProcessingInstruction | Text>;
4+
5+
/**
6+
* Sets root parent to null.
7+
*
8+
* @param nodes
9+
* @return
10+
*/
11+
export function unsetRootParent(nodes: Nodes): Nodes;

β€Žlib/server/utilities.jsβ€Ž

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Sets root parent to null.
3+
*
4+
* @param {Array<Comment|Element|ProcessingInstruction|Text>} nodes
5+
* @return {Array<Comment|Element|ProcessingInstruction|Text>}
6+
*/
7+
function unsetRootParent(nodes) {
8+
for (var index = 0, len = nodes.length; index < len; index++) {
9+
var node = nodes[index];
10+
node.parent = null;
11+
}
12+
return nodes;
13+
}
14+
15+
module.exports = {
16+
unsetRootParent: unsetRootParent
17+
};

β€Žpackage.jsonβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"pojo"
3535
],
3636
"dependencies": {
37-
"domhandler": "3.3.0",
38-
"htmlparser2": "4.1.0"
37+
"domhandler": "4.0.0",
38+
"htmlparser2": "6.0.0"
3939
},
4040
"devDependencies": {
4141
"@commitlint/cli": "^11.0.0",

β€Žtest/helpers/run-tests.jsβ€Ž

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var unsetRootParent = require('../../lib/server/utilities').unsetRootParent;
2+
13
var isKarma =
24
typeof window === 'object' && typeof window.__karma__ === 'object';
35

@@ -10,16 +12,12 @@ var isKarma =
1012
* @param {Function} expectedParser - Expected parser.
1113
*/
1214
function runTests(assert, actualParser, expectedParser, testCases) {
13-
// enable `decodeEntities` for both parsers
14-
// because entities are decoded in the browser
15-
var parserOptions = { decodeEntities: true };
16-
1715
testCases.forEach(function (testCase) {
1816
var _it = testCase.only ? it.only : testCase.skip ? it.skip : it;
1917

2018
_it('parses ' + testCase.name, function () {
21-
var actualOutput = actualParser(testCase.data, parserOptions);
22-
var expectedOutput = expectedParser(testCase.data, parserOptions);
19+
var actualOutput = actualParser(testCase.data);
20+
var expectedOutput = unsetRootParent(expectedParser(testCase.data));
2321

2422
// use `JSON.decycle` since `assert.deepEqual` fails
2523
// when instance types are different in the browser

0 commit comments

Comments
Β (0)