@@ -12,6 +12,7 @@ import {
12
12
import { domGetAttributeValue } from './functions' ;
13
13
import { XNode } from './xnode' ;
14
14
import { XDocument } from './xdocument' ;
15
+ import { XmlOutputOptions } from './xml-output-options' ;
15
16
16
17
// Returns the text value of a node; for nodes without children this
17
18
// is the nodeValue, for nodes with children this is the concatenation
@@ -145,29 +146,35 @@ function xmlTextRecursive(node: XNode, buf: any[], cdata: any) {
145
146
146
147
/**
147
148
* Returns the representation of a node as XML text.
148
- * @param node The starting node.
149
- * @param opt_cdata If using CDATA configuration .
149
+ * @param { XNode } node The starting node.
150
+ * @param { XmlOutputOptions } options XML output options .
150
151
* @returns The XML string.
151
152
*/
152
- export function xmlTransformedText ( node : XNode , opt_cdata : boolean = false ) {
153
+ export function xmlTransformedText (
154
+ node : XNode ,
155
+ options : XmlOutputOptions = {
156
+ cData : false ,
157
+ escape : true
158
+ }
159
+ ) {
153
160
const buffer = [ ] ;
154
- xmlTransformedTextRecursive ( node , buffer , opt_cdata ) ;
161
+ xmlTransformedTextRecursive ( node , buffer , options ) ;
155
162
return buffer . join ( '' ) ;
156
163
}
157
164
158
- function xmlTransformedTextRecursive ( node : XNode , buffer : any [ ] , cdata : boolean ) {
165
+ function xmlTransformedTextRecursive ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
159
166
if ( node . printed ) return ;
160
167
const nodeType = node . transformedNodeType || node . nodeType ;
161
168
const nodeValue = node . transformedNodeValue || node . nodeValue ;
162
169
if ( nodeType == DOM_TEXT_NODE ) {
163
170
if ( node . transformedNodeValue && node . transformedNodeValue . trim ( ) !== '' ) {
164
- const finalText = node . escape ?
171
+ const finalText = node . escape && options . escape ?
165
172
xmlEscapeText ( node . transformedNodeValue ) :
166
173
node . transformedNodeValue ;
167
174
buffer . push ( finalText ) ;
168
175
}
169
176
} else if ( nodeType == DOM_CDATA_SECTION_NODE ) {
170
- if ( cdata ) {
177
+ if ( options . cData ) {
171
178
buffer . push ( nodeValue ) ;
172
179
} else {
173
180
buffer . push ( `<![CDATA[${ nodeValue } ]]>` ) ;
@@ -179,15 +186,15 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], cdata: boolean)
179
186
// had transformations, children should be present at output.
180
187
// This is called here "muted logic".
181
188
if ( node . transformedNodeName !== null && node . transformedNodeName !== undefined ) {
182
- xmlElementLogicTrivial ( node , buffer , cdata ) ;
189
+ xmlElementLogicTrivial ( node , buffer , options ) ;
183
190
} else {
184
- xmlElementLogicMuted ( node , buffer , cdata ) ;
191
+ xmlElementLogicMuted ( node , buffer , options ) ;
185
192
}
186
193
} else if ( nodeType == DOM_DOCUMENT_NODE || nodeType == DOM_DOCUMENT_FRAGMENT_NODE ) {
187
194
const childNodes = node . transformedChildNodes . concat ( node . childNodes ) ;
188
- // const childNodes = node.transformedChildNodes.length > 0 ? node.transformedChildNodes : node.childNodes;
195
+
189
196
for ( let i = 0 ; i < childNodes . length ; ++ i ) {
190
- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
197
+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
191
198
}
192
199
}
193
200
@@ -200,7 +207,7 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], cdata: boolean)
200
207
* @param buffer The XML buffer.
201
208
* @param cdata If using CDATA configuration.
202
209
*/
203
- function xmlElementLogicTrivial ( node : XNode , buffer : any [ ] , cdata : boolean ) {
210
+ function xmlElementLogicTrivial ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
204
211
buffer . push ( `<${ xmlFullNodeName ( node ) } ` ) ;
205
212
206
213
const attributes = node . transformedAttributes || node . attributes ;
@@ -223,7 +230,7 @@ function xmlElementLogicTrivial(node: XNode, buffer: any[], cdata: boolean) {
223
230
} else {
224
231
buffer . push ( '>' ) ;
225
232
for ( let i = 0 ; i < childNodes . length ; ++ i ) {
226
- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
233
+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
227
234
}
228
235
buffer . push ( `</${ xmlFullNodeName ( node ) } >` ) ;
229
236
}
@@ -237,10 +244,10 @@ function xmlElementLogicTrivial(node: XNode, buffer: any[], cdata: boolean) {
237
244
* @param buffer The XML buffer.
238
245
* @param cdata If using CDATA configuration.
239
246
*/
240
- function xmlElementLogicMuted ( node : XNode , buffer : any [ ] , cdata : boolean ) {
247
+ function xmlElementLogicMuted ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
241
248
const childNodes = node . transformedChildNodes . length > 0 ? node . transformedChildNodes : node . childNodes ;
242
249
for ( let i = 0 ; i < childNodes . length ; ++ i ) {
243
- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
250
+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
244
251
}
245
252
}
246
253
0 commit comments