Skip to content

Commit e25d8da

Browse files
committed
replace CRLF with single space for attribute value only
1 parent d847294 commit e25d8da

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-xml-parser",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "Validate XML or Parse XML to JS/JSON very fast without C/C++ based libraries",
55
"main": "./src/parser.js",
66
"scripts": {

spec/assets/mixed.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" standalone="yes" ?>
2+
<!--open the DOCTYPE declaration -
3+
the open square bracket indicates an internal DTD-->
4+
<!DOCTYPE foo [
5+
6+
7+
<!--define the internal DTD-->
8+
<!ELEMENT foo (#PCDATA)>
9+
<!--close the DOCTYPE declaration-->
10+
]>
11+
<ns:root xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
12+
<ptag attr="val" boolean>some data
13+
<nestedtag>
14+
nesteddata<!--single line comment-->
15+
</nestedtag>
16+
after
17+
</ptag>
18+
<ptag>before text
19+
<![CDATA[
20+
<nestedtag>
21+
nested cdata 1<!--single line comment-->
22+
</nestedtag>
23+
]]>middle
24+
<![CDATA[
25+
<nestedtag>
26+
nested cdata 2<!--multi line
27+
comment-->
28+
</nestedtag>
29+
]]>after
30+
<![CDATA[
31+
<nestedtag>
32+
nested cdata 3
33+
</nestedtag>
34+
]]>end
35+
</ptag>
36+
</ns:root>

spec/attr_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("XMLParser", function () {
7373
});
7474

7575
it("should decode HTML entities / char", function () {
76-
var xmlData = '<element id="7" data="foo\nbar" bug="foo&ampbar&apos;"/>';
76+
var xmlData = '<element id="7" data="foo\r\nbar" bug="foo&ampbar&apos;"/>';
7777
var expected = {
7878
"element": {
7979
"id" : 7,

spec/cdata_spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,34 @@ describe("XMLParser", function () {
189189
expect(result).toEqual(expected);
190190
});
191191

192+
it("should validate XML with repeated multiline CDATA and comments", function () {
193+
var fs = require("fs");
194+
var path = require("path");
195+
var fileNamePath = path.join(__dirname, "assets/mixed.xml");
196+
var xmlData = fs.readFileSync(fileNamePath).toString();
197+
198+
199+
var expected = {
200+
"ns:root": {
201+
"ptag": [
202+
{
203+
"nestedtag": "nesteddata",
204+
"@_attr": "val",
205+
"@_boolean": true,
206+
"#text": "some dataafter"
207+
},
208+
"before text\n <nestedtag>\n nested cdata 1\n </nestedtag>\n middle\n <nestedtag>\n nested cdata 2\n </nestedtag>\n after\n <nestedtag>\n nested cdata 3\n </nestedtag>\n end"
209+
],
210+
"@_xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/"
211+
}
212+
};
213+
214+
var result = parser.parse(xmlData,{
215+
ignoreAttributes:false,
216+
allowBooleanAttributes:true
217+
});
218+
//console.log(JSON.stringify(result,null,4));
219+
expect(result).toEqual(expected);
220+
});
221+
192222
});

src/parser.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ var buildOptions = function (options){
4848

4949
var getTraversalObj =function (xmlData,options){
5050
options = buildOptions(options);
51-
xmlData = xmlData.replace(/\r?\n/g, " ");//make it single line
52-
xmlData = xmlData.replace(/<!--.*?-->/g, "");//Remove comments
51+
//xmlData = xmlData.replace(/\r?\n/g, " ");//make it single line
52+
xmlData = xmlData.replace(/<!--[\s\S]*?-->/g, "");//Remove comments
5353

5454
var xmlObj = new xmlNode('!xml');
5555
var currentNode = xmlObj;
5656

57-
var tagsRegx = new RegExp("<((!\\[CDATA\\[(.*?)(\\]\\]>))|((\\w*:)?([\\w:\\-\\._]+))([^>]*)>|((\\/)((\\w*:)?([\\w:\\-\\._]+))>))([^<]*)","g");
57+
var tagsRegx = new RegExp("<((!\\[CDATA\\[([\\s\\S]*?)(\\]\\]>))|((\\w*:)?([\\w:\\-\\._]+))([^>]*)>|((\\/)((\\w*:)?([\\w:\\-\\._]+))>))([^<]*)","g");
5858
var tag = tagsRegx.exec(xmlData);
5959
var nextTag = tagsRegx.exec(xmlData);
6060
var previousMatch,nextMatch;
@@ -167,6 +167,7 @@ function parseValue(val,shouldParse){
167167
var attrsRegx = new RegExp("([^\\s=]+)\\s*(=\\s*(['\"])(.*?)\\3)?","g");
168168
function buildAttributesMap(attrStr,options){
169169
if( !options.ignoreAttributes && typeof attrStr === "string" ){
170+
attrStr = attrStr.replace(/\r?\n/g, " ");
170171
//attrStr = attrStr || attrStr.trim();
171172

172173
var matches = util.getAllMatches(attrStr,attrsRegx);

src/validator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ function readAttributeStr(xmlData,i){
213213
/**
214214
* Select all the attributes whether valid or invalid.
215215
*/
216-
var validAttrStrRegxp = new RegExp("(\\s*)([^\\s=]+)(\\s*=)?(\\s*(['\"])((.|\\n)*?)\\5)?", "g");
216+
var validAttrStrRegxp = new RegExp("(\\s*)([^\\s=]+)(\\s*=)?(\\s*(['\"])(([\\s\\S])*?)\\5)?", "g");
217217

218218
//attr, ="sd", a="amit's", a="sd"b="saf", ab cd=""
219219

0 commit comments

Comments
 (0)