diff options
author | Leonard Richardson <leonardr@segfault.org> | 2015-06-28 15:39:36 -0400 |
---|---|---|
committer | Leonard Richardson <leonardr@segfault.org> | 2015-06-28 15:39:36 -0400 |
commit | 9428b9d6ed0d279a72414a986290821ca4f0caaf (patch) | |
tree | f792dd2d4e0b4c3913d4766a2717e57e7dd23b12 /bs4/element.py | |
parent | 92ad5e0dee9503f507f6277b493dfa96010f3a44 (diff) |
Changed the way soup objects work under copy.copy(). Copying a
NavigableString or a Tag will give you a new NavigableString that's
equal to the old one but not connected to the parse tree. Patch by
Martijn Peters. [bug=1307490]
Diffstat (limited to 'bs4/element.py')
-rw-r--r-- | bs4/element.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/bs4/element.py b/bs4/element.py index 0486da2..c70ad5a 100644 --- a/bs4/element.py +++ b/bs4/element.py @@ -815,6 +815,18 @@ class Tag(PageElement): parserClass = _alias("parser_class") # BS3 + def __copy__(self): + """A copy of a Tag is a new Tag, unconnected to the parse tree. + Its contents are a copy of the old Tag's contents. + """ + clone = type(self)(None, self.builder, self.name, self.namespace, + self.nsprefix, self.attrs) + for attr in ('can_be_empty_element', 'hidden'): + setattr(clone, attr, getattr(self, attr)) + for child in self.contents: + clone.append(child.__copy__()) + return clone + @property def is_empty_element(self): """Is this tag an empty-element tag? (aka a self-closing tag) |