diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 09:54:42 -0500 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2011-02-20 09:54:42 -0500 |
commit | ae349fd47c627f8166526fed8906811707d2f4b2 (patch) | |
tree | 116edd8c1d9a7cf6348f784162fd2291608833c2 /beautifulsoup/element.py | |
parent | 158e76fd3e1005f6f5f932414cb741083d114cb6 (diff) | |
parent | 9f437ea591aeaf16d593350baf081315e56a8b73 (diff) |
Greatly improved the handling of empty-element tags.
Diffstat (limited to 'beautifulsoup/element.py')
-rw-r--r-- | beautifulsoup/element.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/beautifulsoup/element.py b/beautifulsoup/element.py index 5793d59..a70813d 100644 --- a/beautifulsoup/element.py +++ b/beautifulsoup/element.py @@ -429,7 +429,6 @@ class Tag(PageElement, Entities): # chunks be garbage-collected. self.parserClass = parser.__class__ self.name = name - self.isSelfClosing = builder.isSelfClosingTag(name) if attrs == None: attrs = [] if isinstance(attrs, types.DictType): @@ -447,6 +446,26 @@ 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) + + @property + def is_empty_element(self): + """Is this tag an empty-element tag? (aka a self-closing tag) + + A tag that has contents is never an empty-element tag. + + A tag that has no contents may or may not be an empty-element + tag. It depends on the builder used to create the tag. If the + builder has a designated list of empty-element tags, then only + a tag whose name shows up in that list is considered an + empty-element tag. + + If the builder has no designated list of empty-element tags, + then any tag with no contents is an empty-element tag. + """ + return len(self.contents) == 0 and self.can_be_empty_element + isSelfClosing = is_empty_element # BS3 + @property def string(self): @@ -624,7 +643,7 @@ class Tag(PageElement, Entities): attrs.append(decoded) close = '' closeTag = '' - if self.isSelfClosing: + if self.is_empty_element: close = ' /' else: closeTag = '</%s>' % self.name |