Skip to content

Commit 4d9ae1e

Browse files
committed
Pretty print single CDATA nodes. Fixes #224
1 parent b6051f0 commit 4d9ae1e

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

src/XMLWriterBase.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ module.exports = class XMLWriterBase
208208

209209
childNodeCount = node.children.length
210210
firstChildNode = if childNodeCount is 0 then null else node.children[0]
211-
if childNodeCount == 0 or node.children.every((e) -> (e.type is NodeType.Text or e.type is NodeType.Raw) and e.value == '')
211+
if childNodeCount == 0 or node.children.every((e) -> (e.type is NodeType.Text or e.type is NodeType.Raw or e.type is NodeType.CData) and e.value == '')
212212
# empty element
213213
if options.allowEmpty
214214
r += '>'
@@ -217,7 +217,7 @@ module.exports = class XMLWriterBase
217217
else
218218
options.state = WriterState.CloseTag
219219
r += options.spaceBeforeSlash + '/>' + @endline(node, options, level)
220-
else if options.pretty and childNodeCount == 1 and (firstChildNode.type is NodeType.Text or firstChildNode.type is NodeType.Raw) and firstChildNode.value?
220+
else if options.pretty and childNodeCount == 1 and (firstChildNode.type is NodeType.Text or firstChildNode.type is NodeType.Raw or firstChildNode.type is NodeType.CData) and firstChildNode.value?
221221
# do not indent text-only nodes
222222
r += '>'
223223
options.state = WriterState.InsideTag
@@ -232,7 +232,7 @@ module.exports = class XMLWriterBase
232232
# if ANY are a text node, then suppress pretty now
233233
if options.dontPrettyTextNodes
234234
for child in node.children
235-
if (child.type is NodeType.Text or child.type is NodeType.Raw) and child.value?
235+
if (child.type is NodeType.Text or child.type is NodeType.Raw or child.type is NodeType.CData) and child.value?
236236
options.suppressPrettyCount++
237237
prettySuppressed = true
238238
break

test/basic/createxml.coffee

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ suite 'Creating XML:', ->
9494
<!-- CoffeeScript is awesome. -->
9595
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
9696
</xmlbuilder>
97-
<cdata>
98-
<![CDATA[<test att="val">this is a test</test>
99-
Second line]]>
100-
</cdata>
97+
<cdata><![CDATA[<test att="val">this is a test</test>
98+
Second line]]></cdata>
10199
<raw>&<>&</raw>
102100
<atttest att="val">text</atttest>
103101
<atttest att="val">text</atttest>
@@ -133,10 +131,8 @@ suite 'Creating XML:', ->
133131
<!-- CoffeeScript is awesome. -->
134132
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
135133
</xmlbuilder>
136-
<cdata>
137-
<![CDATA[<test att="val">this is a test</test>
138-
Second line]]>
139-
</cdata>
134+
<cdata><![CDATA[<test att="val">this is a test</test>
135+
Second line]]></cdata>
140136
<raw>&<>&</raw>
141137
<atttest att="val">text</atttest>
142138
<atttest att="val">text</atttest>
@@ -176,10 +172,8 @@ suite 'Creating XML:', ->
176172
<!-- CoffeeScript is awesome. -->
177173
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
178174
</xmlbuilder>
179-
<cdata>
180-
<![CDATA[<test att="val">this is a test</test>
181-
Second line]]>
182-
</cdata>
175+
<cdata><![CDATA[<test att="val">this is a test</test>
176+
Second line]]></cdata>
183177
<raw>&<>&</raw>
184178
<atttest att="val">text</atttest>
185179
<atttest att="val">text</atttest>

test/basic/stringwriter.coffee

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ suite 'Creating XML with string writer:', ->
109109
.ele('cdata')
110110
.cdata('<test att="val">this is a test</test>\nSecond line')
111111
.up()
112+
.ele('cdata')
113+
.cdata('test')
114+
.up()
115+
.ele('cdata')
116+
.cdata('test1')
117+
.cdata('test2')
118+
.up()
112119
.ele('raw')
113120
.raw('&<>&')
114121
.up()
@@ -147,9 +154,12 @@ suite 'Creating XML with string writer:', ->
147154
<!-- CoffeeScript is awesome. -->
148155
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
149156
</xmlbuilder>
157+
<cdata><![CDATA[<test att="val">this is a test</test>
158+
Second line]]></cdata>
159+
<cdata><![CDATA[test]]></cdata>
150160
<cdata>
151-
<![CDATA[<test att="val">this is a test</test>
152-
Second line]]>
161+
<![CDATA[test1]]>
162+
<![CDATA[test2]]>
153163
</cdata>
154164
<raw>&<>&</raw>
155165
<atttest att="val">text</atttest>
@@ -186,10 +196,8 @@ suite 'Creating XML with string writer:', ->
186196
<!-- CoffeeScript is awesome. -->
187197
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
188198
</xmlbuilder>
189-
<cdata>
190-
<![CDATA[<test att="val">this is a test</test>
191-
Second line]]>
192-
</cdata>
199+
<cdata><![CDATA[<test att="val">this is a test</test>
200+
Second line]]></cdata>
193201
<raw>&<>&</raw>
194202
<atttest att="val">text</atttest>
195203
<atttest att="val">text</atttest>
@@ -231,10 +239,8 @@ suite 'Creating XML with string writer:', ->
231239
<!-- CoffeeScript is awesome. -->
232240
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
233241
</xmlbuilder>
234-
<cdata>
235-
<![CDATA[<test att="val">this is a test</test>
236-
Second line]]>
237-
</cdata>
242+
<cdata><![CDATA[<test att="val">this is a test</test>
243+
Second line]]></cdata>
238244
<raw>&<>&</raw>
239245
<atttest att="val">text</atttest>
240246
<atttest att="val">text</atttest>
@@ -274,10 +280,8 @@ suite 'Creating XML with string writer:', ->
274280
<!-- CoffeeScript is awesome. -->
275281
<repo type="git">git://github.com/oozcitak/xmlbuilder-js.git</repo>
276282
</xmlbuilder>
277-
<cdata>
278-
<![CDATA[<test att="val">this is a test</test>
279-
Second line]]>
280-
</cdata>
283+
<cdata><![CDATA[<test att="val">this is a test</test>
284+
Second line]]></cdata>
281285
<raw>&<>&</raw>
282286
<atttest att="val">text</atttest>
283287
<atttest att="val">text</atttest>
@@ -293,13 +297,17 @@ suite 'Creating XML with string writer:', ->
293297
.up()
294298
.ele('atttest', 'text')
295299
.att('att', 'val')
300+
.up()
301+
.ele('atttest', { 'att': 'val' }).dat('text')
302+
.up()
296303
.end(builder.stringWriter( { pretty: true, indent: ' ', dontPrettyTextNodes: true } ))
297304

298305
"""
299306
<?xml version="1.0"?>
300307
<root>
301308
<atttest att="val">text</atttest>
302309
<atttest att="val">text</atttest>
310+
<atttest att="val"><![CDATA[text]]></atttest>
303311
</root>
304312
"""
305313
)

0 commit comments

Comments
 (0)