diff options
author | Leonard Richardson <leonard.richardson@canonical.com> | 2012-02-01 11:28:12 -0500 |
---|---|---|
committer | Leonard Richardson <leonard.richardson@canonical.com> | 2012-02-01 11:28:12 -0500 |
commit | cb5b71d9d90f6d3fb70eb9fca297d4e12dda3a3f (patch) | |
tree | 5cb3b1f0b1c004c4cc51ebe04b5df49103fba2d7 | |
parent | 713817f97c12a0eaa331eadef2277f058159aebd (diff) |
Got the new-tag test to pass.
-rw-r--r-- | bs4/element.py | 23 | ||||
-rw-r--r-- | bs4/tests/test_tree.py | 2 |
2 files changed, 17 insertions, 8 deletions
diff --git a/bs4/element.py b/bs4/element.py index 9344f45..5cadfc9 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -440,13 +440,18 @@ class Tag(PageElement): """Represents a found HTML tag with its attributes and contents.""" - def __init__(self, parser, builder, name, attrs=None, parent=None, - previous=None): + def __init__(self, parser=None, builder=None, name=None, attrs=None, + parent=None, previous=None): "Basic constructor." - # We don't actually store the parser object: that lets extracted - # chunks be garbage-collected. - self.parser_class = parser.__class__ + if parser is None: + self.parser_class = None + else: + # We don't actually store the parser object: that lets extracted + # chunks be garbage-collected. + self.parser_class = parser.__class__ + if name is None: + raise ValueError("No value provided for new tag's name.") self.name = name if attrs is None: attrs = {} @@ -458,9 +463,13 @@ class Tag(PageElement): self.hidden = False # Set up any substitutions, such as the charset in a META tag. - self.contains_substitutions = builder.set_up_substitutions(self) + if builder is not None: + self.contains_substitutions = builder.set_up_substitutions(self) - self.can_be_empty_element = builder.can_be_empty_element(name) + self.can_be_empty_element = builder.can_be_empty_element(name) + else: + self.contains_substitutions = False + self.can_be_empty_element = False parserClass = _alias("parser_class") # BS3 diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py index ee52edf..f14a746 100644 --- a/bs4/tests/test_tree.py +++ b/bs4/tests/test_tree.py @@ -540,7 +540,7 @@ class TestTagCreation(SoupTest): soup = self.soup("") new_tag = soup.new_tag("foo", bar="baz") self.assertTrue(isinstance(new_tag, Tag)) - self.assertEqual("foo", new_tag) + self.assertEqual("foo", new_tag.name) self.assertEqual(dict(bar="baz"), new_tag.attrs) self.assertEqual(None, new_tag.parent) |