SVG tags and missing namespace prefixes breaks BeautifulSoup when using htm5lib..

Bug #1073810 reported by Roger Hu on 2012-11-01
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Beautiful Soup
Undecided
Unassigned

Bug Description

Stack error if prefix is None (which can happen with this SVG tag:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

diff --git a/bs4/element.py b/bs4/element.py
index 4a4d3ed..3058d5e 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -26,6 +26,8 @@ class NamespacedAttribute(unicode):
     def __new__(cls, prefix, name, namespace=None):
         if name is None:
             obj = unicode.__new__(cls, prefix)
+ elif prefix is None and name:
+ obj = unicode.__new__(cls, name)
         else:
             obj = unicode.__new__(cls, prefix + ":" + name)
         obj.prefix = prefix
diff --git a/bs4/tests/test_html5lib.py b/bs4/tests/test_html5lib.py
index f195f7d..b2a9f48 100644
--- a/bs4/tests/test_html5lib.py
+++ b/bs4/tests/test_html5lib.py
@@ -56,3 +56,8 @@ class HTML5LibBuilderSmokeTest(SoupTest, HTML5TreeBuilderSmokeTest):
             "<table><thead><tr><td>Foo</td></tr></thead>"
             "<tbody><tr><td>Bar</td></tr></tbody>"
             "<tfoot><tr><td>Baz</td></tr></tfoot></table>")
+
+ def test_prefixed_namespaces(self):
+ soup = self.soup('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">')
+ self.assertEqual("svg", soup.find(attrs={"xmlns" : "http://www.w3.org/2000/svg"}).name)
+ self.assertEqual("svg", soup.find(attrs={"xmlns:xlink" : "http://www.w3.org/1999/xlink"}).name)

Roger Hu (roger-hu) wrote :
Leonard Richardson (leonardr) wrote :

There is similar code in 4.1.2.

Changed in beautifulsoup:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers