diff options
-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>") |