Skip to content

Commit 2485df3

Browse files
authored
Merge pull request #41 from Delagen/master
parse html entities in text values, fixes #40
2 parents c479b5e + 53fde91 commit 2485df3

File tree

5 files changed

+373
-20
lines changed

5 files changed

+373
-20
lines changed

lib/parser.js

Lines changed: 354 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"main": "./src/parser.js",
66
"scripts": {
77
"test": "jasmine spec/*spec.js",
8-
"bundle": "browserify src/parser.js --s parser > lib/parser.js",
8+
"bundle": "browserify src/parser.js -s parser > lib/parser.js",
99
"coverage": "node ./benchmark/perfTest.js; istanbul cover -x \"spec/*spec.js\" jasmine spec/*spec.js;",
1010
"coverage:check": "istanbul check-coverage --branch 90 --statement 90"
1111
},
12-
"src": {
12+
"bin": {
1313
"xml2js": "cli.js"
1414
},
1515
"repository": {
@@ -66,5 +66,8 @@
6666
"jasmine-core": "^2.8.0",
6767
"portfinder": "^1.0.13",
6868
"zombie": "^5.0.7"
69+
},
70+
"dependencies": {
71+
"he": "~1.1.1"
6972
}
7073
}

spec/cdata_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ describe("XMLParser", function () {
8686
var expected = {
8787
"xml": {
8888
"a": "text",
89-
"b": " text \n",
89+
"b": "\n text \n",
9090
"c": "text",
9191
"d": "text"
9292
}
@@ -141,4 +141,4 @@ describe("XMLParser", function () {
141141
expect(result).toBe(true);
142142
});
143143

144-
});
144+
});

spec/xmlParser_spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ describe("XMLParser", function () {
3333
});
3434

3535
it("should parse all values of attributes as string", function () {
36-
var xmlData = "<rootNode><tag int='045' float='65.34'>value</tag></rootNode>";
36+
var xmlData = "<rootNode><tag int='045' float='65.34' text='foo&ampbar&apos;'>value&amp;\r\n&apos;</tag></rootNode>";
3737
var expected = {
3838
"rootNode": {
3939
"tag": {
40-
"#text": "value",
40+
"#text": "value&\r\n'",
4141
"@_int": "045",
42-
"@_float": "65.34"
42+
"@_float": "65.34",
43+
"@_text": "foo&ampbar'"
4344
}
4445
}
4546
};

src/parser.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var he = require("he");
12
var getAllMatches = require("./util").getAllMatches;
23

34
var xmlNode = function(tagname,parent,val){
@@ -73,7 +74,7 @@ var getTraversalObj =function (xmlData,options){
7374
attrs[options.textNodeName] = val;
7475
childNode.val = attrs;
7576
}else{
76-
childNode.val = val;
77+
childNode.val = val;
7778
}
7879
currentNode.addChild(childNode);
7980
i++;
@@ -127,11 +128,13 @@ function resolveNameSpace(tagname,ignore){
127128
return tagname;
128129
}
129130

130-
function parseValue(val,conversion){
131+
function parseValue(val,conversion,isAttribute){
131132
if(val){
132133
if(!conversion || isNaN(val)){
133-
val = "" + val ;
134-
val = val.replace("\n"," ");
134+
val = "" + he.decode(val, {isAttributeValue:isAttribute, strict:true});
135+
if(isAttribute) {
136+
val = val.replace(/\r?\n/g, " ");
137+
}
135138
}else{
136139
if(val.indexOf(".") !== -1){
137140
if(parseFloat){
@@ -164,7 +167,7 @@ function buildAttributesArr(attrStr,ignore,prefix,attrNodeName,ignoreNS,conversi
164167
for (var i = 0; i < matches.length; i++) {
165168
var attrName = resolveNameSpace(matches[i][1],ignoreNS);
166169
if(attrName.length && attrName !== "xmlns") {
167-
attrs[prefix + attrName] = parseValue(matches[i][3], conversion);
170+
attrs[prefix + attrName] = parseValue(matches[i][3], conversion, true);
168171
}
169172
}
170173
if(!Object.keys(attrs).length){

0 commit comments

Comments
 (0)