summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bs4/element.py13
-rw-r--r--bs4/testing.py14
-rw-r--r--bs4/tests/test_lxml.py12
-rw-r--r--bs4/tests/test_soup.py19
4 files changed, 56 insertions, 2 deletions
diff --git a/bs4/element.py b/bs4/element.py
index fdb90e0..7e5810a 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -24,11 +24,22 @@ def _alias(attr):
class NamespacedAttribute(object):
- def __init__(self, namespace_abbreviation, name, namespace):
+ def __init__(self, namespace_abbreviation, name, namespace=None):
self.namespace_abbreviation = namespace_abbreviation
self.name = name
self.namespace = namespace
+ def __eq__(self, other):
+ if isinstance(other, NamespacedAttribute):
+ return (
+ self.namespace_abbreviation == other.namespace_abbreviation
+ and self.name == other.name
+ and self.namespace == other.namespace)
+ elif isinstance(other, basestring):
+ return str(self) == other
+ else:
+ return super(NamespacedAttribute, self).__eq__(other)
+
def __str__(self):
name = self.name
if self.namespace_abbreviation:
diff --git a/bs4/testing.py b/bs4/testing.py
index dc20812..b2ca180 100644
--- a/bs4/testing.py
+++ b/bs4/testing.py
@@ -358,6 +358,20 @@ class HTMLTreeBuilderSmokeTest(object):
# For the rest of the story, see TestSubstitutions in
# test_tree.py.
+class XMLTreeBuilderSmokeTest(object):
+
+ def test_namespaces_are_preserved(self):
+ markup = '<root xmlns:a="http://www.example.com/" xmlns:b="http://example.net/"><a:foo>This tag is in the a namespace</a:foo><b:foo>This tag is in the b namespace</b:foo></root>'
+ soup = self.soup(markup)
+ root = soup.root
+ import pdb; pdb.set_trace()
+ self.assertEquals("http://www.example.com/", root['xmlns:a'])
+ self.assertEquals("http://www.example.net/", root['xmlns:b'])
+
+
+ pass
+
+
def skipIf(condition, reason):
def nothing(test, *args, **kwargs):
diff --git a/bs4/tests/test_lxml.py b/bs4/tests/test_lxml.py
index 92b7389..27ec570 100644
--- a/bs4/tests/test_lxml.py
+++ b/bs4/tests/test_lxml.py
@@ -14,6 +14,7 @@ from bs4.testing import skipIf
from bs4.tests import test_htmlparser
from bs4.testing import (
HTMLTreeBuilderSmokeTest,
+ XMLTreeBuilderSmokeTest,
SoupTest,
skipIf,
)
@@ -35,3 +36,14 @@ class LXMLTreeBuilderSmokeTest(SoupTest, HTMLTreeBuilderSmokeTest):
"<p>foo&#x10000000000000;bar</p>", "<p>foobar</p>")
self.assertSoupEquals(
"<p>foo&#1000000000;bar</p>", "<p>foobar</p>")
+
+@skipIf(
+ not LXML_PRESENT,
+ "lxml seems not to be present, not testing its XML tree builder.")
+class LXMLXMLTreeBuilderSmokeTest(SoupTest, XMLTreeBuilderSmokeTest):
+ """See ``HTMLTreeBuilderSmokeTest``."""
+
+ @property
+ def default_builder(self):
+ return LXMLTreeBuilderForXML()
+
diff --git a/bs4/tests/test_soup.py b/bs4/tests/test_soup.py
index 2b7c003..e9aaa78 100644
--- a/bs4/tests/test_soup.py
+++ b/bs4/tests/test_soup.py
@@ -3,7 +3,10 @@
import unittest
from bs4 import BeautifulSoup
-from bs4.element import SoupStrainer
+from bs4.element import (
+ SoupStrainer,
+ NamespacedAttribute,
+ )
from bs4.dammit import EntitySubstitution, UnicodeDammit
from bs4.testing import SoupTest
import warnings
@@ -233,3 +236,17 @@ class TestUnicodeDammit(unittest.TestCase):
msg = w[0].message
self.assertTrue(isinstance(msg, UnicodeWarning))
self.assertTrue("Some characters could not be decoded" in str(msg))
+
+
+class TestNamedspacedAttribute(SoupTest):
+
+ def test_attribute_is_equivalent_to_colon_separated_string(self):
+ a = NamespacedAttribute("a", "b")
+ self.assertEqual("a:b", a)
+
+ def test_attributes_are_equivalent_if_all_members_identical(self):
+ a = NamespacedAttribute("a", "b", "c")
+ b = NamespacedAttribute("a", "b", "c")
+ self.assertEqual(a, b)
+ b.namespace = "d"
+ self.assertNotEqual(a, b)