Skip to content

Commit 4082902

Browse files
authored
Fix empty tag key name for v5 (#697)
no impact on v4
1 parent eb8aa89 commit 4082902

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/v5/XmlPartReader.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
/**
44
* find paired tag for a stop node
5-
* @param {string} xmlDoc
6-
* @param {string} tagName
5+
* @param {string} xmlDoc
6+
* @param {string} tagName
77
* @param {number} i : start index
88
*/
99
function readStopNode(xmlDoc, tagName, i){
1010
const startIndex = i;
1111
// Starting at 1 since we already have an open tag
1212
let openTagCount = 1;
13-
13+
1414
for (; i < xmlDoc.length; i++) {
15-
if( xmlDoc[i] === "<"){
15+
if( xmlDoc[i] === "<"){
1616
if (xmlDoc[i+1] === "/") {//close tag
1717
const closeIndex = findSubStrIndex(xmlDoc, ">", i, `${tagName} is not closed`);
1818
let closeTagName = xmlDoc.substring(i+2,closeIndex).trim();
@@ -26,18 +26,18 @@ function readStopNode(xmlDoc, tagName, i){
2626
}
2727
}
2828
i=closeIndex;
29-
} else if(xmlDoc[i+1] === '?') {
29+
} else if(xmlDoc[i+1] === '?') {
3030
const closeIndex = findSubStrIndex(xmlDoc, "?>", i+1, "StopNode is not closed.")
3131
i=closeIndex;
32-
} else if(xmlDoc.substr(i + 1, 3) === '!--') {
32+
} else if(xmlDoc.substr(i + 1, 3) === '!--') {
3333
const closeIndex = findSubStrIndex(xmlDoc, "-->", i+3, "StopNode is not closed.")
3434
i=closeIndex;
35-
} else if(xmlDoc.substr(i + 1, 2) === '![') {
35+
} else if(xmlDoc.substr(i + 1, 2) === '![') {
3636
const closeIndex = findSubStrIndex(xmlDoc, "]]>", i, "StopNode is not closed.") - 2;
3737
i=closeIndex;
3838
} else {
3939
const tagData = readTagExp(xmlDoc, i, '>')
40-
40+
4141
if (tagData) {
4242
const openTagName = tagData && tagData.tagName;
4343
if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
@@ -52,7 +52,7 @@ function readStopNode(xmlDoc, tagName, i){
5252

5353
/**
5454
* Read closing tag name
55-
* @param {Source} source
55+
* @param {Source} source
5656
* @returns tag name
5757
*/
5858
function readClosingTagName(source){
@@ -73,9 +73,9 @@ function readClosingTagName(source){
7373
* This function can be used to read normal tag, pi tag.
7474
* This function can't be used to read comment, CDATA, DOCTYPE.
7575
* Eg <tag attr = ' some"' attr= ">" bool>
76-
* @param {string} xmlDoc
76+
* @param {string} xmlDoc
7777
* @param {number} startIndex starting index
78-
* @returns tag expression includes tag name & attribute string
78+
* @returns tag expression includes tag name & attribute string
7979
*/
8080
function readTagExp(parser) {
8181
let inSingleQuotes = false;
@@ -100,8 +100,8 @@ function readTagExp(parser) {
100100
if(inSingleQuotes || inDoubleQuotes){
101101
throw new Error("Invalid attribute expression. Quote is not properly closed");
102102
}else if(!EOE) throw new Error("Unexpected closing of source. Waiting for '>'");
103-
104-
103+
104+
105105
const exp = parser.source.readStr(i);
106106
parser.source.updateBufferBoundary(i + 1);
107107
return buildTagExpObj(exp, parser)
@@ -133,7 +133,7 @@ function readPiExp(parser) {
133133
if(inSingleQuotes || inDoubleQuotes){
134134
throw new Error("Invalid attribute expression. Quote is not properly closed in PI tag expression");
135135
}else if(!EOE) throw new Error("Unexpected closing of source. Waiting for '?>'");
136-
136+
137137
if(!parser.options.attributes.ignore){
138138
//TODO: use regex to verify attributes if not set to ignore
139139
}
@@ -150,7 +150,11 @@ function buildTagExpObj(exp, parser){
150150
};
151151
let attrsExp = "";
152152

153-
if(exp[exp.length -1] === "/") tagExp.selfClosing = true;
153+
// Check for self-closing tag before setting the name
154+
if(exp[exp.length -1] === "/") {
155+
tagExp.selfClosing = true;
156+
exp = exp.slice(0, -1); // Remove the trailing slash
157+
}
154158

155159
//separate tag name
156160
let i = 0;
@@ -182,7 +186,7 @@ function parseAttributesExp(attrStr, parser) {
182186
for (let i = 0; i < len; i++) {
183187
let attrName = parser.processAttrName(matches[i][1]);
184188
let attrVal = parser.replaceEntities(matches[i][4] || true);
185-
189+
186190
parser.outputBuilder.addAttribute(attrName, attrVal);
187191
}
188192
}
@@ -209,4 +213,4 @@ module.exports = {
209213
readClosingTagName: readClosingTagName,
210214
readTagExp: readTagExp,
211215
readPiExp: readPiExp,
212-
}
216+
}

0 commit comments

Comments
 (0)