observablehq / htl

A tagged template literal that allows safe interpolation of values into HTML, following the HTML5 spec
https://observablehq.com/@observablehq/htl
ISC License
305 stars 24 forks source link

html`${""}` should return an empty text node, not null? #4

Closed mbostock closed 4 years ago

mbostock commented 4 years ago

Since we attempt to fold expressions embedded in data directly into markup, this is considered equivalent to empty markup,

html``

which evaluates to null. But if the intent of HTL is that embedded expressions don’t change the structure of the resulting HTML, then we must ensure that we only fold data expressions into markup if there is a preceding or following text node.

For example, here’s another case where we want to generate a text node:

html`${""}<span>hello`

Alternatively, we could have empty markup return an empty text node, and then this would return an empty text node, too:

html`${""}`

That kind of makes sense, but it would mean that any embedded expression that evaluates to null would now insert an empty text node rather than inserting nothing, which feels wrong, too.

html`${null}`
mbostock commented 4 years ago

Fixed in 9518296.