@@ -80,6 +80,8 @@ def serializeElement(element, indent=0):
8080 serializeElement (next_element , indent + 2 )
8181 elif type (element .tag ) == type (etree .Comment ):
8282 rv .append ("|%s<!-- %s -->" % (' ' * indent , element .text ))
83+ if hasattr (element , "tail" ) and element .tail :
84+ rv .append ("|%s\" %s\" " % (' ' * indent , element .tail ))
8385 else :
8486 nsmatch = etree_builders .tag_regexp .match (element .tag )
8587 if nsmatch is not None :
@@ -113,8 +115,8 @@ def serializeElement(element, indent=0):
113115 indent += 2
114116 for child in element .getchildren ():
115117 serializeElement (child , indent )
116- if hasattr (element , "tail" ) and element .tail :
117- rv .append ("|%s\" %s\" " % (' ' * (indent - 2 ), element .tail ))
118+ if hasattr (element , "tail" ) and element .tail :
119+ rv .append ("|%s\" %s\" " % (' ' * (indent - 2 ), element .tail ))
118120 serializeElement (element , 0 )
119121
120122 if finalText is not None :
@@ -286,6 +288,12 @@ def insertDoctype(self, token):
286288
287289 def insertCommentInitial (self , data , parent = None ):
288290 self .initial_comments .append (data )
291+
292+ def insertCommentMain (self , data , parent = None ):
293+ if (parent == self .document and
294+ type (self .document ._elementTree .getroot ()[- 1 ].tag ) == type (etree .Comment )):
295+ warnings .warn ("lxml cannot represent adjacent comments beyond the root elements" , DataLossWarning )
296+ super ().insertComment (data , parent )
289297
290298 def insertRoot (self , token ):
291299 """Create the document root"""
@@ -301,6 +309,8 @@ def insertRoot(self, token):
301309 docStr += ' PUBLIC "%s" "%s"' % (self .doctype .publicId or "" ,
302310 self .doctype .systemId or "" )
303311 docStr += ">"
312+ if self .doctype .name != token ["name" ]:
313+ warnings .warn ("lxml cannot represent doctype with a different name to the root element" , DataLossWarning )
304314 docStr += "<THIS_SHOULD_NEVER_APPEAR_PUBLICLY/>"
305315
306316 try :
@@ -333,4 +343,4 @@ def insertRoot(self, token):
333343 self .openElements .append (root_element )
334344
335345 #Reset to the default insert comment function
336- self .insertComment = super ( TreeBuilder , self ). insertComment
346+ self .insertComment = self . insertCommentMain
0 commit comments