Skip to content

Commit decf141

Browse files
committed
fix: allow a tag to be separated by \n\t chars
1 parent f0bf79e commit decf141

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

spec/data_spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe("XMLParser", function() {
194194
expect(result).toEqual(expected);
195195
});
196196

197-
xit("should trim \t or \n chars", function() {
197+
it("should trim \t or \n chars", function() {
198198
const xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
199199
"<MPD\n" +
200200
"\tavailabilityStartTime=\"2020-02-16T10:52:03.119Z\"\n" +
@@ -204,19 +204,17 @@ describe("XMLParser", function() {
204204
"\t</Period>\n" +
205205
"</MPD>";
206206
const expected = {
207-
"MPD": [
208-
{
207+
"MPD": {
209208
"$": {
210209
"availabilityStartTime": "2020-02-16T10:52:03.119Z",
211-
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
210+
"xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
212211
},
213212
"Period": {
214213
"$": {
215214
"id": "1578477220"
216215
}
217216
}
218-
}
219-
]
217+
}
220218
}
221219

222220
const result = parser.parse(xmlData, {
@@ -225,7 +223,7 @@ describe("XMLParser", function() {
225223
attrNodeName:"$",
226224
attributeNamePrefix : "" //TODO attr node prefix should not set when they're grouped
227225
});
228-
console.log(JSON.stringify(result,null,4));
226+
//console.log(JSON.stringify(result,null,4));
229227
expect(result).toEqual(expected);
230228
});
231229

src/xmlstr2xmlnode.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@ function buildAttributesMap(attrStr, options) {
168168
}
169169

170170
const getTraversalObj = function(xmlData, options) {
171+
xmlData = xmlData.replace(/(\r\n)|\n/, " ");
171172
options = buildOptions(options, defaultOptions, props);
172173
const xmlObj = new xmlNode('!xml');
173174
let currentNode = xmlObj;
174175
let textData = "";
176+
175177
//function match(xmlData){
176178
for(let i=0; i< xmlData.length; i++){
177179
const ch = xmlData[i];
@@ -246,9 +248,9 @@ const getTraversalObj = function(xmlData, options) {
246248

247249
i = closeIndex + 2;
248250
}else {//Opening tag
249-
const closeIndex = closingIndexForOpeningTag(xmlData, i)
250-
//const closeIndex = xmlData.indexOf(">",i);
251-
let tagExp = xmlData.substring(i + 1,closeIndex);
251+
const result = closingIndexForOpeningTag(xmlData, i+1)
252+
let tagExp = result.data;
253+
const closeIndex = result.index;
252254
const separatorIndex = tagExp.indexOf(" ");
253255
let tagName = tagExp;
254256
if(separatorIndex !== -1){
@@ -308,15 +310,22 @@ const getTraversalObj = function(xmlData, options) {
308310

309311
function closingIndexForOpeningTag(data, i){
310312
let attrBoundary;
313+
let tagExp = "";
311314
for (let index = i; index < data.length; index++) {
312315
let ch = data[index];
313316
if (attrBoundary) {
314317
if (ch === attrBoundary) attrBoundary = "";//reset
315318
} else if (ch === '"' || ch === "'") {
316319
attrBoundary = ch;
317320
} else if (ch === '>') {
318-
return index
321+
return {
322+
data: tagExp,
323+
index: index
324+
}
325+
} else if (ch === '\t') {
326+
ch = " "
319327
}
328+
tagExp += ch;
320329
}
321330
}
322331

0 commit comments

Comments
 (0)