feat: mimicks dangerouslySetInnerHTML behavior#12
feat: mimicks dangerouslySetInnerHTML behavior#12lukal-x wants to merge 2 commits intoDavidVujic:mainfrom
Conversation
|
|
Thank you ⭐ I'll have a look! |
| data = [ | ||
| "div", | ||
| [ | ||
| "a", | ||
| { | ||
| "href": "/location", | ||
| "dangerouslySetInnerHTML": {"__html": "Location →"}, | ||
| }, | ||
| "Location", | ||
| ], |
There was a problem hiding this comment.
I have a question: why not adding the location &arr; as a child html node to a?
I didn't know about the dangerouslySetInnerHTML before. Is that mainly for the React parser? I'm thinking if it would make sense to add the child directly. Please correct me if I misunderstand!
There was a problem hiding this comment.
I think I understand the problem better, sorry for the noise! I will continue reviewing 😄
There was a problem hiding this comment.
Essentially there's scenarios where a blanket escape is unwanted because prevents you from inlining contents of an SVG for example, or some kind of markup that you load from a trusted source.
"Trusted" is the key term here. That's why the dangerously prefix.
It's from react yeah so I figure it's a recognizable enough idiom.
What I said about html.unescape is a bad idea btw, I'll edit the first comment to strike it out.
|
What do you think about this alternative approach? Allowing to pass in a function, as a custom render where you can add things that shouldn't be escaped. Here's what I have experimented with so far: |
|
Nice! It's not only more elegant but also more in line with https://weavejester.github.io/hiccup/hiccup2.core.html#var-raw |



Implementation of dangerouslySetInnerHTML attribute just like in React https://react.dev/reference/react-dom/components/common#dangerously-setting-the-inner-html
Description
Allows one to pass raw HTML strings to an element, overriding the HTML escaping routine:
Motivation and Context
This is useful to - for example - render content of an SVG with hiccup, or HTML entities like © (
©) as another example.Although I did check that changing the(edit: ignore this, bad idea)_escapefunction to dohtml.unescape(html.escape("© Python Hiccup 2025"))can accomplish the same. I can include it in this PR if it's wanted.The way it's implemented is by adding a nullable string parameter to
_to_htmlcalledrenamewhich, when set, renames the HTML element about to get rendered. By doing this we can detect thesetDangerouslyInnerHTMLattribute and early return a recursive re-render of a new version of the element but pretending it's a<style>or<script>tag - both of which aren't supposed to get escaped per-spec - and renaming it back to its original name.How Has This Been Tested?
Unit tests and with an SVG:
It might prove beneficial to stress test it, I do write a lot of Python with FP and try hard to avoid bottlenecks however I might've missed something.
Perhaps a switch to comparing strings to detect usage of
dangerouslySetInnerHTMLmight improve performance over filtering the attributes of every element in the tree.Also I'm aware the sizable change in
_to_htmlmight somehow fit better elsewhere but I'm not yet sure where.Types of changes
Checklist:
Should I also mention something in the docs? Is it significant enough?