diff options
author | Leonard Richardson <leonardr@segfault.org> | 2017-05-06 21:38:56 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2017-05-06 21:38:56 -0400 |
commit | fe6afd533f5ae0344616b98776a96f87f439485e (patch) | |
tree | 70dd5f46e23885d1d6dcbe6488f576aeecf46179 | |
parent | e21aa406ab7c524692e2d462e1f30a4c37b1f0fc (diff) | |
download | beautifulsoup4-fe6afd533f5ae0344616b98776a96f87f439485e.tar.gz |
Replace get_attribute_text with get_attribute_list.
-rw-r--r-- | NEWS.txt | 6 | ||||
-rw-r--r-- | bs4/element.py | 8 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 6 | ||||
-rw-r--r-- | doc/source/index.rst | 8 |
4 files changed, 14 insertions, 14 deletions
@@ -1,8 +1,8 @@ = 4.6.0 (Unreleased) = -* Added the `Tag.get_attribute_text` method, which acts like `Tag.get` for - getting the value of an attribute, but which joins attribute - multi-values into a single string value. [bug=1678589] +* Added the `Tag.get_attribute_list` method, which acts like `Tag.get` for + getting the value of an attribute, but which always returns a list, + whether or not the attribute is a multi-value attribute. [bug=1678589] * It's now possible to use a tag's namespace prefix when searching, e.g. soup.find('namespace:tag') [bug=1655332] diff --git a/bs4/element.py b/bs4/element.py index 71cdb65..526db28 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -992,11 +992,11 @@ class Tag(PageElement): attribute.""" return self.attrs.get(key, default) - def get_attribute_text(self, key, default=None): - """The same as get(), but converts lists of values to strings.""" + def get_attribute_list(self, key, default=None): + """The same as get(), but always returns a list.""" value = self.get(key, default) - if isinstance(value, list): - value = " ".join(value) + if not isinstance(value, list): + value = [value] return value def has_attr(self, key): diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 90a6d25..c0e7c40 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1286,9 +1286,9 @@ 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('foo bar', soup.a.get_attribute_text('class')) + def test_get_attribute_list(self): + soup = self.soup("<a id='abc def'>") + self.assertEqual(['abc def'], soup.a.get_attribute_list('id')) def test_accept_charset(self): soup = self.soup('<form accept-charset="ISO-8859-1 UTF-8">') diff --git a/doc/source/index.rst b/doc/source/index.rst index 7bee329..e1b73aa 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -428,11 +428,11 @@ consolidated:: print(rel_soup.p) # <p>Back to the <a rel="index contents">homepage</a></p> -You can use ```get_attribute_text`` to get the value of any attribute as a -string, whether or not it's a multi-valued atribute:: +You can use ```get_attribute_list`` to get a value that's always a list, +string, whether or not it's a multi-valued atribute - css_soup.p.get_attribute_text('class') - # "body strikeout" + id_soup.p.get_attribute_list('id') + # ["my id"] If you parse a document as XML, there are no multi-valued attributes:: |