Skip to content

Commit 057cd7e

Browse files
committed
Revert "Fix #167: '>' in attribute value"
This reverts commit b46db1b.
1 parent 67649c8 commit 057cd7e

File tree

2 files changed

+14
-51
lines changed

2 files changed

+14
-51
lines changed

spec/attr_spec.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -132,39 +132,6 @@ describe("XMLParser", function() {
132132
expect(result).toBe(true);
133133
});
134134

135-
it("should parse an attribute with '>' ", function() {
136-
const xmlData = `<element id="7>" str="" data><selfclosing/><selfclosing /><selfclosingwith attr/></element>`;
137-
const expected = {
138-
"element": {
139-
"id": '7>',
140-
"str": "",
141-
"data": true,
142-
"selfclosing": [
143-
"",
144-
""
145-
],
146-
"selfclosingwith": {
147-
"attr": true
148-
}
149-
}
150-
};
151-
152-
let result = parser.parse(xmlData, {
153-
attributeNamePrefix: "",
154-
ignoreAttributes: false,
155-
parseAttributeValue: true,
156-
allowBooleanAttributes: true
157-
});
158-
159-
//console.log(JSON.stringify(result,null,4));
160-
expect(result).toEqual(expected);
161-
162-
result = validator.validate(xmlData, {
163-
allowBooleanAttributes: true
164-
});
165-
expect(result).toBe(true);
166-
});
167-
168135
it("should not remove xmlns when namespaces are not set to be ignored", function() {
169136
const xmlData = `<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"></project>`;
170137
const expected = {

src/xmlstr2xmlnode.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ const util = require('./util');
44
const buildOptions = require('./util').buildOptions;
55
const xmlNode = require('./xmlNode');
66
const TagType = {OPENING: 1, CLOSING: 2, SELF: 3, CDATA: 4};
7-
const attrstr_regex = '((\\s*[\\w\\-._:]+(=((\'([^\']*)\')|("([^"]*)")))?)*)\\s*';
8-
let regx = '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|(([\\w:\\-._]*:)?([\\w:\\-._]+))'+attrstr_regex+'(\\/)?>|((\\/)(([\\w:\\-._]*:)?([\\w:\\-._]+))\\s*>))([^<]*)';
9-
//'<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|(([\\w:\\-._]*:)?([\\w:\\-._]+))([^>]*)>|((\\/)(([\\w:\\-._]*:)?([\\w:\\-._]+))\\s*>))([^<]*)';
10-
7+
let regx =
8+
'<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|(([\\w:\\-._]*:)?([\\w:\\-._]+))([^>]*)>|((\\/)(([\\w:\\-._]*:)?([\\w:\\-._]+))\\s*>))([^<]*)';
119

1210
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
1311
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
@@ -81,14 +79,12 @@ const getTraversalObj = function(xmlData, options) {
8179
let tag = tagsRegx.exec(xmlData);
8280
let nextTag = tagsRegx.exec(xmlData);
8381
while (tag) {
84-
//console.log(tag)
8582
const tagType = checkForTagType(tag);
86-
//console.log(tagType)
8783

8884
if (tagType === TagType.CLOSING) {
8985
//add parsed data to parent node
90-
if (currentNode.parent && tag[22]) {
91-
currentNode.parent.val = util.getValue(currentNode.parent.val) + '' + processTagValue(tag[22], options);
86+
if (currentNode.parent && tag[14]) {
87+
currentNode.parent.val = util.getValue(currentNode.parent.val) + '' + processTagValue(tag[14], options);
9288
}
9389
if (options.stopNodes.length && options.stopNodes.includes(currentNode.tagname)) {
9490
currentNode.child = []
@@ -105,29 +101,29 @@ const getTraversalObj = function(xmlData, options) {
105101
//for backtracking
106102
currentNode.val = util.getValue(currentNode.val) + options.cdataPositionChar;
107103
//add rest value to parent node
108-
if (tag[22]) {
109-
currentNode.val += processTagValue(tag[22], options);
104+
if (tag[14]) {
105+
currentNode.val += processTagValue(tag[14], options);
110106
}
111107
} else {
112-
currentNode.val = (currentNode.val || '') + (tag[3] || '') + processTagValue(tag[22], options);
108+
currentNode.val = (currentNode.val || '') + (tag[3] || '') + processTagValue(tag[14], options);
113109
}
114110
} else if (tagType === TagType.SELF) {
115-
if (currentNode && tag[22]) {
116-
currentNode.val = util.getValue(currentNode.val) + '' + processTagValue(tag[22], options);
111+
if (currentNode && tag[14]) {
112+
currentNode.val = util.getValue(currentNode.val) + '' + processTagValue(tag[14], options);
117113
}
118114

119115
const childNode = new xmlNode(options.ignoreNameSpace ? tag[7] : tag[5], currentNode, '');
120-
/* if (tag[8] && tag[8].length > 0) {
116+
if (tag[8] && tag[8].length > 0) {
121117
tag[8] = tag[8].substr(0, tag[8].length - 1);
122-
} */
118+
}
123119
childNode.attrsMap = buildAttributesMap(tag[8], options);
124120
currentNode.addChild(childNode);
125121
} else {
126122
//TagType.OPENING
127123
const childNode = new xmlNode(
128124
options.ignoreNameSpace ? tag[7] : tag[5],
129125
currentNode,
130-
processTagValue(tag[22], options)
126+
processTagValue(tag[14], options)
131127
);
132128
if (options.stopNodes.length && options.stopNodes.includes(childNode.tagname)) {
133129
childNode.startIndex=tag.index + tag[1].length
@@ -159,9 +155,9 @@ function processTagValue(val, options) {
159155
function checkForTagType(match) {
160156
if (match[4] === ']]>') {
161157
return TagType.CDATA;
162-
} else if (match[18] === '/') {
158+
} else if (match[10] === '/') {
163159
return TagType.CLOSING;
164-
} else if (match[16] === '/') {
160+
} else if (typeof match[8] !== 'undefined' && match[8].substr(match[8].length - 1) === '/') {
165161
return TagType.SELF;
166162
} else {
167163
return TagType.OPENING;

0 commit comments

Comments
 (0)