Skip to content

Commit

Permalink
Merge pull request #45 from remarkablemark/build/dependencies
Browse files Browse the repository at this point in the history
build(package): upgrade `domhandler` to v4 and `htmlparser2` to v6
  • Loading branch information
remarkablemark committed Dec 25, 2020
2 parents f8b0749 + ec5673e commit 74811a3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
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

0 comments on commit 74811a3

Please sign in to comment.