changeset 68:d7e235461c97

HTML Support - Change imports to internal names
author Bastian Blank <bblank@thinkmo.de>
date Sun, 30 May 2010 17:39:14 +0200
parents 47b1c1aa1a2b
children 54c60c7e7e35
files emeraldtree/html.py emeraldtree/tests/test_html.py
diffstat 2 files changed, 17 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/emeraldtree/html.py	Sun May 30 17:28:59 2010 +0200
+++ b/emeraldtree/html.py	Sun May 30 17:39:14 2010 +0200
@@ -2,7 +2,7 @@
 # The ElementTree toolkit is
 #
 # Copyright (c) 1999-2007 by Fredrik Lundh
-#               2008-2009 Bastian Blank <bblank@thinkmo.de>
+#               2008-2010 Bastian Blank <bblank@thinkmo.de>
 #
 # By obtaining, using, and/or copying this software and/or its
 # associated documentation, you agree that you have read, understood,
@@ -36,7 +36,7 @@
 import mimetools, StringIO
 from HTMLParser import HTMLParser as HTMLParserBase
 
-import ElementTree
+from . import tree
 
 
 ##
@@ -67,12 +67,10 @@
     namespace = "http://www.w3.org/1999/xhtml"
 
     def __init__(self, encoding=None, builder=None):
+        HTMLParserBase.__init__(self)
         self.__stack = []
-        if builder is None:
-            builder = ElementTree.TreeBuilder()
-        self.__builder = builder
+        self.__builder = builder or tree.TreeBuilder()
         self.encoding = encoding or "iso-8859-1"
-        HTMLParserBase.__init__(self)
 
     ##
     # Flushes parser buffers, and return the root element.
@@ -87,7 +85,7 @@
     # (Internal) Handles start tags.
 
     def handle_starttag(self, tag, attrs):
-        tag = ElementTree.QName(tag.lower(), self.namespace)
+        tag = tree.QName(tag.lower(), self.namespace)
         if tag.name == "meta":
             # look for encoding directives
             http_equiv = content = None
@@ -114,7 +112,7 @@
                 # Handle short attributes
                 if value is None:
                     value = key
-                key = ElementTree.QName(key.lower(), self.namespace)
+                key = tree.QName(key.lower(), self.namespace)
                 attrib[key] = value
         self.__builder.start(tag, attrib)
         if tag.name in self.IGNOREEND:
@@ -125,8 +123,8 @@
     # (Internal) Handles end tags.
 
     def handle_endtag(self, tag):
-        if not isinstance(tag, ElementTree.QName):
-            tag = ElementTree.QName(tag.lower(), self.namespace)
+        if not isinstance(tag, tree.QName):
+            tag = tree.QName(tag.lower(), self.namespace)
         if tag.name in self.IGNOREEND:
             return
         lasttag = self.__stack.pop()
--- a/emeraldtree/tests/test_html.py	Sun May 30 17:28:59 2010 +0200
+++ b/emeraldtree/tests/test_html.py	Sun May 30 17:39:14 2010 +0200
@@ -1,6 +1,6 @@
 import py.test
-from emeraldtree.ElementTree import *
-from emeraldtree.html import *
+
+from .. import html, tree
 
 def serialize(elem, **options):
     from cStringIO import StringIO
@@ -10,27 +10,27 @@
     return file.getvalue()
 
 def test_HTMLParser_simple1():
-    elem = HTML('<a></a>')
+    elem = html.HTML('<a></a>')
     assert elem.tag.name == 'a'
     assert len(elem) == 0
 
 def test_HTMLParser_simple2():
-    elem = HTML('<a><b /></a>')
+    elem = html.HTML('<a><b /></a>')
     assert elem.tag.name == 'a'
     assert len(elem) == 1
     assert elem[0].tag.name == 'b'
     assert len(elem[0]) == 0
 
 def test_HTMLParser_text1():
-    elem = HTML('<a>b</a>')
+    elem = html.HTML('<a>b</a>')
     assert elem.tag.name == 'a'
-    assert isinstance(elem, Element)
+    assert isinstance(elem, tree.Element)
     assert len(elem) == 1
     assert elem[0] == 'b'
     assert isinstance(elem[0], unicode)
 
 def test_HTMLParser_text2():
-    elem = HTML('<a>b<c>d</c>d</a>')
+    elem = html.HTML('<a>b<c>d</c>d</a>')
     assert elem.tag.name == 'a'
     assert len(elem) == 3
     assert elem[0] == 'b'
@@ -38,11 +38,11 @@
     assert elem[2] == 'd'
 
 def test_HTMLParser_ignoreend():
-    elem = HTML('<br>')
+    elem = html.HTML('<br>')
     assert elem.tag.name == 'br'
     assert len(elem) == 0
 
-    elem = HTML('<br></br>')
+    elem = html.HTML('<br></br>')
     assert elem.tag.name == 'br'
     assert len(elem) == 0