question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(htm + vhtml) How do I insert unescaped HTML from variable?

See original GitHub issue

Hello. 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>&lt;p&gt;hello&lt;/p&gt;</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:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
goranmoomincommented, Dec 3, 2019

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

import htm from "htm";
import vhtml from "vhtml";

const html = htm.bind(vhtml);

const frag = "<p>Hello, World!</p>"
console.log(html`<div>${html([frag])}</div>`);

produces:

<div><p>Hello, World!</p></div>

codesandbox

0reactions
oolingcommented, Sep 28, 2022

@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 :

import {htm,h,MyComponent} from 'my/local/host/path/...';
let jsx = htm.bind(h);
console.log(
  jsx([
  `<div><${MyComponent}/></div>`
  ])
);

Result : Screenshot_20220928-193911403

Anyway the screenshot above is taken from Spck Editor on Android.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found