Skip to content

Commit

Permalink
call tagValueProcessor for stop node
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Dec 13, 2021
1 parent cd99804 commit e8c323d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
54 changes: 54 additions & 0 deletions spec/stopNodes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,58 @@ describe("XMLParser StopNodes", function() {
});
expect(result).toBe(true);
});

it("should call tagValueProcessor for stop Node", function() {
const XMLdata = `
<products capture-installed="true">
<script/>
<product>
<pid>8</pid>
<modelno>6273033</modelno>
<name>
<![CDATA[ Big Red Truck ]]>
</name>
<category>
<![CDATA[ Toys]]>
</category>
<currency>USD</currency>
<price>
<actualprice>19.20</actualprice>
</price>
</product>
`;

const expected = {
"products": {
"script": "",
"product": {
"pid": "8",
"modelno": "6273033",
"name": " Big Red Truck ",
"category": " Toys",
"currency": "USD",
"price": "19.20"
}
}
};

const options = {
ignoreAttributes: true,
stopNodes: [
"products.product.price"
],
tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {
if(jPath === 'products.product.price'){
// console.log(tagName, tagValue, jPath, hasAttributes, isLeafNode);
return /([0-9]+\.[0-9]+)/.exec(tagValue)[1]
}
},
// preserveOrder: true,
};
const parser = new XMLParser(options);
let result = parser.parse(XMLdata);
// console.log(JSON.stringify(result, null,4));

expect(expected).toEqual(result);
});
});
9 changes: 7 additions & 2 deletions src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ function addExternalEntities(externalEntities){
* @param {boolean} dontTrim
* @param {boolean} hasAttributes
* @param {boolean} isLeafNode
* @param {boolean} escapeEntities
*/
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode) {
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
if (val !== undefined) {
if (this.options.trimValues && !dontTrim) {
val = val.trim();
}
if(val.length > 0){
val = this.replaceEntitiesValue(val);
if(!escapeEntities) val = this.replaceEntitiesValue(val);

const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
if(newval === null || newval === undefined){
Expand Down Expand Up @@ -292,6 +293,10 @@ const parseXml = function(xmlData) {
if(tagName !== tagExp && attrExpPresent){
childNode[":@"] = this.buildAttributesMap(tagExp, jPath);
}
if(tagContent) {
tagContent = this.parseTextData(tagContent, tagName, jPath, true, attrExpPresent, true, true);
}

jPath = jPath.substr(0, jPath.lastIndexOf("."));
childNode.add(this.options.textNodeName, tagContent);

Expand Down

0 comments on commit e8c323d

Please sign in to comment.