(htm + vhtml) How do I insert unescaped HTML from variable?
See original GitHub issueHello. Trying hml-2.2.1 mini + vhtml-2.1.0 min. Below are few use cases I tried, which demonstrate totally inconsistent behaviour.
Case 1 (good but useless):
let h = "<p>hello</p>";
document.body.innerHTML = html`${h}`;
// wanted: <p>hello</p>
// got: <p>hello</p>
Case 2: wrapping ${h} into <div>
(real use case):
let h = "<p>hello</p>";
document.body.innerHTML = html`<div>${h}</div>`;
// wanted: <div><p>hello</p></div>
// got: <div><p>hello</p></div>
Case 3: manually calling html() with wrapping <div>
(real use case):
let h = "<p>hello</p>";
return html`<div>${html(h)}</div>`;
// wanted: <div><p>hello</p></div>
// got: <div>hello<undefined></undefined></div>
Case 3a (real example I initially stuck into; it looses <p>
around “text”):
let h = "<main><h1>title</h1><p>text</p></main>";
return html`<div>${html(h)}</div>`;
// wanted: <div><main><h1>title</h1><p>text</p></main></div>
// got: <div><main><h1>title</h1>text</main><undefined></undefined></div>
Case 4: manually calling html() without wrapping <div>
:
let h = "<p>hello</p>";
return html`${html(h)}`;
// wanted: <p>hello</p>
// got: ,,,h,,e,,l,,l,,o,,<undefined></undefined>
Case 5: manually calling html() with wrapping <div>
and variable containing multiple elements:
let h = "<p>hello</p><p>world</p>";
return html`<div>${html(h)}</div>`;
// wanted: <div><p>hello</p><p>world</p></div>
// got: <div>world<undefined></undefined></div>
I suppose I do it wrong in case 5, I should wrap multiple <p>
into some single element, like I do in case 3a. I could also ignore weird <undefined>
elements if it worked correctly otherwise.
So, how do I do it correctly, then?
Thanks.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
How to escape & unescape HTML characters in string in ...
Escape HTML ; 1. function escape(htmlStr) { ; 2. return htmlStr.replace(/&/g, "&") ; 3 .replace(/</g, "<") ; 4 .replace(/>/g, ">") ; 5 .replace(/"/g,...
Read more >show a smarty variable with html content - Stack Overflow
I try to show it html-formatted. When showing it like {$html} only plain text appears without formatting. I try like: {$html|unescape} but then ......
Read more >Rendering raw/unescaped HTML in Blazor - Meziantou's blog
In this post, I describe how to render a string that contains HTML elements in an ASP.NET Core Blazor component.
Read more >How to Assign Block of HTML Code to a JavaScript Variable
Answer: Use the concatenation operator (+). The simple and safest way to use the concatenation operator ( + ) to assign or store...
Read more >How to escape html code with html allowed
Escape HTML = Lose HTML! · Note that the $output variable is completely unnecessary here, all it does is introduce an opportunity for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Okay, maybe my computer was a little strange 😦
Template tag functions get arguments as arrays: so you might have more luck in calling
html(['<p>Hello, World!</p>'])
.This
produces:
codesandbox
@goranmoomin I noticed it only worked if the value only contain valid html, when I add an preact component it doesn’t work, how to solve it?
Here’s an example :
Result :
Anyway the screenshot above is taken from
Spck Editor
on Android.