Skip to content

Commit c8140dd

Browse files
committed
Add support for comments as siblings of the root node to lxml
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%401116
1 parent 11ec09e commit c8140dd

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/html5lib/treebuilders/etree_lxml.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self):
3636
self._childNodes = []
3737

3838
def appendChild(self, element):
39-
warnings.warn("lxml does not support comments as siblings of the root node", DataLossWarning)
39+
self._elementTree.getroot().addnext(element._element)
4040

4141
def _getChildNodes(self):
4242
return self._childNodes
@@ -50,11 +50,19 @@ def serializeElement(element, indent=0):
5050
if not hasattr(element, "tag"):
5151
rv.append("#document")
5252
if element.docinfo.internalDTD:
53-
dtd_str = element.docinfo.doctype
54-
if not dtd_str:
53+
if not (element.docinfo.public_id or element.docinfo.system_url):
5554
dtd_str = "<!DOCTYPE %s>"%element.docinfo.root_name
55+
else:
56+
dtd_str = """<!DOCTYPE %s PUBLIC "%s" "%s">"""%(
57+
element.docinfo.root_name, element.docinfo.public_id,
58+
element.docinfo.system_url)
5659
rv.append("|%s%s"%(' '*(indent+2), dtd_str))
57-
serializeElement(element.getroot(), indent+2)
60+
next_element = element.getroot()
61+
while next_element.getprevious() is not None:
62+
next_element = next_element.getprevious()
63+
while next_element is not None:
64+
serializeElement(next_element, indent+2)
65+
next_element = next_element.getnext()
5866
elif type(element.tag) == type(etree.Comment):
5967
rv.append("|%s<!-- %s -->"%(' '*indent, element.text))
6068
else:
@@ -136,6 +144,7 @@ def __init__(self, fullTree = False):
136144
def reset(self):
137145
_base.TreeBuilder.reset(self)
138146
self.insertComment = self.insertCommentInitial
147+
self.initial_comments = []
139148
self.doctype = None
140149

141150
def testSerializer(self, element):
@@ -159,7 +168,7 @@ def insertDoctype(self, name, publicId, systemId):
159168
self.doctype = doctype
160169

161170
def insertCommentInitial(self, data, parent=None):
162-
warnings.warn("lxml does not support comments as siblings of the root node", DataLossWarning)
171+
self.initial_comments.append(data)
163172

164173
def insertRoot(self, name):
165174
"""Create the document root"""
@@ -181,6 +190,10 @@ def insertRoot(self, name):
181190
print docStr
182191
raise
183192

193+
#Append the initial comments:
194+
for comment_data in self.initial_comments:
195+
root.addprevious(etree.Comment(comment_data))
196+
184197
#Create the root document and add the ElementTree to it
185198
self.document = self.documentClass()
186199
self.document._elementTree = root.getroottree()

0 commit comments

Comments
 (0)