Skip to content

Commit

Permalink
[svn r3989] r4826@delle: sbehnel | 2008-10-25 20:19:53 +0200
Browse files Browse the repository at this point in the history
 fixed doctype serialisation for internal subsets without public/system ID

--HG--
branch : trunk
  • Loading branch information
scoder committed Oct 25, 2008
1 parent 03eab51 commit 7dd465c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Expand Up @@ -17,6 +17,10 @@ Features added
Bugs fixed
----------

* Internal DTD subsets that did not specify a system or public ID were
not serialised and did not appear in the docinfo property of
ElementTrees.

* Fix a pre-Py3k warning when parsing from a gzip file in Py2.6.

* Test suite fixes for libxml2 2.7.
Expand Down
5 changes: 5 additions & 0 deletions src/lxml/lxml.etree.pyx
Expand Up @@ -328,6 +328,9 @@ cdef public class _Document [ type LxmlDocumentType, object LxmlDocument ]:
return None
return _elementFactory(self, c_node)

cdef bint hasdoctype(self):
return self._c_doc.intSubset is not NULL

cdef getdoctype(self):
cdef tree.xmlDtd* c_dtd
cdef xmlNode* c_root_node
Expand Down Expand Up @@ -520,6 +523,8 @@ cdef class DocInfo:
elif system_url:
return u'<!DOCTYPE %s SYSTEM "%s">' % (
root_name, system_url)
elif self._doc.hasdoctype():
return u'<!DOCTYPE %s>' % root_name
else:
return u""

Expand Down
4 changes: 1 addition & 3 deletions src/lxml/serializer.pxi
Expand Up @@ -189,9 +189,7 @@ cdef void _writeDtdToBuffer(tree.xmlOutputBuffer* c_buffer,
cdef tree.xmlDtd* c_dtd
cdef xmlNode* c_node
c_dtd = c_doc.intSubset
if c_dtd == NULL or c_dtd.name == NULL:
return
if c_dtd.ExternalID == NULL and c_dtd.SystemID == NULL:
if c_dtd is NULL or c_dtd.name is NULL:
return
if cstd.strcmp(c_root_name, c_dtd.name) != 0:
return
Expand Down
18 changes: 18 additions & 0 deletions src/lxml/tests/test_etree.py
Expand Up @@ -2097,6 +2097,24 @@ def test_docinfo_empty(self):
self.assertEquals(docinfo.root_name, 'html')
self.assertEquals(docinfo.doctype, '')

def test_docinfo_name_only(self):
etree = self.etree
xml = _bytes('<!DOCTYPE root><root></root>')
tree = etree.parse(BytesIO(xml))
docinfo = tree.docinfo
self.assertEquals(docinfo.encoding, "UTF-8")
self.assertEquals(docinfo.xml_version, "1.0")
self.assertEquals(docinfo.public_id, None)
self.assertEquals(docinfo.system_url, None)
self.assertEquals(docinfo.root_name, 'root')
self.assertEquals(docinfo.doctype, '<!DOCTYPE root>')

def test_doctype_name_only_roundtrip(self):
etree = self.etree
xml = _bytes('<!DOCTYPE root>\n<root/>')
tree = etree.parse(BytesIO(xml))
self.assertEquals(xml, etree.tostring(tree))

def test_xml_base(self):
etree = self.etree
root = etree.XML(_bytes("<root/>"), base_url="http://no/such/url")
Expand Down

0 comments on commit 7dd465c

Please sign in to comment.