diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2013-06-03 11:38:23 -0400 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2013-06-03 11:38:23 -0400 |
commit | 37f53a378ddab1c67a54448bb87cb61b5e122a44 (patch) | |
tree | cf9fad56204fffdaaf1fa5c3982a0c627ee08057 | |
parent | f5ff09d09ef6c60f5d5052fac25d4e128bdce34f (diff) |
A NavigableString object now has an immutable '.name' property whose
+ value is always None. This makes it easier to iterate over a mixed
+ list of tags and strings without having to check whether each
+ element is a tag or a string.
-rw-r--r-- | bs4/diagnose.py | 2 | ||||
-rw-r--r-- | bs4/element.py | 7 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 6 |
3 files changed, 14 insertions, 1 deletions
diff --git a/bs4/diagnose.py b/bs4/diagnose.py index 7c7ea7a..df72f65 100644 --- a/bs4/diagnose.py +++ b/bs4/diagnose.py @@ -202,5 +202,5 @@ def profile(num_elements=100000, parser="lxml"): if __name__ == '__main__': #diagnose(sys.stdin.read()) - profile(10000, parser="html5lib") + profile(100000, parser="lxml") # benchmark_parsers() diff --git a/bs4/element.py b/bs4/element.py index 742624d..f248895 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -674,6 +674,13 @@ class NavigableString(unicode, PageElement): output = self.format_string(self, formatter) return self.PREFIX + output + self.SUFFIX + @property + def name(self): + return None + + @name.setter + def name(self, name): + raise AttributeError("A NavigableString cannot be given a name.") class PreformattedString(NavigableString): """A NavigableString not subject to the normal formatting rules. diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index 2d09f96..0acc092 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -1219,6 +1219,12 @@ class TestCDAtaListAttributes(SoupTest): # attribute for any other tag. self.assertEqual('ISO-8859-1 UTF-8', soup.a['accept-charset']) + def test_string_has_immutable_name_property(self): + string = self.soup("s").string + self.assertEqual(None, string.name) + def t(): + string.name = 'foo' + self.assertRaises(AttributeError, t) class TestPersistence(SoupTest): "Testing features like pickle and deepcopy." |