[Checkins] SVN: z3c.pt/trunk/ Correctly handle CDATA-sections when using the ElementTree-parser.

Malthe Borch mborch at gmail.com
Tue Aug 12 18:26:29 EDT 2008


Log message for revision 89759:
  Correctly handle CDATA-sections when using the ElementTree-parser.

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/src/z3c/pt/etree.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-08-12 22:24:28 UTC (rev 89758)
+++ z3c.pt/trunk/CHANGES.txt	2008-08-12 22:26:28 UTC (rev 89759)
@@ -4,6 +4,9 @@
 Version 1.0dev
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- CDATA sections are now correctly handled when using the
+  ElementTree-parser. [malthe]
+
 - Fixed bug in path-expressions where string instances would be
   (attempted) called. [malthe]
 

Modified: z3c.pt/trunk/src/z3c/pt/etree.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/etree.py	2008-08-12 22:24:28 UTC (rev 89758)
+++ z3c.pt/trunk/src/z3c/pt/etree.py	2008-08-12 22:26:28 UTC (rev 89759)
@@ -166,6 +166,11 @@
         def getparent(self):
             return self._parent
 
+        def getroottree(self):
+            while self._parent is not None:
+                self = self._parent
+            return self
+            
         def insert(self, position, element):
             element._parent = self
             ET._ElementInterface.insert(self, position, element)
@@ -178,8 +183,20 @@
             
         @property
         def nsmap(self):
+            # TODO: Return correct namespace map
             return {None: config.XML_NS}
-        
+
+        @property
+        def prefix(self):
+            try:
+                ns, prefix = self.tag.split('}')
+            except:
+                return None
+            
+            for prefix, namespace in self.nsmap.items():
+                if namespace == ns:
+                    return prefix
+            
     namespaces = {}
     def ns_lookup(ns):
         return namespaces.setdefault(ns, {})
@@ -201,7 +218,8 @@
             # processing instructions
             self._parser.CommentHandler = self.handle_comment
             self._parser.ProcessingInstructionHandler = self.handle_pi
-            #self._target.start("document", {})
+            self._parser.StartCdataSectionHandler = self.handle_cdata_start
+            self._parser.EndCdataSectionHandler = self.handle_cdata_end
        
         def doctype(self, name, pubid, system):
             self.doctype = u'<!DOCTYPE %(name)s PUBLIC "%(pubid)s" "%(system)s">' % \
@@ -219,6 +237,13 @@
             self._target.data("<?%(target)s %(data)s?>" % dict(target=target, data=data))
             self._target.end(name)
 
+        def handle_cdata_start(self):
+            self._target.start(utils.xml_attr('cdata'), {
+                utils.tal_attr('cdata'): ''})
+
+        def handle_cdata_end(self):
+            self._target.end(utils.xml_attr('cdata'))
+            
     def element_factory(tag, attrs=None, nsmap=None):
         if attrs is None:
             attrs = {}
@@ -236,7 +261,6 @@
         element = object.__new__(factory)
         element.__init__(tag, attrs)
         return element
-        #return factory(tag, attrs)
 
     def parse(body):
         target = TreeBuilder(element_factory=element_factory)
@@ -247,6 +271,3 @@
         root = parser.close()
 
         return root, parser.doctype
-
-    def CDATA(text):
-        return text



More information about the Checkins mailing list