summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2013-05-20 10:20:40 -0400
committerLeonard Richardson <leonardr@segfault.org>2013-05-20 10:20:40 -0400
commita4d113a2f6648d7f97d29bbbd2634949a4050eb0 (patch)
treed74845850aa94bb8899ae175fe32a6ec08321e1a
parent9f370bad91d80570a57156f53c6a9efc918ff90f (diff)
Gave new_string() the ability to create subclasses of
NavigableString. [bug=1181986]
-rw-r--r--NEWS.txt3
-rw-r--r--bs4/__init__.py6
-rw-r--r--bs4/tests/test_tree.py6
-rw-r--r--doc/source/index.rst14
4 files changed, 26 insertions, 3 deletions
diff --git a/NEWS.txt b/NEWS.txt
index 2c8541c..50b3cca 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -1,5 +1,8 @@
= 4.2.1 (Unreleased) =
+* Gave new_string() the ability to create subclasses of
+ NavigableString. [bug=1181986]
+
* Fixed test failures when lxml is not installed. [bug=1181589]
* html5lib now supports Python 3. Fixed some Python 2-specific
diff --git a/bs4/__init__.py b/bs4/__init__.py
index a5e7a86..0195bcb 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -17,7 +17,7 @@ http://www.crummy.com/software/BeautifulSoup/bs4/doc/
"""
__author__ = "Leonard Richardson (leonardr@segfault.org)"
-__version__ = "4.2.0"
+__version__ = "4.2.1"
__copyright__ = "Copyright (c) 2004-2013 Leonard Richardson"
__license__ = "MIT"
@@ -201,9 +201,9 @@ class BeautifulSoup(Tag):
"""Create a new tag associated with this soup."""
return Tag(None, self.builder, name, namespace, nsprefix, attrs)
- def new_string(self, s):
+ def new_string(self, s, subclass=NavigableString):
"""Create a new NavigableString associated with this soup."""
- navigable = NavigableString(s)
+ navigable = subclass(s)
navigable.setup()
return navigable
diff --git a/bs4/tests/test_tree.py b/bs4/tests/test_tree.py
index 4f12d20..c39b3f7 100644
--- a/bs4/tests/test_tree.py
+++ b/bs4/tests/test_tree.py
@@ -689,6 +689,12 @@ class TestTagCreation(SoupTest):
self.assertEqual("foo", s)
self.assertTrue(isinstance(s, NavigableString))
+ def test_new_string_can_create_navigablestring_subclass(self):
+ soup = self.soup("")
+ s = soup.new_string("foo", Comment)
+ self.assertEqual("foo", s)
+ self.assertTrue(isinstance(s, Comment))
+
class TestTreeModification(SoupTest):
def test_attribute_modification(self):
diff --git a/doc/source/index.rst b/doc/source/index.rst
index e254855..a91854c 100644
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1806,6 +1806,20 @@ Python string in to ``append()``, or you can call the factory method
tag.contents
# [u'Hello', u' there']
+If you want to create a comment or some other subclass of
+``NavigableString``, pass that class as the second argument to
+``new_string()``::
+
+ from bs4 import Comment
+ new_comment = soup.new_string("Nice to see you.", Comment)
+ tag.append(new_comment)
+ tag
+ # <b>Hello there<!--Nice to see you.--></b>
+ tag.contents
+ # [u'Hello', u' there', u'Nice to see you.']
+
+(This is a new feature in Beautiful Soup 4.2.1.)
+
What if you need to create a whole new tag? The best solution is to
call the factory method ``BeautifulSoup.new_tag()``::