diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 09:09:59 -0500 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 09:09:59 -0500 |
commit | 52ce808c1b43a9efb55e0186647a801b8a7c23a5 (patch) | |
tree | 1d5a6252ec26a43cd3754ba905d5fc2c732fb943 | |
parent | ec444785a35722778ad188a66fef483cc93d7bbb (diff) |
Added tests of custom lists of empty-element tags.
-rw-r--r-- | beautifulsoup/element.py | 2 | ||||
-rw-r--r-- | tests/test_lxml.py | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/beautifulsoup/element.py b/beautifulsoup/element.py index c0c28d4..a70813d 100644 --- a/beautifulsoup/element.py +++ b/beautifulsoup/element.py @@ -446,7 +446,7 @@ class Tag(PageElement, Entities): # Set up any substitutions, such as the charset in a META tag. self.contains_substitutions = builder.set_up_substitutions(self) - self.can_be_empty_element = (builder.can_be_empty_element(name)) + self.can_be_empty_element = builder.can_be_empty_element(name) @property def is_empty_element(self): diff --git a/tests/test_lxml.py b/tests/test_lxml.py index 77dd1f1..2f1e98e 100644 --- a/tests/test_lxml.py +++ b/tests/test_lxml.py @@ -495,6 +495,12 @@ class TestLXMLBuilderEncodingConversion(SoupTest): from beautifulsoup.builder.lxml_builder import LXMLTreeBuilderForXML class TestLXMLXMLBuilder(SoupTest): + """Test XML-specific parsing behavior. + + Most of the tests use HTML as an example, since Beautiful Soup is + mainly an HTML parser. This test suite is a base for XML-specific + tree builders. + """ @property def default_builder(self): @@ -511,3 +517,22 @@ class TestLXMLXMLBuilder(SoupTest): def test_self_nonempty_tag_is_not_empty_element(self): soup = self.soup("<p><ihavecontents>contents</ihavecontents></p>") self.assertFalse(soup.ihavecontents.is_empty_element) + + def test_designated_empty_element_tags(self): + # A constructor argument allows you to say which empty tags + # should be presented as empty-element tags. + builder = LXMLTreeBuilderForXML(empty_element_tags=['bar']) + + soup = BeautifulSoup(builder=builder, markup="<foo />") + self.assertEquals(str(soup), "<foo></foo>") + + soup = BeautifulSoup(builder=builder, markup="<bar></bar>") + self.assertEquals(str(soup), "<bar />") + + def test_designated_empty_element_tag_does_not_change_parser_behavior(self): + # The designated list of empty-element tags only affects how + # empty tags are presented. It does not affect how tags are + # parsed--that's the parser's job. + builder = LXMLTreeBuilderForXML(empty_element_tags=['bar']) + soup = BeautifulSoup(builder=builder, markup="<bar>contents</bar>") + self.assertEquals(soup.encode(), "<bar>contents</bar>") |