Permalink
Browse files

Fixed indentation level in internal dtd elements when pretty printing1

  • Loading branch information...
oozcitak committed Dec 24, 2013
1 parent c0c5312 commit a96ef557937a959bdf31c2d08d304eb174b62d74
View
@@ -61,7 +61,17 @@ module.exports = class XMLBuilder
# `options.indent` indentation for pretty print
# `options.newline` newline sequence for pretty print
toString: (options) ->
pretty = options?.pretty or false
indent = options?.indent or ' '
newline = options?.newline or '\n'
r = ''
r += @xmldec.toString options if @xmldec?
r += @doctype.toString options if @doctype?
r += @rootObject.toString options
# remove trailing newline
if pretty and r.slice(-newline.length) == newline
r = r.slice(0, -newline.length)
return r
View
@@ -50,7 +50,7 @@ module.exports = class XMLDTDAttList
newline = options?.newline or '\n'
level or= 0
space = new Array(level).join(indent)
space = new Array(level + 1).join(indent)
r = ''
View
@@ -34,7 +34,7 @@ module.exports = class XMLDTDElement
newline = options?.newline or '\n'
level or= 0
space = new Array(level).join(indent)
space = new Array(level + 1).join(indent)
r = ''
View
@@ -52,7 +52,7 @@ module.exports = class XMLDTDEntity
newline = options?.newline or '\n'
level or= 0
space = new Array(level).join(indent)
space = new Array(level + 1).join(indent)
r = ''
@@ -34,7 +34,7 @@ module.exports = class XMLDTDNotation
newline = options?.newline or '\n'
level or= 0
space = new Array(level).join(indent)
space = new Array(level + 1).join(indent)
r = ''
View
@@ -160,9 +160,9 @@ module.exports = class XMLDocType
# internal subset
if @children.length > 0
r += ' ['
r += newline if pretty
for child in @children
r += child.toString options, level + 1
r += newline if pretty
r += ']'
# close tag
@@ -33,7 +33,7 @@ module.exports = class XMLProcessingInstruction
newline = options?.newline or '\n'
level or= 0
space = new Array(level).join(indent)
space = new Array(level + 1).join(indent)
r = ''
View
@@ -78,6 +78,61 @@ vows
'<root><node>test</node></root>'
assert.strictEqual topic.end(), xml
'Internal and external dtd (pretty print)':
topic: () ->
xmlbuilder.create('root')
.dtd('hello.dtd')
.ins('pub_border', 'thin')
.ele('img', 'EMPTY')
.com('Image attributes follow')
.att('img', 'height', 'CDATA', '#REQUIRED')
.att('img', 'visible', '(yes|no)', '#DEFAULT', "yes")
.not('fs', { sysID: 'http://my.fs.com/reader' })
.not('fs-nt', { pubID: 'FS Network Reader 1.0', sysID: 'http://my.fs.com/reader' })
.att('img', 'src', 'NOTATION (fs|fs-nt)', '#REQUIRED')
.dat('<owner>John</owner>')
.ele('node')
.ent('ent', 'my val')
.ent('ent', { sysID: 'http://www.myspec.com/ent' })
.ent('ent', { pubID: '-//MY//SPEC ENT//EN', sysID: 'http://www.myspec.com/ent' })
.ent('ent', { sysID: 'http://www.myspec.com/ent', nData: 'entprg' })
.ent('ent', { pubID: '-//MY//SPEC ENT//EN', sysID: 'http://www.myspec.com/ent', nData: 'entprg' })
.pent('ent', 'my val')
.pent('ent', { sysID: 'http://www.myspec.com/ent' })
.pent('ent', { pubID: '-//MY//SPEC ENT//EN', sysID: 'http://www.myspec.com/ent' })
.root()
.ele('node').txt('test')
'resulting XML': (topic) ->
xml = """
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "hello.dtd" [
<?pub_border thin?>
<!ELEMENT img EMPTY>
<!-- Image attributes follow -->
<!ATTLIST img height CDATA #REQUIRED>
<!ATTLIST img visible (yes|no) "yes">
<!NOTATION fs SYSTEM "http://my.fs.com/reader">
<!NOTATION fs-nt PUBLIC "FS Network Reader 1.0" "http://my.fs.com/reader">
<!ATTLIST img src NOTATION (fs|fs-nt) #REQUIRED>
<![CDATA[<owner>John</owner>]]>
<!ELEMENT node (#PCDATA)>
<!ENTITY ent "my val">
<!ENTITY ent SYSTEM "http://www.myspec.com/ent">
<!ENTITY ent PUBLIC "-//MY//SPEC ENT//EN" "http://www.myspec.com/ent">
<!ENTITY ent SYSTEM "http://www.myspec.com/ent" NDATA entprg>
<!ENTITY ent PUBLIC "-//MY//SPEC ENT//EN" "http://www.myspec.com/ent" NDATA entprg>
<!ENTITY % ent "my val">
<!ENTITY % ent SYSTEM "http://www.myspec.com/ent">
<!ENTITY % ent PUBLIC "-//MY//SPEC ENT//EN" "http://www.myspec.com/ent">
]>
<root>
<node>test</node>
</root>
"""
assert.strictEqual topic.end({ pretty: true }), xml
.export(module)
View
@@ -30,7 +30,7 @@ vows
.text('')
'resulting XML': (topic) ->
xml = '<test10></test10>\n'
xml = '<test10></test10>'
assert.strictEqual topic.doc().toString({ pretty: true }), xml
.export(module)

0 comments on commit a96ef55

Please sign in to comment.