Skip to content

Commit

Permalink
fix unpaired tag
Browse files Browse the repository at this point in the history
when unpaired tags appear in last of a nested tag. Also throw error when unpaired tag is used as closing tag
  • Loading branch information
amitguptagwl committed Apr 28, 2023
1 parent b6cad83 commit 6511e07
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/v4/2.XMLparseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ const xmlDataStr = `
<empty />
<unpaired>
<unpaired />
<unpaired></unpaired>
<unpaired>
</rootNode>`;

const options = {
Expand All @@ -904,7 +904,7 @@ Output
}
}
```

**Note**: Unpaired tag can't be used as closing tag e.g. `</unpaired>` is not allowed.
## updateTag

This property allow you to change tag name when you send different name, skip a tag from the parsing result when you return false, or to change attributes.
Expand Down
113 changes: 112 additions & 1 deletion spec/unpairedTags_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe("unpaired and empty tags", function() {
<unpaired>
<unpaired />
<unpaired>
<unpaired></unpaired>
<unpaired />
<unpaired>
</rootNode>`;

Expand Down Expand Up @@ -317,3 +317,114 @@ describe("unpaired and empty tags", function() {

});
});

describe("unpaired tas position", function() {
it(" when appears last in nested tag", function() {
const xmlData = `<root><a><u></a><b>w</b></root>`;
const expected = {
"root": {
"a": {
"u": "",
},
"b":"w"
}
};
const options = {
unpairedTags: ["u"]
};
const parser = new XMLParser(options);
// const parser = new XMLParser({ updateTag});
let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it(" when unpair then unpair self closed", function() {
const xmlData = `<root><v><v/><u></root>`;
const expected = {
"root": {
"v": ["",""],
"u": ""
}
}
const options = {
unpairedTags: ["u","v"]
};
const parser = new XMLParser(options);
// const parser = new XMLParser({ updateTag});
let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it("when unpair then unpair", function() {
const xmlData = `<root><v><v></root>`;
const expected = {
"root": {
"v": ["",""]
}
}
const options = {
unpairedTags: ["u","v"]
};
const parser = new XMLParser(options);
// const parser = new XMLParser({ updateTag});
let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it(" when 2 unpaired then unpaired self closed", function() {
const xmlData = `<root><v><v><v/></root>`;
const expected = {
"root": {
"v": ["","",""]
}
}
const options = {
unpairedTags: ["u","v"]
};
const parser = new XMLParser(options);
// const parser = new XMLParser({ updateTag});
let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it(" when unpaired followed by normal self closed", function() {
const xmlData = `<root><v><a/></root>`;
const expected = {
"root": {
"v": "",
"a": ""
}
}
const options = {
unpairedTags: ["u","v"]
};
const parser = new XMLParser(options);
// const parser = new XMLParser({ updateTag});
let result = parser.parse(xmlData);

// console.log(JSON.stringify(result,null,4));
expect(result).toEqual(expected);

});
it(" when unpaired is used as closing tag then it's error", function() {
const xmlData = `<root><v><a/></v></root>`;
const options = {
unpairedTags: ["u","v"]
};
const parser = new XMLParser(options);

expect(() =>{
parser.parse(xmlData);
}).toThrowError("Unpaired tag can not be used as closing tag: </v>")

});

});
5 changes: 4 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,13 @@ const parseXml = function(xmlData) {

//check if last tag of nested tag was unpaired tag
const lastTagName = jPath.substring(jPath.lastIndexOf(".")+1);
if(tagName && this.options.unpairedTags.indexOf(tagName) !== -1 ){
throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
}
let propIndex = 0
if(lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1 ){
propIndex = jPath.lastIndexOf('.', jPath.lastIndexOf('.')-1)
if(propIndex < 1) propIndex = jPath.lastIndexOf(".");
this.tagsNodeStack.pop();
}else{
propIndex = jPath.lastIndexOf(".");
}
Expand Down

0 comments on commit 6511e07

Please sign in to comment.