Skip to content

Commit 4c35142

Browse files
committed
parse html entities in text values, fixes #40
1 parent 749a918 commit 4c35142

File tree

4 files changed

+364
-12
lines changed

4 files changed

+364
-12
lines changed

bin/parser.js

Lines changed: 4 additions & 3 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){
@@ -129,10 +130,10 @@ function resolveNameSpace(tagname,ignore){
129130
return tagname;
130131
}
131132

132-
function parseValue(val,conversion){
133+
function parseValue(val,conversion,isAttribute){
133134
if(val){
134135
if(!conversion || isNaN(val)){
135-
val = "" + val ;
136+
val = "" + he.decode(val, {isAttributeValue:isAttribute, strict:true});
136137
}else{
137138
if(val.indexOf(".") !== -1){
138139
if(parseFloat){
@@ -165,7 +166,7 @@ function buildAttributesArr(attrStr,ignore,prefix,attrNodeName,ignoreNS,conversi
165166
for (var i = 0; i < matches.length; i++) {
166167
var attrName = resolveNameSpace(matches[i][1],ignoreNS);
167168
if(attrName.length && attrName !== "xmlns") {
168-
attrs[prefix + attrName] = parseValue(matches[i][3], conversion);
169+
attrs[prefix + attrName] = parseValue(matches[i][3], conversion, true);
169170
}
170171
}
171172
if(!Object.keys(attrs).length){

lib/parser.js

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

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "./bin/parser.js",
66
"scripts": {
77
"test": "jasmine spec/*spec.js",
8-
"bundle": "browserify bin/parser.js --s parser > lib/parser.js",
8+
"bundle": "browserify bin/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
},
@@ -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/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
};

0 commit comments

Comments
 (0)