[Lxml-checkins] r45119 - in lxml/trunk: . doc src/lxml src/lxml/tests

scoder at codespeak.net scoder at codespeak.net
Mon Jul 16 00:16:34 CEST 2007


Author: scoder
Date: Mon Jul 16 00:16:33 2007
New Revision: 45119

Modified:
   lxml/trunk/CHANGES.txt
   lxml/trunk/doc/compatibility.txt
   lxml/trunk/src/lxml/etree.pyx
   lxml/trunk/src/lxml/tests/test_elementtree.py
   lxml/trunk/src/lxml/tests/test_etree.py
Log:
partial merge from lxml.html branch: support Comment/PI/Element in iter methods

Modified: lxml/trunk/CHANGES.txt
==============================================================================
--- lxml/trunk/CHANGES.txt	(original)
+++ lxml/trunk/CHANGES.txt	Mon Jul 16 00:16:33 2007
@@ -41,6 +41,9 @@
 
 * ``Element()`` did not raise an exception on tag names containing ':'
 
+* ``Element.getiterator(tag)`` did not accept ``Comment`` and
+  ``ProcessingInstruction`` as tags. It also accepts ``Element`` now.
+
 * The XML parser did not report undefined entities as error
 
 * The text in exceptions raised by XML parsers, validators and XPath

Modified: lxml/trunk/doc/compatibility.txt
==============================================================================
--- lxml/trunk/doc/compatibility.txt	(original)
+++ lxml/trunk/doc/compatibility.txt	Mon Jul 16 00:16:33 2007
@@ -122,6 +122,17 @@
   not.  This means that a comment text "text" that ElementTree serializes as
   "<!-- text -->" will become "<!--text-->" in lxml.
 
+* When '*' is used as filter in the ``Element.getiterator()`` method,
+  ElementTree returns all elements in the tree, including comments and
+  processing instructions. lxml.etree only returns real Elements, i.e. tree
+  nodes that have a string tag name.  Without a filter, both libraries iterate
+  over all nodes.
+
+  Note that currently only lxml.etree supports passing the ``Element`` factory
+  function as filter to select only Elements.  Both libraries support passing
+  the ``Comment`` and ``ProcessingInstruction`` factories to select the
+  respective tree nodes.
+
 * ElementTree merges the target of a processing instruction into ``PI.text``,
   while lxml.etree puts it into the ``.target`` property and leaves it out of
   the ``.text`` property.  The ``pi.text`` in ElementTree therefore

Modified: lxml/trunk/src/lxml/etree.pyx
==============================================================================
--- lxml/trunk/src/lxml/etree.pyx	(original)
+++ lxml/trunk/src/lxml/etree.pyx	Mon Jul 16 00:16:33 2007
@@ -1645,17 +1645,26 @@
 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
+        elif tag is Entity:
+            self._node_type = tree.XML_ENTITY_REF_NODE
+        elif tag is Element:
+            self._node_type = tree.XML_ELEMENT_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':
@@ -1673,7 +1682,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
@@ -1704,7 +1715,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:
@@ -1750,14 +1763,15 @@
     # keep next node to return and a depth counter in the tree
     cdef _Element _next_node
     cdef _Element _top_node
-    cdef int _include_all_types
     def __init__(self, _Element node not None, tag=None, inclusive=True):
         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()
 
@@ -1783,7 +1797,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/trunk/src/lxml/tests/test_elementtree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_elementtree.py	(original)
+++ lxml/trunk/src/lxml/tests/test_elementtree.py	Mon Jul 16 00:16:33 2007
@@ -1455,6 +1455,70 @@
             [a2],
             list(c.getiterator('a')))
 
+    def test_getiterator_filter_all(self):
+        Element = self.etree.Element
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        c = SubElement(a, 'c')
+        d = SubElement(b, 'd')
+        e = SubElement(c, 'e')
+
+        self.assertEquals(
+            [a, b, d, c, e],
+            list(a.getiterator('*')))
+
+    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

Modified: lxml/trunk/src/lxml/tests/test_etree.py
==============================================================================
--- lxml/trunk/src/lxml/tests/test_etree.py	(original)
+++ lxml/trunk/src/lxml/tests/test_etree.py	Mon Jul 16 00:16:33 2007
@@ -1299,6 +1299,64 @@
             [d, f],
             list(a.getiterator('{b}*')))
 
+    def test_getiterator_filter_entities(self):
+        Element = self.etree.Element
+        Entity = self.etree.Entity
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        entity_b = Entity("TEST-b")
+        b.append(entity_b)
+
+        self.assertEquals(
+            [entity_b],
+            list(a.getiterator(Entity)))
+
+        entity_a = Entity("TEST-a")
+        a.append(entity_a)
+
+        self.assertEquals(
+            [entity_b, entity_a],
+            list(a.getiterator(Entity)))
+
+        self.assertEquals(
+            [entity_b],
+            list(b.getiterator(Entity)))
+
+    def test_getiterator_filter_element(self):
+        Element = self.etree.Element
+        Comment = self.etree.Comment
+        PI = self.etree.PI
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        a.append(Comment("test"))
+        a.append(PI("pi", "content"))
+        c = SubElement(a, 'c')
+
+        self.assertEquals(
+            [a, b, c],
+            list(a.getiterator(Element)))
+
+    def test_getiterator_filter_all_comment_pi(self):
+        # ElementTree iterates over everything here
+        Element = self.etree.Element
+        Comment = self.etree.Comment
+        PI = self.etree.PI
+        SubElement = self.etree.SubElement
+
+        a = Element('a')
+        b = SubElement(a, 'b')
+        a.append(Comment("test"))
+        a.append(PI("pi", "content"))
+        c = SubElement(a, 'c')
+
+        self.assertEquals(
+            [a, b, c],
+            list(a.getiterator('*')))
+
     def test_findall_ns(self):
         XML = self.etree.XML
         root = XML('<a xmlns:x="X" xmlns:y="Y"><x:b><c/></x:b><b/><c><x:b/><b/></c><b/></a>')


More information about the lxml-checkins mailing list