The DOM is stupid. It has appendChild
and insertBefore
, but no prependChild
and insertAfter
.
So we should just walk the DOM back to front.
We will deal with fragments. A Fragment can contain a variable number of nodes, 0 or more.
seq()
creates a fragment with 0 or more nodesmatch()
creates a fragment with 0 or 1 nodechildren
creates a fragment with 0 or more nodes
On create or update, a fragment will take a location, that is basically a (parent, nextSibling?)
pair.
The fragment will then just parent.insertBefore(…, nextSibling)
its content. if nextSibling
is null, it is the same as appendChild
, just the thing we want in that case.
It will then return its own first child in case it did create some nodes, or just return the previous nextSibling
in case it didn’t.
We then go on and iterate all the fragments back to front.