Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/j2x.js
#	src/x2j.js
#	test/j2x.spec.js
  • Loading branch information
Delagen committed Mar 15, 2018
2 parents cb41e13 + d076ea6 commit 165fc5c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 34 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
3.3.5 / 2018-03-15
* fix https://github.com/NaturalIntelligence/fast-xml-parser/issues/67 : attrNodeName invalid behavior
* fix: remove decodeHTML char condition
3.3.4 / 2018-03-14
* remove dependency on "he" package
* refactor code to separate methods in separate files.
* draft code for transforming XML to json string. It is not officially documented due to performance issue.
3.3.0 / 2018-03-05
* use common default options for XML parsing for consistency. And add `parseToNimn` method.
* update nexttodo
Expand Down
7 changes: 2 additions & 5 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var defaultOptions = {

function Parser(options) {
this.options = Object.assign({}, defaultOptions, options);
if (this.options.ignoreAttributes) {
if (this.options.ignoreAttributes || this.options.attrNodeName) {
this.isAttribute = function(/*a*/) { return false;};
} else {
this.attrPrefixLen = this.options.attributeNamePrefix.length;
Expand Down Expand Up @@ -855,7 +855,6 @@ var defaultOptions = {
parseAttributeValue: false,
arrayMode: false,
trimValues: true, //Trim string values of tag and attributes
//decodeHTMLchar: false,
cdataTagName: false,
cdataPositionChar: "\\c",
tagValueProcessor: a => a,
Expand Down Expand Up @@ -1001,9 +1000,7 @@ function buildAttributesMap(attrStr, options) {
if (options.trimValues) {
matches[i][4] = matches[i][4].trim();
}
if (options.decodeHTMLchar) {
matches[i][4] = options.attrValueProcessor(matches[i][4]);
}
matches[i][4] = options.attrValueProcessor(matches[i][4]);
attrs[options.attributeNamePrefix + attrName] = parseValue(matches[i][4], options.parseAttributeValue);
} else if (options.allowBooleanAttributes) {
attrs[options.attributeNamePrefix + attrName] = true;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast-xml-parser",
"version": "3.3.4",
"version": "3.3.5",
"description": "Validate XML or Parse XML to JS/JSON very fast without C/C++ based libraries",
"browser": "./parser.browser.js",
"main": "./parser.node.js",
Expand Down
31 changes: 15 additions & 16 deletions src/j2x.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,21 @@ const defaultOptions = {
attrValueProcessor: (a) => a
};

class Parser {
constructor(options) {
this.options = Object.assign({}, defaultOptions, options);
if (this.options.ignoreAttributes) {
this.isAttribute = (/*a*/) => false;
} else {
this.attrPrefixLen = this.options.attributeNamePrefix.length;
this.isAttribute = isAttribute;
}
if (this.options.cdataTagName) {
this.isCDATA = isCDATA;
} else {
this.isCDATA = (/*a*/) => false;
}
this.replaceCDATAstr = replaceCDATAstr;
this.replaceCDATAarr = replaceCDATAarr;
function Parser(options) {
this.options = Object.assign({}, defaultOptions, options);
if (this.options.ignoreAttributes || this.options.attrNodeName) {
this.isAttribute = function(/*a*/) { return false;};
} else {
this.attrPrefixLen = this.options.attributeNamePrefix.length;
this.isAttribute = isAttribute;
}
if (this.options.cdataTagName) {
this.isCDATA = isCDATA;
} else {
this.isCDATA = function(/*a*/) { return false;};
}
this.replaceCDATAstr = replaceCDATAstr;
this.replaceCDATAarr = replaceCDATAarr;

if (this.options.format) {
this.indentate = indentate;
Expand Down
7 changes: 2 additions & 5 deletions src/x2j.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const defaultOptions = {
parseAttributeValue: false,
arrayMode: false,
trimValues: true, //Trim string values of tag and attributes
//decodeHTMLchar: false,
cdataTagName: false,
cdataPositionChar: "\\c",
tagValueProcessor: (a) => a,
Expand Down Expand Up @@ -160,10 +159,8 @@ function buildAttributesMap(attrStr, options) {
if (options.trimValues) {
match[4] = match[4].trim();
}
if (options.decodeHTMLchar) {
match[4] = options.attrValueProcessor(match[4]);
}
attrs[options.attributeNamePrefix + attrName] = parseValue(match[4], options.parseAttributeValue);
matches[i][4] = options.attrValueProcessor(matches[i][4]);
attrs[options.attributeNamePrefix + attrName] = parseValue(matches[i][4], options.parseAttributeValue);
} else if (options.allowBooleanAttributes) {
attrs[options.attributeNamePrefix + attrName] = true;
}
Expand Down
56 changes: 49 additions & 7 deletions test/j2x.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {j2xParser: Parser} = require("../parser");
const he = require("he");

describe("XMLParser", function() {

it("should parse to XML with nested tags", function() {
const jObj = {
a: {
Expand Down Expand Up @@ -258,7 +259,8 @@ describe("XMLParser", function() {
it("should supress empty node to self closing node when parsing to XML", function() {
const jObj = {
a: {
"@": {
"notattr" : "val",
"@": {
b: "val>1",
c: "val<2"
},
Expand All @@ -280,17 +282,18 @@ describe("XMLParser", function() {
}
};
const parser = new Parser({
cdataTagName: "__cdata",
attrNodeName: "@",
encodeHTMLchar: true,
cdataTagName: "__cdata",
attributeNamePrefix: "",
attrNodeName: "@",
encodeHTMLchar: true,
supressEmptyNode: true,
tagValueProcessor: (a) => he.encode(a, {useNamedReferences: true}),
attrValueProcessor: (a) => he.encode(a, {isAttributeValue: isAttribute, useNamedReferences: true})
});
const result = parser.parse(jObj);
//console.log(result);
const expected = `<a b="val&gt;1" c="val&lt;2"><tag><k>34</k><g/><nested b="val&gt;1" c="val&lt;2"/></tag>text<![CDATA[this text is > from CDATA]]>value&gt;<![CDATA[]]></a>`;
expect(result).eql(expected);
const expected = `<a b="val&gt;1" c="val&lt;2"><notattr>val</notattr><tag><k>34</k><g/><nested b="val&gt;1" c="val&lt;2"/></tag>text<![CDATA[this text is > from CDATA]]>value&gt;<![CDATA[]]></a>`;
expect(result).toEqual(expected);
});

it("should format when parsing to XML", function() {
Expand Down Expand Up @@ -329,7 +332,46 @@ text<![CDATA[this text is > from CDATA]]>value&gt;<![CDATA[this is another text]
`;
//console.log(result);
//console.log(expected);
expect(result).eql(expected);
expect(result).equal(expected);
});


it("should format when parsing to XML", function() {
const jObj = {
root: {
element: {
$: {
aaa: "aaa",
bbb: "bbb"
},
_: 1
},
element2: {
$: {
aaa: "aaa2",
bbb: "bbb2"
},
subelement: {$: {aaa: "sub_aaa"}}
},
date:"test"
}
};
const parser = new Parser({
attributeNamePrefix: "",
attrNodeName: "$",
textNodeName: "_",
ignoreAttributes: false,
cdataTagName: "$cdata",
cdataPositionChar: "\\c",
format: false,
indentBy: "\t",
supressEmptyNode: true
});
const result = parser.parse(jObj);
const expected = '<root><element aaa="aaa" bbb="bbb">1</element><element2 aaa="aaa2" bbb="bbb2"><subelement aaa="sub_aaa"/></element2><date>test</date></root>';
//console.log(result);
//console.log(expected);
expect(result).equal(expected);
});

});

0 comments on commit 165fc5c

Please sign in to comment.