Skip to content

Commit

Permalink
Add transformTagName option to transform tag names when parsing (#469)
Browse files Browse the repository at this point in the history
  • Loading branch information
erkie authored Jul 6, 2022
1 parent 2203711 commit 6ad40ee
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
36 changes: 36 additions & 0 deletions spec/transform_tagname_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

const {XMLParser} = require("../src/fxp");

describe("XMLParser", function() {
it("should parse lowercase tagnames", function() {
const xmlData = `<?xml version='1.0'?>
<root>
<person>Person 1</person>
<Person>Person 2</Person>
<PERSON>Person 3</PERSON>
<person>Person 4</person>
</root>
`;

const parser = new XMLParser({
// transformTagName: (tagName) => tagName
ignoreDeclaration: true,
transformTagName: (tagName) => tagName.toLowerCase()
});
let result = parser.parse(xmlData);

const expected = {
"root": {
"person": [
"Person 1",
"Person 2",
"Person 3",
"Person 4"
]
}
};

expect(result).toEqual(expected);
});
});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type X2jOptions = {
htmlEntities: boolean;
ignoreDeclaration: boolean;
ignorePiTags: boolean;
transformTagName: ((tagName: string) => string) | false;
};
type strnumOptions = {
hex: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
{ regex: new RegExp("\"", "g"), val: "&quot;" }
],
processEntities: true,
stopNodes: []
stopNodes: [],
transformTagName: false,
};

function Builder(options) {
Expand Down
3 changes: 2 additions & 1 deletion src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
processEntities: true,
htmlEntities: false,
ignoreDeclaration: false,
ignorePiTags: false
ignorePiTags: false,
transformTagName: false,
};

const buildOptions = function(options) {
Expand Down
13 changes: 12 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
}
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

if(currentNode){
textData = this.saveTextToParentTag(textData, currentNode, jPath);
}
Expand Down Expand Up @@ -257,12 +261,15 @@ const parseXml = function(xmlData) {

i = closeIndex + 2;
}else {//Opening tag

let result = readTagExp(xmlData,i, this. options.removeNSPrefix);
let tagName= result.tagName;
let tagExp = result.tagExp;
let attrExpPresent = result.attrExpPresent;
let closeIndex = result.closeIndex;

if (this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

//save text as child node
if (currentNode && textData) {
Expand Down Expand Up @@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
}else{
tagExp = tagExp.substr(0, tagExp.length - 1);
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

const childNode = new xmlNode(tagName);
if(tagName !== tagExp && attrExpPresent){
Expand Down

0 comments on commit 6ad40ee

Please sign in to comment.