diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2012-04-16 10:06:26 -0400 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2012-04-16 10:06:26 -0400 |
commit | 0afe0af7cd8240ab790ccbcea6ecbdf69f21461e (patch) | |
tree | bd6a8fc992d24d144466e74ea9f7b2ac4fb31fa1 /bs4/element.py | |
parent | c40bc98de62545aa8855311a1d046af5cd9020ba (diff) |
Attribute values are now run through the provided output formatter. Previously they were always run through the 'minimal' formatter. [bug=980237]
Diffstat (limited to 'bs4/element.py')
-rw-r--r-- | bs4/element.py | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/bs4/element.py b/bs4/element.py index 496f2ad..684da38 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -57,6 +57,18 @@ class PageElement(object): None : None } + @classmethod + def format_string(self, s, formatter='minimal'): + """Format the given string using the given formatter.""" + if not callable(formatter): + formatter = self.FORMATTERS.get( + formatter, EntitySubstitution.substitute_xml) + if formatter is None: + output = s + else: + output = formatter(s) + return output + def setup(self, parent=None, previous_element=None): """Sets up the initial relations between this element and other elements.""" @@ -617,14 +629,7 @@ class NavigableString(unicode, PageElement): self.__class__.__name__, attr)) def output_ready(self, formatter="minimal"): - if not callable(formatter): - formatter = self.FORMATTERS.get( - formatter, EntitySubstitution.substitute_xml) - if formatter is None: - output = self - else: - output = formatter(self) - + output = self.format_string(self, formatter) return self.PREFIX + output + self.SUFFIX @@ -950,8 +955,10 @@ class Tag(PageElement): and '%SOUP-ENCODING%' in val): val = self.substitute_encoding(val, eventual_encoding) - decoded = (str(key) + '=' - + EntitySubstitution.substitute_xml(val, True)) + text = self.format_string(val, formatter) + decoded = ( + str(key) + '=' + + EntitySubstitution.quoted_attribute_value(text)) attrs.append(decoded) close = '' closeTag = '' |