Permalink
Browse files

Add XHTML namespace to wrapper elements if parser is namespacing gene…

…rated elements

(The default parser generates namespaced elements.)
  • Loading branch information...
1 parent 4dc02d1 commit 3bb3bd861a0fdaca870cca17ae9f5a1f14aa3e3f @dairiki committed Mar 31, 2012
Showing with 29 additions and 3 deletions.
  1. +12 −3 src/lxml/html/html5parser.py
  2. +17 −0 src/lxml/html/tests/test_html5parser.py
@@ -110,7 +110,7 @@ def fragment_fromstring(html, create_parent=False,
if create_parent:
if not isinstance(create_parent, _strings):
- create_parent = 'div'
+ create_parent = _ns_prefix(parser) + 'div'
new_root = Element(create_parent)
if elements:
if isinstance(elements[0], _strings):
@@ -166,11 +166,20 @@ def fromstring(html, guess_charset=True, parser=None):
# content that was passed in. We will create a fake container, which
# is the body tag, except <body> implies too much structure.
if _contains_block_level_tag(body):
- body.tag = 'div'
+ body.tag = _ns_prefix(parser) + 'div'
else:
- body.tag = 'span'
+ body.tag = _ns_prefix(parser) + 'span'
return body
+def _ns_prefix(parser):
+ try:
+ use_ns = bool(parser.tree.namespaceHTMLElements)
+ except AttributeError:
+ use_ns = True
+ if use_ns:
+ return '{%s}' % XHTML_NAMESPACE
+ else:
+ return ''
def parse(filename_url_or_file, guess_charset=True, parser=None):
"""Parse a filename, URL, or file-like object into an HTML document
@@ -164,6 +164,11 @@ def test_create_parent(self):
self.assertEqual(elem.text, 'head')
self.assertEqual(elem[0].tag, 'child')
+ def test_create_parent_default_type(self):
+ parser = DummyParser(fragments=[])
+ elem = self.call_it('html', parser=parser, create_parent=True)
+ self.assertEqual(elem.tag, xhtml_tag('div'))
+
def test_create_parent_default_type_no_ns(self):
parser = DummyParser(fragments=[], namespaceHTMLElements=False)
elem = self.call_it('html', parser=parser, create_parent=True)
@@ -233,13 +238,25 @@ def test_returns_body_if_single_element_has_tail(self):
parser = DummyParser(root=root)
self.assertEqual(self.call_it('', parser=parser), body)
+ def test_wraps_multiple_fragments_in_div(self):
+ E = HTMLElementMaker()
+ parser = DummyParser(root=E.html(E.head(), E.body(E.h1(), E.p())))
+ elem = self.call_it('', parser=parser)
+ self.assertEqual(elem.tag, xhtml_tag('div'))
+
def test_wraps_multiple_fragments_in_div_no_ns(self):
E = HTMLElementMaker(namespaceHTMLElements=False)
parser = DummyParser(root=E.html(E.head(), E.body(E.h1(), E.p())),
namespaceHTMLElements=False)
elem = self.call_it('', parser=parser)
self.assertEqual(elem.tag, 'div')
+ def test_wraps_multiple_fragments_in_span(self):
+ E = HTMLElementMaker()
+ parser = DummyParser(root=E.html(E.head(), E.body('foo', E.a('link'))))
+ elem = self.call_it('', parser=parser)
+ self.assertEqual(elem.tag, xhtml_tag('span'))
+
def test_wraps_multiple_fragments_in_span_no_ns(self):
E = HTMLElementMaker(namespaceHTMLElements=False)
parser = DummyParser(root=E.html(E.head(), E.body('foo', E.a('link'))),

0 comments on commit 3bb3bd8

Please sign in to comment.