←back to thread

Element: setHTML() method

(developer.mozilla.org)
170 points todsacerdoti | 3 comments | | HN request time: 0s | source
Show context
ibowankenobi ◴[] No.45675803[source]
The API design could be better. Document fragments are designed to be reused. It should accept an optional fragment key which accepts a document fragment.If not a fragment, throw, if has children, empty contents first.
replies(1): >>45675902 #
1. spankalee ◴[] No.45675902[source]
In what way are document fragments meant to be reused?

They empty their contents into the new parent when they're appended, so they can't be meaningfully appended a second time without rebuilding them.

`<template>` is mean to be reused, since you're meant to clone it in order to use it, and then you can clone it again.

replies(1): >>45676831 #
2. ibowankenobi ◴[] No.45676831[source]
You can absolutely reuse a document fragment

https://ibrahimtanyalcin.github.io/Cahir/

the whole rendering uses a single fragment.

replies(1): >>45679277 #
3. halapro ◴[] No.45679277[source]
You can absolutely not reuse a DocumentFragment. The moment you append it to a node, the fragment is emptied.

https://dom.spec.whatwg.org/#mutation-algorithms

> To insert a node into a parent before a child [...]:

> If node is a DocumentFragment node:

> Remove its children