@@ -12,6 +12,7 @@ import {
1212import { domGetAttributeValue } from './functions' ;
1313import { XNode } from './xnode' ;
1414import { XDocument } from './xdocument' ;
15+ import { XmlOutputOptions } from './xml-output-options' ;
1516
1617// Returns the text value of a node; for nodes without children this
1718// is the nodeValue, for nodes with children this is the concatenation
@@ -145,29 +146,35 @@ function xmlTextRecursive(node: XNode, buf: any[], cdata: any) {
145146
146147/**
147148 * 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 .
150151 * @returns The XML string.
151152 */
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+ ) {
153160 const buffer = [ ] ;
154- xmlTransformedTextRecursive ( node , buffer , opt_cdata ) ;
161+ xmlTransformedTextRecursive ( node , buffer , options ) ;
155162 return buffer . join ( '' ) ;
156163}
157164
158- function xmlTransformedTextRecursive ( node : XNode , buffer : any [ ] , cdata : boolean ) {
165+ function xmlTransformedTextRecursive ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
159166 if ( node . printed ) return ;
160167 const nodeType = node . transformedNodeType || node . nodeType ;
161168 const nodeValue = node . transformedNodeValue || node . nodeValue ;
162169 if ( nodeType == DOM_TEXT_NODE ) {
163170 if ( node . transformedNodeValue && node . transformedNodeValue . trim ( ) !== '' ) {
164- const finalText = node . escape ?
171+ const finalText = node . escape && options . escape ?
165172 xmlEscapeText ( node . transformedNodeValue ) :
166173 node . transformedNodeValue ;
167174 buffer . push ( finalText ) ;
168175 }
169176 } else if ( nodeType == DOM_CDATA_SECTION_NODE ) {
170- if ( cdata ) {
177+ if ( options . cData ) {
171178 buffer . push ( nodeValue ) ;
172179 } else {
173180 buffer . push ( `<![CDATA[${ nodeValue } ]]>` ) ;
@@ -179,15 +186,15 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], cdata: boolean)
179186 // had transformations, children should be present at output.
180187 // This is called here "muted logic".
181188 if ( node . transformedNodeName !== null && node . transformedNodeName !== undefined ) {
182- xmlElementLogicTrivial ( node , buffer , cdata ) ;
189+ xmlElementLogicTrivial ( node , buffer , options ) ;
183190 } else {
184- xmlElementLogicMuted ( node , buffer , cdata ) ;
191+ xmlElementLogicMuted ( node , buffer , options ) ;
185192 }
186193 } else if ( nodeType == DOM_DOCUMENT_NODE || nodeType == DOM_DOCUMENT_FRAGMENT_NODE ) {
187194 const childNodes = node . transformedChildNodes . concat ( node . childNodes ) ;
188- // const childNodes = node.transformedChildNodes.length > 0 ? node.transformedChildNodes : node.childNodes;
195+
189196 for ( let i = 0 ; i < childNodes . length ; ++ i ) {
190- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
197+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
191198 }
192199 }
193200
@@ -200,7 +207,7 @@ function xmlTransformedTextRecursive(node: XNode, buffer: any[], cdata: boolean)
200207 * @param buffer The XML buffer.
201208 * @param cdata If using CDATA configuration.
202209 */
203- function xmlElementLogicTrivial ( node : XNode , buffer : any [ ] , cdata : boolean ) {
210+ function xmlElementLogicTrivial ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
204211 buffer . push ( `<${ xmlFullNodeName ( node ) } ` ) ;
205212
206213 const attributes = node . transformedAttributes || node . attributes ;
@@ -223,7 +230,7 @@ function xmlElementLogicTrivial(node: XNode, buffer: any[], cdata: boolean) {
223230 } else {
224231 buffer . push ( '>' ) ;
225232 for ( let i = 0 ; i < childNodes . length ; ++ i ) {
226- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
233+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
227234 }
228235 buffer . push ( `</${ xmlFullNodeName ( node ) } >` ) ;
229236 }
@@ -237,10 +244,10 @@ function xmlElementLogicTrivial(node: XNode, buffer: any[], cdata: boolean) {
237244 * @param buffer The XML buffer.
238245 * @param cdata If using CDATA configuration.
239246 */
240- function xmlElementLogicMuted ( node : XNode , buffer : any [ ] , cdata : boolean ) {
247+ function xmlElementLogicMuted ( node : XNode , buffer : any [ ] , options : XmlOutputOptions ) {
241248 const childNodes = node . transformedChildNodes . length > 0 ? node . transformedChildNodes : node . childNodes ;
242249 for ( let i = 0 ; i < childNodes . length ; ++ i ) {
243- xmlTransformedTextRecursive ( childNodes [ i ] , buffer , cdata ) ;
250+ xmlTransformedTextRecursive ( childNodes [ i ] , buffer , options ) ;
244251 }
245252}
246253
0 commit comments