tornadoweb / tornadoweb/tornado
Support for __html__ method and marking variables as safe to use in templates to avoid escaping
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 22.2k
- Forks
- 5.6k
- Avg merge
- 3h 42m
- Merged PRs (30d)
- 16
Description
Feature
Something like Django's safestring or the markupsafe library.
A lot of libraries (wtforms, django, jinja, etc.) follow a convention for autoescaping that if an object implements a special method called __html__, the value returned by this method should be assumed as safe to use in templates and not be escaped.
e.g.:
class SafeString(str):
def __html__(self):
return self
# escape function
def escape(value):
if hasattr(value, "__html__"):
return value.__html__()
else:
# escape normally
...
Obstacle
Although Tornado makes it easy to use a a custom autoescape function, the trouble is that tornado's template system converts str (and its subclass) instances to bytes. And during this conversion the information that it is a safe-marked variable (i.e. it has the __html__ method) is lost and the escape function has no way of knowing.
Possible solutions
1. Preserve the __html__ method during conversion
Can be done possibly in template.py or in escape.utf8 function.
This will allow interoperability with third party libs that inherit their SafeString class from str.
2. Provide a custom implementation of SafeString that inherits from bytes instead of str
This is what I'm currently doing in my project.
But to work with 3rd party libs I have to explicitly convert their safe str to my custom safe bytes, something like this:
{{ mark_safe(third_party_safe_str) }}
I think it's an essential feature for any template system. I don't see a reason why Tornado shouldn't have it built-in.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the conversion paths in tornado/template.py around lines 660-674 and escape.py around utf8 at lines 214-224. Compare how html-style values are handled by the linked safestring and markupsafe conventions. Done means the chosen approach preserves safe-marked values through template rendering and supports interoperability with third-party safe strings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, web-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100