support string attribute on NavigableString objects
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Beautiful Soup |
Fix Committed
|
Wishlist
|
Unassigned |
Bug Description
This is a wishlist item.
Currently, Tag and NavigableString objects use different mechanisms to change their text content:
* Tag objects use the "string" attribute
* NavigableString objects use the "replace_with()" method
For example,
====
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(
>>> soup.find(
'AAA'
>>> soup.find(
>>> soup
<html><
====
It would be nice if NavigableString objects *also* supported the "string" attribute, which would be convenient for code that must handle both object types. (This is how I ran into this; I used conditionals for now.)
This gives no indication of error, but it has no effect:
===
>>> soup.find(
>>> soup
<html><
===
I think this enhancement request is consistent with the fact that the four basic object types already share many attributes and methods.
It turns out this is already done; I implemented this in the 4.13 branch a while ago, while reconciling the type system, but I didn't mention it in the CHANGELOG.