From ec5673ef38050f808ce49e2e4ee165d30492b190 Mon Sep 17 00:00:00 2001 From: Mark Date: Thu, 24 Dec 2020 18:30:37 -0500 Subject: [PATCH] build(package): upgrade `domhandler` to v4 and `htmlparser2` to v6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. `` is parsed correctly. Remove root parent node to keep parser backwards compatible. --- karma.conf.js | 2 +- lib/server/html-to-dom.d.ts | 4 ++-- lib/server/html-to-dom.js | 10 ++++++---- lib/server/utilities.d.ts | 11 +++++++++++ lib/server/utilities.js | 17 +++++++++++++++++ package.json | 4 ++-- test/helpers/run-tests.js | 10 ++++------ 7 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 lib/server/utilities.d.ts create mode 100644 lib/server/utilities.js diff --git a/karma.conf.js b/karma.conf.js index 2a8eabbb..ce6a094d 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -20,7 +20,7 @@ module.exports = config => { ], // list of files / patterns to exclude - exclude: ['lib/server/**/*.js'], + exclude: ['lib/server/html-to-dom.js'], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor diff --git a/lib/server/html-to-dom.d.ts b/lib/server/html-to-dom.d.ts index 63693270..f1097705 100644 --- a/lib/server/html-to-dom.d.ts +++ b/lib/server/html-to-dom.d.ts @@ -12,10 +12,10 @@ import { * Parses HTML string to DOM nodes in Node.js. * * This is the same method as `require('htmlparser2').parseDOM` - * https://github.com/fb55/htmlparser2/blob/v4.1.0/src/index.ts#L18-L22 + * https://github.com/fb55/htmlparser2/blob/v6.0.0/src/index.ts#L29-L41 * * @param html - HTML markup. - * @param options - Parser options (https://github.com/fb55/domhandler/tree/v3.3.0#readme). + * @param options - Parser options (https://github.com/fb55/domhandler/tree/v4.0.0#readme). * @return - DOM nodes. */ export default function HTMLDOMParser( diff --git a/lib/server/html-to-dom.js b/lib/server/html-to-dom.js index f75c0c46..ee0bab79 100644 --- a/lib/server/html-to-dom.js +++ b/lib/server/html-to-dom.js @@ -1,14 +1,16 @@ var Parser = require('htmlparser2/lib/Parser').Parser; var DomHandler = require('domhandler').DomHandler; +var unsetRootParent = require('./utilities').unsetRootParent; + /** * Parses HTML string to DOM nodes in Node.js. * * This is the same method as `require('htmlparser2').parseDOM` - * https://github.com/fb55/htmlparser2/blob/v4.1.0/src/index.ts#L18-L22 + * https://github.com/fb55/htmlparser2/blob/v6.0.0/src/index.ts#L29-L41 * * @param {string} html - HTML markup. - * @param {DomHandlerOptions} [options] - Parser options (https://github.com/fb55/domhandler/tree/v3.3.0#readme). + * @param {DomHandlerOptions} [options] - Parser options (https://github.com/fb55/domhandler/tree/v4.0.0#readme). * @return {Array<Comment|Element|ProcessingInstruction|Text>} - DOM nodes. */ function HTMLDOMParser(html, options) { @@ -16,13 +18,13 @@ function HTMLDOMParser(html, options) { throw new TypeError('First argument must be a string.'); } - if (!html) { + if (html === '') { return []; } var handler = new DomHandler(undefined, options); new Parser(handler, options).end(html); - return handler.dom; + return unsetRootParent(handler.dom); } module.exports = HTMLDOMParser; diff --git a/lib/server/utilities.d.ts b/lib/server/utilities.d.ts new file mode 100644 index 00000000..a4e5dd52 --- /dev/null +++ b/lib/server/utilities.d.ts @@ -0,0 +1,11 @@ +// TypeScript Version: 4.1 + +type Nodes = Array<Comment | Element | ProcessingInstruction | Text>; + +/** + * Sets root parent to null. + * + * @param nodes + * @return + */ +export function unsetRootParent(nodes: Nodes): Nodes; diff --git a/lib/server/utilities.js b/lib/server/utilities.js new file mode 100644 index 00000000..cf8e6eac --- /dev/null +++ b/lib/server/utilities.js @@ -0,0 +1,17 @@ +/** + * Sets root parent to null. + * + * @param {Array<Comment|Element|ProcessingInstruction|Text>} nodes + * @return {Array<Comment|Element|ProcessingInstruction|Text>} + */ +function unsetRootParent(nodes) { + for (var index = 0, len = nodes.length; index < len; index++) { + var node = nodes[index]; + node.parent = null; + } + return nodes; +} + +module.exports = { + unsetRootParent: unsetRootParent +}; diff --git a/package.json b/package.json index b9601065..e12b0972 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "pojo" ], "dependencies": { - "domhandler": "3.3.0", - "htmlparser2": "4.1.0" + "domhandler": "4.0.0", + "htmlparser2": "6.0.0" }, "devDependencies": { "@commitlint/cli": "^11.0.0", diff --git a/test/helpers/run-tests.js b/test/helpers/run-tests.js index 9a5a98b0..591ae267 100644 --- a/test/helpers/run-tests.js +++ b/test/helpers/run-tests.js @@ -1,3 +1,5 @@ +var unsetRootParent = require('../../lib/server/utilities').unsetRootParent; + var isKarma = typeof window === 'object' && typeof window.__karma__ === 'object'; @@ -10,16 +12,12 @@ var isKarma = * @param {Function} expectedParser - Expected parser. */ function runTests(assert, actualParser, expectedParser, testCases) { - // enable `decodeEntities` for both parsers - // because entities are decoded in the browser - var parserOptions = { decodeEntities: true }; - testCases.forEach(function (testCase) { var _it = testCase.only ? it.only : testCase.skip ? it.skip : it; _it('parses ' + testCase.name, function () { - var actualOutput = actualParser(testCase.data, parserOptions); - var expectedOutput = expectedParser(testCase.data, parserOptions); + var actualOutput = actualParser(testCase.data); + var expectedOutput = unsetRootParent(expectedParser(testCase.data)); // use `JSON.decycle` since `assert.deepEqual` fails // when instance types are different in the browser