diff options
Diffstat (limited to 'bs4')
-rw-r--r-- | bs4/element.py | 11 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 4 |
2 files changed, 13 insertions, 2 deletions
diff --git a/bs4/element.py b/bs4/element.py index 115ab24..5462592 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -131,8 +131,8 @@ class PageElement(object): # to methods like encode() and prettify(): # # "html" - All Unicode characters with corresponding HTML entities - # are converted to those entities on output. - # "minimal" - Bare ampersands and angle brackets are converted to + # are converted to those entities on output. + # "minimal" - Bare ampersands and angle brackets are converted to # XML entities: & < > # None - The null formatter. Unicode characters are never # converted to entities. This is not recommended, but it's @@ -992,6 +992,13 @@ class Tag(PageElement): attribute.""" return self.attrs.get(key, default) + def string_attr(self, key, default=None): + """The same as get(), but converts lists of values to strings.""" + value = self.get(key, default) + if isinstance(value, list): + value = " ".join(value) + return value + def has_attr(self, key): return key in self.attrs diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 354473a..f57255d 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1286,6 +1286,10 @@ class TestCDAtaListAttributes(SoupTest): soup = self.soup("<a class='foo\tbar'>") self.assertEqual(b'<a class="foo bar"></a>', soup.a.encode()) + def test_attribute_values_joined_into_string_through_string_attr(self): + soup = self.soup("<a class='foo\tbar'>") + self.assertEqual(b'foo bar', soup.a.string_attr('class')) + def test_accept_charset(self): soup = self.soup('<form accept-charset="ISO-8859-1 UTF-8">') self.assertEqual(['ISO-8859-1', 'UTF-8'], soup.form['accept-charset']) |