summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS.txt3
-rw-r--r--bs4/__init__.py4
-rw-r--r--bs4/element.py6
-rw-r--r--doc/source/index.rst15
4 files changed, 15 insertions, 13 deletions
diff --git a/NEWS.txt b/NEWS.txt
index 6ba5275..cc4c976 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -61,6 +61,9 @@
* Improved the exception raised when you call .unwrap() or
.replace_with() on an element that's not attached to a tree.
+* You can now create a NavigableString or a subclass just by invoking
+ the constructor. [bug=1294315]
+
= 4.3.2 (20131002) =
* Fixed a bug in which short Unicode input was improperly encoded to
diff --git a/bs4/__init__.py b/bs4/__init__.py
index c78c54e..4b92152 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -241,9 +241,7 @@ class BeautifulSoup(Tag):
def new_string(self, s, subclass=NavigableString):
"""Create a new NavigableString associated with this soup."""
- navigable = subclass(s)
- navigable.setup()
- return navigable
+ return subclass(s)
def insert_before(self, successor):
raise NotImplementedError("BeautifulSoup objects don't support insert_before().")
diff --git a/bs4/element.py b/bs4/element.py
index 2ea68b3..7fdeb08 100644
--- a/bs4/element.py
+++ b/bs4/element.py
@@ -674,9 +674,11 @@ class NavigableString(unicode, PageElement):
how to handle non-ASCII characters.
"""
if isinstance(value, unicode):
- return unicode.__new__(cls, value)
- u = unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING)
+ u = unicode.__new__(cls, value)
+ else:
+ u = unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING)
u.setup()
+ return u
def __copy__(self):
return self
diff --git a/doc/source/index.rst b/doc/source/index.rst
index bab20d8..3e4d547 100644
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -1803,17 +1803,17 @@ like calling ``.append()`` on a Python list::
soup.a.contents
# [u'Foo', u'Bar']
-``BeautifulSoup.new_string()`` and ``.new_tag()``
+``NavigableString()`` and ``.new_tag()``
-------------------------------------------------
If you need to add a string to a document, no problem--you can pass a
-Python string in to ``append()``, or you can call the factory method
-``BeautifulSoup.new_string()``::
+Python string in to ``append()``, or you can call the ``NavigableString``
+constructor::
soup = BeautifulSoup("<b></b>")
tag = soup.b
tag.append("Hello")
- new_string = soup.new_string(" there")
+ new_string = NavigableString(" there")
tag.append(new_string)
tag
# <b>Hello there.</b>
@@ -1821,18 +1821,17 @@ Python string in to ``append()``, or you can call the factory method
# [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()``::
+``NavigableString``, just call the constructor::
from bs4 import Comment
- new_comment = soup.new_string("Nice to see you.", Comment)
+ new_comment = Comment("Nice to see you.")
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.)
+(This is a new feature in Beautiful Soup 4.4.0.)
What if you need to create a whole new tag? The best solution is to
call the factory method ``BeautifulSoup.new_tag()``::