зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1799354, replaceChildren should fire DOMNodeRemoved when needed (and not assert about 'Want to fire DOMNodeRemoved event, but it's not safe'), r=peterv
Differential Revision: https://phabricator.services.mozilla.com/D161682
This commit is contained in:
Родитель
4aff70800a
Коммит
e8b7bce992
|
@ -2069,21 +2069,6 @@ void FragmentOrElement::SetInnerHTMLInternal(const nsAString& aInnerHTML,
|
|||
}
|
||||
}
|
||||
|
||||
void FragmentOrElement::FireNodeRemovedForChildren() {
|
||||
Document* doc = OwnerDoc();
|
||||
// Optimize the common case
|
||||
if (!nsContentUtils::HasMutationListeners(
|
||||
doc, NS_EVENT_BITS_MUTATION_NODEREMOVED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsINode> child;
|
||||
for (child = GetFirstChild(); child && child->GetParentNode() == this;
|
||||
child = child->GetNextSibling()) {
|
||||
nsContentUtils::MaybeFireNodeRemoved(child, this);
|
||||
}
|
||||
}
|
||||
|
||||
void FragmentOrElement::AddSizeOfExcludingThis(nsWindowSizes& aSizes,
|
||||
size_t* aNodeSize) const {
|
||||
nsIContent::AddSizeOfExcludingThis(aSizes, aNodeSize);
|
||||
|
|
|
@ -119,12 +119,6 @@ class FragmentOrElement : public nsIContent {
|
|||
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS_INHERITED(
|
||||
FragmentOrElement, nsIContent)
|
||||
|
||||
/**
|
||||
* Fire a DOMNodeRemoved mutation event for all children of this node
|
||||
* TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
|
||||
*/
|
||||
MOZ_CAN_RUN_SCRIPT_BOUNDARY void FireNodeRemovedForChildren();
|
||||
|
||||
static void ClearContentUnbinder();
|
||||
static bool CanSkip(nsINode* aNode, bool aRemovingAllowed);
|
||||
static bool CanSkipInCC(nsINode* aNode);
|
||||
|
|
|
@ -2139,12 +2139,31 @@ void nsINode::ReplaceChildren(nsINode* aNode, ErrorResult& aRv) {
|
|||
return;
|
||||
}
|
||||
}
|
||||
nsCOMPtr<nsINode> node = aNode;
|
||||
|
||||
// Batch possible DOMSubtreeModified events.
|
||||
mozAutoSubtreeModified subtree(OwnerDoc(), nullptr);
|
||||
|
||||
if (nsContentUtils::HasMutationListeners(
|
||||
OwnerDoc(), NS_EVENT_BITS_MUTATION_NODEREMOVED)) {
|
||||
FireNodeRemovedForChildren();
|
||||
if (node) {
|
||||
if (node->NodeType() == DOCUMENT_FRAGMENT_NODE) {
|
||||
node->FireNodeRemovedForChildren();
|
||||
} else if (nsCOMPtr<nsINode> parent = node->GetParentNode()) {
|
||||
nsContentUtils::MaybeFireNodeRemoved(node, parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Needed when used in combination with contenteditable (maybe)
|
||||
mozAutoDocUpdate updateBatch(OwnerDoc(), true);
|
||||
|
||||
nsAutoMutationBatch mb(this, true, true);
|
||||
|
||||
// The code above explicitly dispatched DOMNodeRemoved events if needed.
|
||||
nsAutoScriptBlockerSuppressNodeRemoved scriptBlocker;
|
||||
|
||||
// Replace all with node within this.
|
||||
while (mFirstChild) {
|
||||
RemoveChildNode(mFirstChild, true);
|
||||
|
@ -3558,6 +3577,21 @@ void nsINode::RemoveMutationObserver(
|
|||
}
|
||||
}
|
||||
|
||||
void nsINode::FireNodeRemovedForChildren() {
|
||||
Document* doc = OwnerDoc();
|
||||
// Optimize the common case
|
||||
if (!nsContentUtils::HasMutationListeners(
|
||||
doc, NS_EVENT_BITS_MUTATION_NODEREMOVED)) {
|
||||
return;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsINode> child;
|
||||
for (child = GetFirstChild(); child && child->GetParentNode() == this;
|
||||
child = child->GetNextSibling()) {
|
||||
nsContentUtils::MaybeFireNodeRemoved(child, this);
|
||||
}
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsNodeWeakReference, nsIWeakReference)
|
||||
|
||||
nsNodeWeakReference::nsNodeWeakReference(nsINode* aNode)
|
||||
|
|
|
@ -1645,6 +1645,12 @@ class nsINode : public mozilla::dom::EventTarget {
|
|||
|
||||
bool UnoptimizableCCNode() const;
|
||||
|
||||
/**
|
||||
* Fire a DOMNodeRemoved mutation event for all children of this node
|
||||
* TODO: Convert this to MOZ_CAN_RUN_SCRIPT (bug 1415230)
|
||||
*/
|
||||
MOZ_CAN_RUN_SCRIPT_BOUNDARY void FireNodeRemovedForChildren();
|
||||
|
||||
private:
|
||||
mozilla::dom::SVGUseElement* DoGetContainingSVGUseShadowHost() const;
|
||||
|
||||
|
|
|
@ -271,6 +271,7 @@ skip-if = (os == "android" || headless) # See
|
|||
support-files =
|
||||
bug1739957.sjs
|
||||
[test_bug1784187.html]
|
||||
[test_bug1799354.html]
|
||||
[test_bug5141.html]
|
||||
[test_bug28293.html]
|
||||
[test_bug28293.xhtml]
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Test bug 1799354</title>
|
||||
<script src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
|
||||
<script>
|
||||
add_task(t => {
|
||||
let root = document.createElement("div");
|
||||
root.innerHTML = "<div id='a'>text<div id='b'>text2</div></div>";
|
||||
const a = root.firstChild;
|
||||
const b = a.lastChild;
|
||||
const txt = b.previousSibling;
|
||||
const txt2 = b.firstChild;
|
||||
let events = [];
|
||||
function listener(event) {
|
||||
events.push(event);
|
||||
}
|
||||
root.addEventListener("DOMNodeRemoved", listener);
|
||||
|
||||
// Test 1, replace current children with an element.
|
||||
b.replaceChildren(txt);
|
||||
is(events.length, 2, "Should have got two DOMNodeRemoved events");
|
||||
// replaceChildren removes first all the child nodes of b and then when
|
||||
// a new child is added to it, that child is removed from its original parent.
|
||||
// https://dom.spec.whatwg.org/commit-snapshots/6b3f055f3891a63423bf235d46f38ffdb298c2e7/#concept-node-replace-all
|
||||
is(events[0].target, txt2);
|
||||
is(events[0].relatedNode, b);
|
||||
is(events[1].target, txt);
|
||||
is(events[1].relatedNode, a);
|
||||
|
||||
// Test 2, replace current children with a document fragment.
|
||||
events = [];
|
||||
const df = document.createDocumentFragment();
|
||||
const dfChild1 = document.createElement("div");
|
||||
df.appendChild(dfChild1);
|
||||
const dfChild2 = document.createElement("div");
|
||||
df.appendChild(dfChild2);
|
||||
df.addEventListener("DOMNodeRemoved", listener);
|
||||
|
||||
b.replaceChildren(df);
|
||||
is(events.length, 3, "Should have got three DOMNodeRemoved events");
|
||||
is(events[0].target, txt);
|
||||
is(events[0].relatedNode, b);
|
||||
is(events[1].target, dfChild1);
|
||||
is(events[1].relatedNode, df);
|
||||
is(events[2].target, dfChild2);
|
||||
is(events[2].relatedNode, df);
|
||||
|
||||
// Test 3, replace current children with multiple elements.
|
||||
events = [];
|
||||
const rootChild1 = document.createElement("div");
|
||||
root.appendChild(rootChild1);
|
||||
const rootChild2 = document.createElement("div");
|
||||
root.appendChild(rootChild2);
|
||||
// Note, if replaceChildren gets more than one parameter, it moves the nodes
|
||||
// to a new internal document fragment and then removes the current children.
|
||||
b.replaceChildren(rootChild1, rootChild2);
|
||||
is(events.length, 4, "Should have got four DOMNodeRemoved events");
|
||||
is(events[0].target, rootChild1);
|
||||
is(events[0].relatedNode, root);
|
||||
is(events[1].target, rootChild2);
|
||||
is(events[1].relatedNode, root);
|
||||
is(events[2].target, dfChild1);
|
||||
is(events[2].relatedNode, b);
|
||||
is(events[3].target, dfChild2);
|
||||
is(events[3].relatedNode, b);
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<p id="display"></p>
|
||||
<div id="content" style="display: none"></div>
|
||||
<pre id="test"></pre>
|
||||
</body>
|
||||
</html>
|
Загрузка…
Ссылка в новой задаче