You need to replace &
first:
def escape_html(s):
s = s.replace('&', "&")
s = s.replace(">", ">")
s = s.replace("<", "<")
s = s.replace('"', """)
return s
because otherwise you are replacing the &
in each of the other replacements you made. This has nothing to do with Python raw string literals; that only disables \
-style escapes.
You could also just use the cgi.escape()
function; set the second argument to True
to have it escape quotes.
Demo:
>>> def escape_html(s):
... s = s.replace('&', "&")
... s = s.replace(">", ">")
... s = s.replace("<", "<")
... s = s.replace('"', """)
... return s
...
>>> escape_html('<script>alert("Oops & bummer!")</script>')
'<script>alert("Oops & bummer!")</script>'
>>> import cgi
>>> cgi.escape('<script>alert("Oops & bummer!")</script>', True)
'<script>alert("Oops & bummer!")</script>'