Skip to content

Commit

Permalink
refactor: removed parse5-sax-parser package in favor parse5 (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 19, 2021
1 parent 6654c9d commit 0758b1a
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 74 deletions.
20 changes: 2 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"dependencies": {
"html-minifier-terser": "^5.1.1",
"parse5-sax-parser": "^6.0.1"
"parse5": "^6.0.1"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
Expand Down
26 changes: 26 additions & 0 deletions src/parse5-traverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const traverse = (root, callback) => {
const visit = (node, parent) => {
let res;

if (callback) {
res = callback(node, parent);
}

let { childNodes } = node;

// in case a <template> tag is in the middle of the HTML: https://github.com/JPeer264/node-rcs-core/issues/58
if (node.content && Array.isArray(node.content.childNodes)) {
({ childNodes } = node.content);
}

if (res !== false && Array.isArray(childNodes) && childNodes.length >= 0) {
childNodes.forEach((child) => {
visit(child, node);
});
}
};

visit(root, null);
};

module.exports = traverse;
14 changes: 9 additions & 5 deletions src/plugins/sources-plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import SAXParser from 'parse5-sax-parser';
import parse5 from 'parse5';

import traverse from '../parse5-traverse';

import {
getFilter,
Expand All @@ -9,12 +11,16 @@ import {

export default (options) =>
function process(html) {
const parser5 = new SAXParser({ sourceCodeLocationInfo: true });
const sources = [];
const document = parse5.parse(html, { sourceCodeLocationInfo: true });

parser5.on('startTag', (node) => {
traverse(document, (node) => {
const { tagName, attrs: attributes, sourceCodeLocation } = node;

if (!tagName) {
return;
}

attributes.forEach((attribute) => {
let { name } = attribute;

Expand Down Expand Up @@ -92,8 +98,6 @@ export default (options) =>
});
});

parser5.end(html);

const urlFilter = getFilter(options.sources.urlFilter);
const imports = new Map();
const replacements = new Map();
Expand Down
27 changes: 21 additions & 6 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

39 changes: 27 additions & 12 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

135 changes: 105 additions & 30 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,9 @@ <h2>An Ordered HTML List</h2>
<img src='\nested\image3.png' />
<img src='/nested\image3.png' />
<img src='nested\image3.png' />

<div class="will-be-parsed">
<template type="some_type">
<div class="will-not-be-parse"></div>
</template>
</div>

0 comments on commit 0758b1a

Please sign in to comment.