2
2
3
3
/**
4
4
* find paired tag for a stop node
5
- * @param {string } xmlDoc
6
- * @param {string } tagName
5
+ * @param {string } xmlDoc
6
+ * @param {string } tagName
7
7
* @param {number } i : start index
8
8
*/
9
9
function readStopNode ( xmlDoc , tagName , i ) {
10
10
const startIndex = i ;
11
11
// Starting at 1 since we already have an open tag
12
12
let openTagCount = 1 ;
13
-
13
+
14
14
for ( ; i < xmlDoc . length ; i ++ ) {
15
- if ( xmlDoc [ i ] === "<" ) {
15
+ if ( xmlDoc [ i ] === "<" ) {
16
16
if ( xmlDoc [ i + 1 ] === "/" ) { //close tag
17
17
const closeIndex = findSubStrIndex ( xmlDoc , ">" , i , `${ tagName } is not closed` ) ;
18
18
let closeTagName = xmlDoc . substring ( i + 2 , closeIndex ) . trim ( ) ;
@@ -26,18 +26,18 @@ function readStopNode(xmlDoc, tagName, i){
26
26
}
27
27
}
28
28
i = closeIndex ;
29
- } else if ( xmlDoc [ i + 1 ] === '?' ) {
29
+ } else if ( xmlDoc [ i + 1 ] === '?' ) {
30
30
const closeIndex = findSubStrIndex ( xmlDoc , "?>" , i + 1 , "StopNode is not closed." )
31
31
i = closeIndex ;
32
- } else if ( xmlDoc . substr ( i + 1 , 3 ) === '!--' ) {
32
+ } else if ( xmlDoc . substr ( i + 1 , 3 ) === '!--' ) {
33
33
const closeIndex = findSubStrIndex ( xmlDoc , "-->" , i + 3 , "StopNode is not closed." )
34
34
i = closeIndex ;
35
- } else if ( xmlDoc . substr ( i + 1 , 2 ) === '![' ) {
35
+ } else if ( xmlDoc . substr ( i + 1 , 2 ) === '![' ) {
36
36
const closeIndex = findSubStrIndex ( xmlDoc , "]]>" , i , "StopNode is not closed." ) - 2 ;
37
37
i = closeIndex ;
38
38
} else {
39
39
const tagData = readTagExp ( xmlDoc , i , '>' )
40
-
40
+
41
41
if ( tagData ) {
42
42
const openTagName = tagData && tagData . tagName ;
43
43
if ( openTagName === tagName && tagData . tagExp [ tagData . tagExp . length - 1 ] !== "/" ) {
@@ -52,7 +52,7 @@ function readStopNode(xmlDoc, tagName, i){
52
52
53
53
/**
54
54
* Read closing tag name
55
- * @param {Source } source
55
+ * @param {Source } source
56
56
* @returns tag name
57
57
*/
58
58
function readClosingTagName ( source ) {
@@ -73,9 +73,9 @@ function readClosingTagName(source){
73
73
* This function can be used to read normal tag, pi tag.
74
74
* This function can't be used to read comment, CDATA, DOCTYPE.
75
75
* Eg <tag attr = ' some"' attr= ">" bool>
76
- * @param {string } xmlDoc
76
+ * @param {string } xmlDoc
77
77
* @param {number } startIndex starting index
78
- * @returns tag expression includes tag name & attribute string
78
+ * @returns tag expression includes tag name & attribute string
79
79
*/
80
80
function readTagExp ( parser ) {
81
81
let inSingleQuotes = false ;
@@ -100,8 +100,8 @@ function readTagExp(parser) {
100
100
if ( inSingleQuotes || inDoubleQuotes ) {
101
101
throw new Error ( "Invalid attribute expression. Quote is not properly closed" ) ;
102
102
} else if ( ! EOE ) throw new Error ( "Unexpected closing of source. Waiting for '>'" ) ;
103
-
104
-
103
+
104
+
105
105
const exp = parser . source . readStr ( i ) ;
106
106
parser . source . updateBufferBoundary ( i + 1 ) ;
107
107
return buildTagExpObj ( exp , parser )
@@ -133,7 +133,7 @@ function readPiExp(parser) {
133
133
if ( inSingleQuotes || inDoubleQuotes ) {
134
134
throw new Error ( "Invalid attribute expression. Quote is not properly closed in PI tag expression" ) ;
135
135
} else if ( ! EOE ) throw new Error ( "Unexpected closing of source. Waiting for '?>'" ) ;
136
-
136
+
137
137
if ( ! parser . options . attributes . ignore ) {
138
138
//TODO: use regex to verify attributes if not set to ignore
139
139
}
@@ -150,7 +150,11 @@ function buildTagExpObj(exp, parser){
150
150
} ;
151
151
let attrsExp = "" ;
152
152
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
+ }
154
158
155
159
//separate tag name
156
160
let i = 0 ;
@@ -182,7 +186,7 @@ function parseAttributesExp(attrStr, parser) {
182
186
for ( let i = 0 ; i < len ; i ++ ) {
183
187
let attrName = parser . processAttrName ( matches [ i ] [ 1 ] ) ;
184
188
let attrVal = parser . replaceEntities ( matches [ i ] [ 4 ] || true ) ;
185
-
189
+
186
190
parser . outputBuilder . addAttribute ( attrName , attrVal ) ;
187
191
}
188
192
}
@@ -209,4 +213,4 @@ module.exports = {
209
213
readClosingTagName : readClosingTagName ,
210
214
readTagExp : readTagExp ,
211
215
readPiExp : readPiExp ,
212
- }
216
+ }
0 commit comments