Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(package): upgrade domhandler to v4 and htmlparser2 to v6 #45

Merged
merged 1 commit into from
Dec 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/server/html-to-dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 6 additions & 4 deletions lib/server/html-to-dom.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
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) {
if (typeof html !== 'string') {
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;
11 changes: 11 additions & 0 deletions lib/server/utilities.d.ts
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 17 additions & 0 deletions lib/server/utilities.js
Original file line number Diff line number Diff line change
@@ -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
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions test/helpers/run-tests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var unsetRootParent = require('../../lib/server/utilities').unsetRootParent;

var isKarma =
typeof window === 'object' && typeof window.__karma__ === 'object';

Expand All @@ -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
Expand Down