Issue #2225 - Implement Element.replaceChildren

This accounts for refactoring changes (e.g. avoids RemoveChild*
changes).

Partially based on https://bugzilla.mozilla.org/show_bug.cgi?id=1626015
This commit is contained in:
FranklinDM 2023-05-01 20:50:50 +08:00 committed by roytam1
commit ae50e9e01d
3 changed files with 37 additions and 0 deletions

View file

@ -1963,6 +1963,40 @@ nsINode::Append(const Sequence<OwningNodeOrString>& aNodes,
AppendChild(*node, aRv);
}
// https://dom.spec.whatwg.org/#dom-parentnode-replacechildren
void nsINode::ReplaceChildren(const Sequence<OwningNodeOrString>& aNodes,
ErrorResult& aRv) {
nsCOMPtr<nsIDocument> doc = OwnerDoc();
nsCOMPtr<nsINode> node = ConvertNodesOrStringsIntoNode(aNodes, doc, aRv);
if (aRv.Failed()) {
return;
}
EnsurePreInsertionValidity(*node, nullptr, aRv);
if (aRv.Failed()) {
return;
}
// Needed when used in combination with contenteditable (maybe)
mozAutoDocUpdate updateBatch(doc, UPDATE_CONTENT_MODEL, true);
nsAutoMutationBatch mb(this, true, false);
// Replace all with node within this.
while (mFirstChild) {
int32_t index = IndexOf(mFirstChild);
if (index == -1) {
NS_ASSERTION(index != -1, "First child must have an index");
return;
}
RemoveChildAt(index, true);
}
mb.RemovalDone();
AppendChild(*node, aRv);
mb.NodesAdded();
}
void
nsINode::doRemoveChildAt(uint32_t aIndex, bool aNotify,
nsIContent* aKid, nsAttrAndChildArray& aChildArray)