Skip to content

Add support for ignoring < inside stop nodes #499

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 62 additions & 0 deletions spec/html_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,66 @@ const parsingOptions = {
output = output.replace('₹','&inr;');
expect(output.replace(/\s+/g, "")).toEqual(html.replace(/\s+/g, ""));
});


it("should fail to parse HTML <script> tags containing '<' without using options.ignoreTagsInNodes", function () {
const html = `
<html lang="en">
<body>
<script>
// Without options.ignoreTagsInNodes, '<' will attempt to create a new tag, throwing an error since it's missing a closing '>'
if (1 < 2) {}
</script>
</body>
</html>`;

const parsingOptions = {
ignoreAttributes: false,
preserveOrder: true,
unpairedTags: ["hr", "br", "link", "meta"],
stopNodes: ["*.pre", "*.script"],
processEntities: true,
htmlEntities: true,
};
const parser = new XMLParser(parsingOptions);
expect(function () { parser.parse(html); }).toThrow();
});


it("should parse HTML <script> tags containing '<' by using options.ignoreTagsInNodes", function () {
const html = `
<html lang="en">
<body>
<script>
// The character '<' should not create a new tag, and should not need a corresponding '>'
if (1 < 2) {}
</script>
</body>
</html>`;

const parsingOptions = {
ignoreAttributes: false,
preserveOrder: true,
unpairedTags: ["hr", "br", "link", "meta"],
stopNodes: ["*.pre", "*.script"],
ignoreTagsInNodes: ["*.script"],
processEntities: true,
htmlEntities: true,
};
const parser = new XMLParser(parsingOptions);
let result = parser.parse(html);

const builderOptions = {
ignoreAttributes: false,
format: true,
preserveOrder: true,
suppressEmptyNode: false,
unpairedTags: ["hr", "br", "link", "meta"],
stopNodes: ["*.pre", "*.script"],
}
const builder = new XMLBuilder(builderOptions);
let output = builder.build(result);
expect(output.replace(/\s+/g, "")).toEqual(html.replace(/\s+/g, ""));
});

});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Control how tag value should be parsed. Called only if tag value is not empty
attributeValueProcessor: (attrName: string, attrValue: string, jPath: string) => string;
numberParseOptions: strnumOptions;
stopNodes: string[];
ignoreTagsInNodes: string[];
unpairedTags: string[];
alwaysCreateTextNode: boolean;
isArray: (tagName: string, jPath: string, isLeafNode: boolean, isAttribute: boolean) => boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const defaultOptions = {
attributeValueProcessor: function(attrName, val) {
return val;
},
stopNodes: [], //nested tags will not be parsed even for errors
stopNodes: [], // nested tags will not be parsed even for errors
ignoreTagsInNodes: [], // nested tags will not be parsed even for errors
alwaysCreateTextNode: false,
isArray: () => false,
commentPropName: false,
Expand Down
23 changes: 12 additions & 11 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OrderedObjParser{
this.parseTextData = parseTextData;
this.resolveNameSpace = resolveNameSpace;
this.buildAttributesMap = buildAttributesMap;
this.isItStopNode = isItStopNode;
this.checkNodePathMatch = checkNodePathMatch;
this.replaceEntitiesValue = replaceEntitiesValue;
this.readStopNodeData = readStopNodeData;
this.saveTextToParentTag = saveTextToParentTag;
Expand Down Expand Up @@ -289,7 +289,7 @@ const parseXml = function(xmlData) {
currentNode = this.tagsNodeStack.pop();
}

if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
if (this.checkNodePathMatch(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
let tagContent = "";
//self-closing tag
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
Expand All @@ -302,7 +302,7 @@ const parseXml = function(xmlData) {
//normal tag
else{
//read until closing tag is found
const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1);
const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1, this.checkNodePathMatch(this.options.ignoreTagsInNodes, jPath, tagName));
if(!result) throw new Error(`Unexpected end of ${tagName}`);
i = result.i;
tagContent = result.tagContent;
Expand Down Expand Up @@ -403,15 +403,15 @@ function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
//TODO: use jPath to simplify the logic
/**
*
* @param {string[]} stopNodes
* @param {string[]} nodePaths
* @param {string} jPath
* @param {string} currentTagName
* @param {string} currentTagName
*/
function isItStopNode(stopNodes, jPath, currentTagName){
function checkNodePathMatch(nodePaths, jPath, currentTagName) {
const allNodesExp = "*." + currentTagName;
for (const stopNodePath in stopNodes) {
const stopNodeExp = stopNodes[stopNodePath];
if( allNodesExp === stopNodeExp || jPath === stopNodeExp ) return true;
for (const nodePath in nodePaths) {
const nodeExp = nodePaths[nodePath];
if (allNodesExp === nodeExp || jPath === nodeExp) return true;
}
return false;
}
Expand Down Expand Up @@ -494,8 +494,9 @@ function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
* @param {string} xmlData
* @param {string} tagName
* @param {number} i
* @param {boolean} ignoreNestedTags Ignores nested tags if true. This allows parsing of tags like <script>if (a < b) {}</script> without the < triggering a new open tag.
*/
function readStopNodeData(xmlData, tagName, i){
function readStopNodeData(xmlData, tagName, i, ignoreNestedTags){
const startIndex = i;
// Starting at 1 since we already have an open tag
let openTagCount = 1;
Expand Down Expand Up @@ -524,7 +525,7 @@ function readStopNodeData(xmlData, tagName, i){
} else if(xmlData.substr(i + 1, 2) === '![') {
const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
i=closeIndex;
} else {
} else if (!ignoreNestedTags) {
const tagData = readTagExp(xmlData, i, '>')

if (tagData) {
Expand Down