Commits

Anonymous committed 77a556f

Tree - Only write document start (doctype, xml definition) if we write a complete one

  • Participants
  • Parent commits 0121a7e

Comments (0)

Files changed (1)

File emeraldtree/tree.py

     Node class.
     """
 
-    def write(self, write, encoding=None, namespaces={}, method=None):
+    def write(self, write, encoding=None, namespaces={}, method=None, document=False):
         if not method or method == "xml":
             Writer = XMLWriter
         elif method == "html":
         else:
             Writer = TextWriter
 
-        Writer(encoding, namespaces).write(write, self)
+        Writer(encoding, namespaces).write(write, self, document=document)
 
 
 ##
             namespaces = namespaces.copy()
             namespaces[default_namespace] = ''
 
-        self._root.write(write, encoding=encoding, namespaces=namespaces, method=method)
+        self._root.write(write, encoding=encoding, namespaces=namespaces, method=method, document=True)
 
 # --------------------------------------------------------------------
 # serialization support
         "http://purl.org/dc/elements/1.1/": "dc",
     }
 
-    def serialize_start(self, write):
+    def serialize_document_start(self, write):
         pass
 
-    def write(self, write, element):
+    def write(self, write, element, document=False):
         qnames, namespaces = self._namespaces(element)
 
         if self.encoding:
         else:
             write_encode = write
 
-        self.serialize_start(write_encode)
+        if document:
+            self.serialize_document_start(write_encode)
         self.serialize(write_encode, element, qnames, namespaces)
 
 
             for e in elem:
                 self.serialize(write, e, qnames)
 
-    def serialize_start(self, write):
-        if self.encoding and self.encoding not in ("utf-8", "us-ascii"):
-            write(u"<?xml version='1.0' encoding='%s'?>\n" % self.encoding)
+    def serialize_dicument_start(self, write):
+        write(u"<?xml version='1.0' encoding='%s'?>\n" % self.encoding)
 
 
 class HTMLWriter(MLBaseWriter):
             for e in elem:
                 self.serialize(write, e, qnames)
 
-    def serialize_start(self, write):
+    def serialize_document_start(self, write):
         write(u"<!DOCTYPE html>\n")