Skip to content

Commit 8755eb6

Browse files
committed
Extend getTreeBuilder()'s second argument (implementation) to the "dom" treebuilder type. Should be fully backwards compatible. Currently only tested with minidom, though.
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%401098
1 parent 4bf7e88 commit 8755eb6

File tree

2 files changed

+230
-197
lines changed

2 files changed

+230
-197
lines changed

src/html5lib/treebuilders/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,32 @@ def getTreeBuilder(treeType, implementation=None, **kwargs):
4040
4141
"simpletree" - a built-in DOM-ish tree type with support for some
4242
more pythonic idioms.
43-
"dom" - The xml.dom.minidom DOM implementation
43+
"dom" - A generic builder for DOM implementations, defaulting to
44+
a xml.dom.minidom based implementation for the sake of
45+
backwards compatibility (as releases up until 0.10 had a
46+
builder called "dom" that was a minidom implemenation).
4447
"etree" - A generic builder for tree implementations exposing an
4548
elementtree-like interface (known to work with
4649
ElementTree, cElementTree and lxml.etree).
4750
"beautifulsoup" - Beautiful soup (if installed)
4851
49-
implementation - (Currently applies to the "etree" tree type only). A module
50-
implementing the tree type e.g. xml.etree.ElementTree or
51-
lxml.etree."""
52+
implementation - (Currently applies to the "etree" and "dom" tree types). A
53+
module implementing the tree type e.g.
54+
xml.etree.ElementTree or lxml.etree."""
5255

5356
treeType = treeType.lower()
5457
if treeType not in treeBuilderCache:
55-
if treeType in ("dom", "simpletree"):
56-
mod = __import__(treeType, globals())
57-
treeBuilderCache[treeType] = mod.TreeBuilder
58+
if treeType == "dom":
59+
import dom
60+
# XXX: Keep backwards compatibility by using minidom if no implementation is given
61+
if implementation == None:
62+
from xml.dom import minidom
63+
implementation = minidom
64+
# XXX: NEVER cache here, caching is done in the dom submodule
65+
return dom.getDomModule(implementation, **kwargs).TreeBuilder
66+
elif treeType == "simpletree":
67+
import simpletree
68+
treeBuilderCache[treeType] = simpletree.TreeBuilder
5869
elif treeType == "beautifulsoup":
5970
import soup
6071
treeBuilderCache[treeType] = soup.TreeBuilder

0 commit comments

Comments
 (0)