Re: [whatwg/dom] Suggestion: `Node.insertAfter(newNode, referenceNode)` (#986)

> > If child is null, this prepends node as the first child.
> 
> with `insertBefore`, if `child` is `null` the element is appended (like `appendChild` would do) ... I find this proposed behavior here a bit controversial ... the _before_ "_appends_ with null, the _after_ "_prepend_" with null as first child ... am I the only one not fully getting the use case for prepending as first child with a method called _after_ ?

@WebReflection It's based on the semantics of `elem.insertBefore`

- `elem.insertBefore(node, null)` appends because in essence, it's just seeking forward from the start to the position within the element's children right before the reference node (without skipping past it) and inserting there. Absent a reference node, it just seeks to the end (where it can't seek further), thus appending.
- `elem.insertAfter(node, null)` I'm proposing to prepend because of the same reason, just in the opposite direction and starting from the opposite end. It seeks back from the start to the position within the element's children right after the reference node (as in, it likewise doesn't skip past it) and inserting the node there. Absent a reference node, it just seeks to the start (where it can't seek further), thus prepending.

Of course, it's not spec'd like this (nobody with any sense is going to implement it that way), but that's the general idea.

Separately, in virtual DOM frameworks, in the face of fragments, we literally don't know what the next sibling will be, and so it's faster for us to recompute it each time from our internal model.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/986#issuecomment-860462140

Received on Monday, 14 June 2021 07:44:14 UTC