@@ -25,7 +25,7 @@ import { XmlOutputOptions } from './xml-output-options';
25
25
* @param disallowBrowserSpecificOptimization A boolean, to avoid browser optimization.
26
26
* @returns The XML value as a string.
27
27
*/
28
- export function xmlValue ( node : any , disallowBrowserSpecificOptimization : boolean = false ) : string {
28
+ export function xmlValue ( node : XNode | any , disallowBrowserSpecificOptimization : boolean = false ) : string {
29
29
if ( ! node ) {
30
30
return '' ;
31
31
}
@@ -42,25 +42,28 @@ export function xmlValue(node: any, disallowBrowserSpecificOptimization: boolean
42
42
case DOM_DOCUMENT_NODE :
43
43
case DOM_DOCUMENT_FRAGMENT_NODE :
44
44
if ( ! disallowBrowserSpecificOptimization ) {
45
- // IE, Safari, Opera, and friends
45
+ // Only returns something if node has either `innerText` or `textContent` (not an XNode).
46
+ // IE, Safari, Opera, and friends (`innerText`)
46
47
const innerText = node . innerText ;
47
48
if ( innerText != undefined ) {
48
49
return innerText ;
49
50
}
50
- // Firefox
51
+ // Firefox (`textContent`)
51
52
const textContent = node . textContent ;
52
53
if ( textContent != undefined ) {
53
54
return textContent ;
54
55
}
55
56
}
56
57
57
58
if ( node . transformedChildNodes . length > 0 ) {
58
- for ( let i = 0 ; i < node . transformedChildNodes . length ; ++ i ) {
59
- ret += xmlValue ( node . transformedChildNodes [ i ] ) ;
59
+ const transformedTextNodes = node . transformedChildNodes . filter ( ( n : XNode ) => n . nodeType !== DOM_ATTRIBUTE_NODE ) ;
60
+ for ( let i = 0 ; i < transformedTextNodes . length ; ++ i ) {
61
+ ret += xmlValue ( transformedTextNodes [ i ] ) ;
60
62
}
61
63
} else {
62
- for ( let i = 0 ; i < node . childNodes . length ; ++ i ) {
63
- ret += xmlValue ( node . childNodes [ i ] ) ;
64
+ const textNodes = node . childNodes . filter ( ( n : XNode ) => n . nodeType !== DOM_ATTRIBUTE_NODE ) ;
65
+ for ( let i = 0 ; i < textNodes . length ; ++ i ) {
66
+ ret += xmlValue ( textNodes [ i ] ) ;
64
67
}
65
68
}
66
69
@@ -96,7 +99,7 @@ export function xmlValue2(node: any, disallowBrowserSpecificOptimization: boolea
96
99
return textContent ;
97
100
}
98
101
}
99
- // pobrecito!
102
+
100
103
const len = node . transformedChildNodes . length ;
101
104
for ( let i = 0 ; i < len ; ++ i ) {
102
105
ret += xmlValue ( node . transformedChildNodes [ i ] ) ;
@@ -137,10 +140,15 @@ function xmlTextRecursive(node: XNode, buffer: string[], options: XmlOutputOptio
137
140
buffer . push ( `<!--${ node . nodeValue } -->` ) ;
138
141
} else if ( node . nodeType == DOM_ELEMENT_NODE ) {
139
142
buffer . push ( `<${ xmlFullNodeName ( node ) } ` ) ;
140
- for ( let i = 0 ; i < node . attributes . length ; ++ i ) {
141
- const a = node . attributes [ i ] ;
142
- if ( a && a . nodeName && a . nodeValue ) {
143
- buffer . push ( ` ${ xmlFullNodeName ( a ) } ="${ xmlEscapeAttr ( a . nodeValue ) } "` ) ;
143
+
144
+ for ( let i = 0 ; i < node . childNodes . length ; ++ i ) {
145
+ const childNode = node . childNodes [ i ] ;
146
+ if ( ! childNode || childNode . nodeType !== DOM_ATTRIBUTE_NODE ) {
147
+ continue ;
148
+ }
149
+
150
+ if ( childNode . nodeName && childNode . nodeValue ) {
151
+ buffer . push ( ` ${ xmlFullNodeName ( childNode ) } ="${ xmlEscapeAttr ( childNode . nodeValue ) } "` ) ;
144
152
}
145
153
}
146
154
@@ -233,7 +241,11 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], options: XmlOut
233
241
function xmlElementLogicTrivial ( node : XNode , buffer : string [ ] , options : XmlOutputOptions ) {
234
242
buffer . push ( `<${ xmlFullNodeName ( node ) } ` ) ;
235
243
236
- const attributes = node . transformedAttributes || node . attributes ;
244
+ let attributes = node . transformedChildNodes . filter ( n => n . nodeType === DOM_ATTRIBUTE_NODE ) ;
245
+ if ( attributes . length === 0 ) {
246
+ attributes = node . childNodes . filter ( n => n . nodeType === DOM_ATTRIBUTE_NODE ) ;
247
+ }
248
+
237
249
for ( let i = 0 ; i < attributes . length ; ++ i ) {
238
250
const attribute = attributes [ i ] ;
239
251
if ( ! attribute ) {
@@ -245,7 +257,11 @@ function xmlElementLogicTrivial(node: XNode, buffer: string[], options: XmlOutpu
245
257
}
246
258
}
247
259
248
- let childNodes = node . transformedChildNodes . length > 0 ? node . transformedChildNodes : node . childNodes ;
260
+ let childNodes = node . transformedChildNodes . filter ( n => n . nodeType !== DOM_ATTRIBUTE_NODE ) ;
261
+ if ( childNodes . length === 0 ) {
262
+ childNodes = node . childNodes . filter ( n => n . nodeType !== DOM_ATTRIBUTE_NODE ) ;
263
+ }
264
+
249
265
childNodes = childNodes . sort ( ( a , b ) => a . siblingPosition - b . siblingPosition ) ;
250
266
if ( childNodes . length === 0 ) {
251
267
if ( options . outputMethod === 'html' && [ 'hr' , 'link' , 'meta' ] . includes ( node . nodeName ) ) {
0 commit comments