-
-
Notifications
You must be signed in to change notification settings - Fork 168
Description
Prior to #400, some str
methods were overridden using a generic wrapper that called escape
on all argument values that were str
, then wrapped the result in Markup
. This made typing difficult, and was inefficient. When removing the wrapper, I noticed it was only applied to str
methods that returned str
, not other method that took str
arguments. I also noticed that some arguments shouldn't have escape
called on them even though they are str
.
There are some methods where the escaping makes sense. For example, str.center
is similar to adding strings, which should escape the values.
But there are many other places where the escaping is inconsistent or not obvious. For example, __contains__
(and find
, index
, etc.) is not overridden, but strip
is, so you can get the following behavior.
>>> from markupsafe import Markup
>>> s = Markup("<br>")
>>> "<" in s
True
>>> s.strip("<")
Markup("<br>")
strip
calls escape
on its argument, resulting in the actual call being str.strip(s, "<")
. That's definitely not correct, because strip
removes any number of each character, in any order, but then intention was obviously to remove the <
.
Python 3.9 added removeprefix
and removesuffix
, which remove the exact string of characters if it is present. But it's not clear if those should escape their argument, should removeprefix("<")
remove the literal <
, or <
?
All methods should at least be consistent. Regardless of which direction we choose, escaping or not, the user can always wrap the argument in Markup
or escape
to get the other meaning.