[Lxml-checkins] r44207 - in lxml/branch/lxml-1.3: . src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Tue Jun 12 22:18:08 CEST 2007


Author: scoder
Date: Tue Jun 12 22:18:06 2007
New Revision: 44207

Modified:
   lxml/branch/lxml-1.3/CHANGES.txt
   lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi
   lxml/branch/lxml-1.3/src/lxml/etree.pyx
   lxml/branch/lxml-1.3/src/lxml/tests/test_elementtree.py
Log:
support Comment and ProcessingInstruction elements in el.getiterator()

Modified: lxml/branch/lxml-1.3/CHANGES.txt
==============================================================================
--- lxml/branch/lxml-1.3/CHANGES.txt	(original)
+++ lxml/branch/lxml-1.3/CHANGES.txt	Tue Jun 12 22:18:06 2007
@@ -25,6 +25,9 @@
 * Replacing the children slice of an Element would cut off the tails of the
   original children
 
+* ``Element.getiterator(tag)`` did not accept ``Comment`` and
+  ``ProcessingInstruction`` as tags
+
 * API functions now check incoming strings for XML conformity.  Zero bytes or
   low ASCII characters are no longer accepted.
 

Modified: lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi	(original)
+++ lxml/branch/lxml-1.3/src/lxml/apihelpers.pxi	Tue Jun 12 22:18:06 2007
@@ -436,6 +436,9 @@
     * its name string equals the c_name string
     """
     cdef char* c_node_href
+    if c_node.type != tree.XML_ELEMENT_NODE:
+        # not an element, only succeed if we match everything
+        return c_name is NULL and c_href is NULL
     if c_name is NULL:
         if c_href is NULL:
             # always match

Modified: lxml/branch/lxml-1.3/src/lxml/etree.pyx
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/etree.pyx	(original)
+++ lxml/branch/lxml-1.3/src/lxml/etree.pyx	Tue Jun 12 22:18:06 2007
@@ -1596,17 +1596,22 @@
 cdef public class _ElementTagMatcher [ object LxmlElementTagMatcher,
                                        type LxmlElementTagMatcherType ]:
     cdef object _pystrings
+    cdef int _node_type
     cdef char* _href
     cdef char* _name
     cdef _initTagMatch(self, tag):
+        self._href = NULL
+        self._name = NULL
         if tag is None:
-            self._href = NULL
-            self._name = NULL
+            self._node_type = 0
+        elif tag is Comment:
+            self._node_type = tree.XML_COMMENT_NODE
+        elif tag is ProcessingInstruction:
+            self._node_type = tree.XML_PI_NODE
         else:
+            self._node_type = tree.XML_ELEMENT_NODE
             self._pystrings = _getNsTag(tag)
-            if self._pystrings[0] is None:
-                self._href = NULL
-            else:
+            if self._pystrings[0] is not None:
                 self._href = _cstr(self._pystrings[0])
             self._name = _cstr(self._pystrings[1])
             if self._name[0] == c'*' and self._name[1] == c'\0':
@@ -1624,7 +1629,9 @@
         cdef xmlNode* c_node
         c_node = self._next_element(node._c_node)
         while c_node is not NULL and \
-                  not _tagMatches(c_node, self._href, self._name):
+                  self._node_type != 0 and \
+                  (self._node_type != c_node.type or
+                   not _tagMatches(c_node, self._href, self._name)):
             c_node = self._next_element(c_node)
         if c_node is NULL:
             self._node = None
@@ -1655,7 +1662,9 @@
             self._next_element = _nextElement
         if tag is not None:
             while c_node is not NULL and \
-                      not _tagMatches(c_node, self._href, self._name):
+                      self._node_type != 0 and \
+                      (self._node_type != c_node.type or
+                       not _tagMatches(c_node, self._href, self._name)):
                 c_node = self._next_element(c_node)
         if c_node is not NULL:
             # store Python ref:
@@ -1702,9 +1711,11 @@
         self._top_node  = node
         self._next_node = node
         self._initTagMatch(tag)
-        if tag is not None and \
-               not _tagMatches(node._c_node, self._href, self._name) or \
-               not inclusive:
+        if not inclusive or \
+               tag is not None and \
+               self._node_type != 0 and \
+               (self._node_type != node._c_node.type or
+                not _tagMatches(node._c_node, self._href, self._name)):
             # this cannot raise StopIteration, self._next_node != None
             self.next()
 
@@ -1727,7 +1738,8 @@
 
     cdef xmlNode* _nextNodeAnyTag(self, xmlNode* c_node):
         tree.BEGIN_FOR_EACH_ELEMENT_FROM(self._top_node._c_node, c_node, 0)
-        return c_node
+        if self._node_type == 0 or self._node_type == c_node.type:
+            return c_node
         tree.END_FOR_EACH_ELEMENT_FROM(c_node)
         return NULL
 

Modified: lxml/branch/lxml-1.3/src/lxml/tests/test_elementtree.py
==============================================================================
--- lxml/branch/lxml-1.3/src/lxml/tests/test_elementtree.py	(original)
+++ lxml/branch/lxml-1.3/src/lxml/tests/test_elementtree.py	Tue Jun 12 22:18:06 2007
@@ -1429,6 +1429,56 @@
             [a2],
             list(c.getiterator('a')))
 
+    def test_getiterator_filter_comment(self):
+        Element = self.etree.Element
+        Comment = self.etree.Comment
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        comment_b = Comment("TEST-b")
+        b.append(comment_b)
+
+        self.assertEquals(
+            [comment_b],
+            list(a.getiterator(Comment)))
+
+        comment_a = Comment("TEST-a")
+        a.append(comment_a)
+
+        self.assertEquals(
+            [comment_b, comment_a],
+            list(a.getiterator(Comment)))
+
+        self.assertEquals(
+            [comment_b],
+            list(b.getiterator(Comment)))
+
+    def test_getiterator_filter_pi(self):
+        Element = self.etree.Element
+        PI = self.etree.ProcessingInstruction
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        pi_b = PI("TEST-b")
+        b.append(pi_b)
+
+        self.assertEquals(
+            [pi_b],
+            list(a.getiterator(PI)))
+
+        pi_a = PI("TEST-a")
+        a.append(pi_a)
+
+        self.assertEquals(
+            [pi_b, pi_a],
+            list(a.getiterator(PI)))
+
+        self.assertEquals(
+            [pi_b],
+            list(b.getiterator(PI)))
+
     def test_getiterator_with_text(self):
         Element = self.etree.Element
         SubElement = self.etree.SubElement


More information about the lxml-checkins mailing list