Bug 1121994 - Implement adopted callback for custom elements.

Tag UXP Issue #1344
This commit is contained in:
Gaming4JC 2020-01-05 18:15:57 -05:00 committed by Roy Tam
commit d35ff2985e
14 changed files with 76 additions and 130 deletions

View file

@ -47,7 +47,7 @@ CustomElementCallback::Call()
ni->LocalName(), ni->NamespaceID(),
extType.IsEmpty() ? nullptr : &extType);
nsContentUtils::EnqueueLifecycleCallback(
nsIDocument::eConnected, mThisObject, nullptr, definition);
nsIDocument::eConnected, mThisObject, nullptr, nullptr, definition);
}
static_cast<LifecycleCreatedCallback *>(mCallback.get())->Call(mThisObject, rv);
@ -60,6 +60,10 @@ CustomElementCallback::Call()
case nsIDocument::eDisconnected:
static_cast<LifecycleDisconnectedCallback *>(mCallback.get())->Call(mThisObject, rv);
break;
case nsIDocument::eAdopted:
static_cast<LifecycleAdoptedCallback *>(mCallback.get())->Call(mThisObject,
mAdoptedCallbackArgs.mOldDocument, mAdoptedCallbackArgs.mNewDocument, rv);
break;
case nsIDocument::eAttributeChanged:
static_cast<LifecycleAttributeChangedCallback *>(mCallback.get())->Call(mThisObject,
mArgs.name, mArgs.oldValue, mArgs.newValue, mArgs.namespaceURI, rv);
@ -332,7 +336,9 @@ CustomElementRegistry::SetupCustomElement(Element* aElement,
/* static */ UniquePtr<CustomElementCallback>
CustomElementRegistry::CreateCustomElementCallback(
nsIDocument::ElementCallbackType aType, Element* aCustomElement,
LifecycleCallbackArgs* aArgs, CustomElementDefinition* aDefinition)
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition)
{
MOZ_ASSERT(aDefinition, "CustomElementDefinition should not be null");
@ -360,6 +366,12 @@ CustomElementRegistry::CreateCustomElementCallback(
}
break;
case nsIDocument::eAdopted:
if (aDefinition->mCallbacks->mAdoptedCallback.WasPassed()) {
func = aDefinition->mCallbacks->mAdoptedCallback.Value();
}
break;
case nsIDocument::eAttributeChanged:
if (aDefinition->mCallbacks->mAttributeChangedCallback.WasPassed()) {
func = aDefinition->mCallbacks->mAttributeChangedCallback.Value();
@ -388,6 +400,9 @@ CustomElementRegistry::CreateCustomElementCallback(
callback->SetArgs(*aArgs);
}
if (aAdoptedCallbackArgs) {
callback->SetAdoptedCallbackArgs(*aAdoptedCallbackArgs);
}
return Move(callback);
}
@ -395,6 +410,7 @@ CustomElementRegistry::CreateCustomElementCallback(
CustomElementRegistry::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition)
{
CustomElementDefinition* definition = aDefinition;
@ -407,7 +423,8 @@ CustomElementRegistry::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType
}
auto callback =
CreateCustomElementCallback(aType, aCustomElement, aArgs, definition);
CreateCustomElementCallback(aType, aCustomElement, aArgs,
aAdoptedCallbackArgs, definition);
if (!callback) {
return;
}
@ -926,7 +943,7 @@ CustomElementRegistry::Upgrade(Element* aElement,
};
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAttributeChanged,
aElement,
&args, aDefinition);
&args, nullptr, aDefinition);
}
}
}
@ -934,7 +951,7 @@ CustomElementRegistry::Upgrade(Element* aElement,
// Step 4.
if (aElement->IsInComposedDoc()) {
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eConnected, aElement,
nullptr, aDefinition);
nullptr, nullptr, aDefinition);
}
// Step 5.
@ -958,7 +975,8 @@ CustomElementRegistry::Upgrade(Element* aElement,
// This is for old spec.
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eCreated,
aElement, nullptr, aDefinition);
aElement, nullptr,
nullptr, aDefinition);
}
//-----------------------------------------------------
@ -1141,6 +1159,11 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CustomElementDefinition)
cb.NoteXPCOMChild(callbacks->mDisconnectedCallback.Value());
}
if (callbacks->mAdoptedCallback.WasPassed()) {
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCallbacks->mAdoptedCallback");
cb.NoteXPCOMChild(callbacks->mAdoptedCallback.Value());
}
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mConstructor");
cb.NoteXPCOMChild(tmp->mConstructor);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

View file

@ -38,6 +38,12 @@ struct LifecycleCallbackArgs
nsString namespaceURI;
};
struct LifecycleAdoptedCallbackArgs
{
nsCOMPtr<nsIDocument> mOldDocument;
nsCOMPtr<nsIDocument> mNewDocument;
};
class CustomElementCallback
{
public:
@ -54,6 +60,13 @@ public:
mArgs = aArgs;
}
void SetAdoptedCallbackArgs(LifecycleAdoptedCallbackArgs& aAdoptedCallbackArgs)
{
MOZ_ASSERT(mType == nsIDocument::eAdopted,
"Arguments are only used by adopted callback.");
mAdoptedCallbackArgs = aAdoptedCallbackArgs;
}
private:
// The this value to use for invocation of the callback.
RefPtr<Element> mThisObject;
@ -63,6 +76,7 @@ private:
// Arguments to be passed to the callback,
// used by the attribute changed callback.
LifecycleCallbackArgs mArgs;
LifecycleAdoptedCallbackArgs mAdoptedCallbackArgs;
// CustomElementData that contains this callback in the
// callback queue.
CustomElementData* mOwnerData;
@ -365,6 +379,7 @@ public:
static void EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition);
void GetCustomPrototype(nsIAtom* aAtom,
@ -381,7 +396,9 @@ private:
static UniquePtr<CustomElementCallback> CreateCustomElementCallback(
nsIDocument::ElementCallbackType aType, Element* aCustomElement,
LifecycleCallbackArgs* aArgs, CustomElementDefinition* aDefinition);
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition);
/**
* Registers an unresolved custom element that is a candidate for

View file

@ -2602,8 +2602,8 @@ Element::SetAttrAndNotify(int32_t aNamespaceID,
(ns.IsEmpty() ? NullString() : ns)
};
nsContentUtils::EnqueueLifecycleCallback(
nsIDocument::eAttributeChanged, this, &args, definition);
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAttributeChanged,
this, &args, nullptr, definition);
}
}
}
@ -2867,8 +2867,8 @@ Element::UnsetAttr(int32_t aNameSpaceID, nsIAtom* aName,
(ns.IsEmpty() ? NullString() : ns)
};
nsContentUtils::EnqueueLifecycleCallback(
nsIDocument::eAttributeChanged, this, &args, definition);
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAttributeChanged,
this, &args, nullptr, definition);
}
}
}

View file

@ -9676,6 +9676,7 @@ nsContentUtils::EnqueueUpgradeReaction(Element* aElement,
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition)
{
// No DocGroup means no custom element reactions stack.
@ -9684,6 +9685,7 @@ nsContentUtils::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
}
CustomElementRegistry::EnqueueLifecycleCallback(aType, aCustomElement, aArgs,
aAdoptedCallbackArgs,
aDefinition);
}

View file

@ -126,6 +126,7 @@ class EventTarget;
class IPCDataTransfer;
class IPCDataTransferItem;
struct LifecycleCallbackArgs;
struct LifecycleAdoptedCallbackArgs;
class NodeInfo;
class nsIContentChild;
class nsIContentParent;
@ -2734,6 +2735,7 @@ public:
static void EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
mozilla::dom::LifecycleCallbackArgs* aArgs = nullptr,
mozilla::dom::LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs = nullptr,
mozilla::dom::CustomElementDefinition* aDefinition = nullptr);
static void GetCustomPrototype(nsIDocument* aDoc,

View file

@ -2582,6 +2582,7 @@ public:
eCreated,
eConnected,
eDisconnected,
eAdopted,
eAttributeChanged
};

View file

@ -526,6 +526,23 @@ nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
nsIDocument* newDoc = aNode->OwnerDoc();
if (newDoc) {
if (CustomElementRegistry::IsCustomElementEnabled()) {
// Adopted callback must be enqueued whenever a nodes
// shadow-including inclusive descendants that is custom.
Element* element = aNode->IsElement() ? aNode->AsElement() : nullptr;
if (element) {
RefPtr<CustomElementData> data = element->GetCustomElementData();
if (data && data->mState == CustomElementData::State::eCustom) {
LifecycleAdoptedCallbackArgs args = {
oldDoc,
newDoc
};
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAdopted,
element, nullptr, &args);
}
}
}
// XXX what if oldDoc is null, we don't know if this should be
// registered or not! Can that really happen?
if (wasRegistered) {

View file

@ -13,6 +13,8 @@
callback LifecycleCreatedCallback = void();
callback LifecycleConnectedCallback = void();
callback LifecycleDisconnectedCallback = void();
callback LifecycleAdoptedCallback = void(Document? oldDocument,
Document? newDocment);
callback LifecycleAttributeChangedCallback = void(DOMString attrName,
DOMString? oldValue,
DOMString? newValue,
@ -22,6 +24,7 @@ dictionary LifecycleCallbacks {
LifecycleCreatedCallback? createdCallback;
LifecycleConnectedCallback? connectedCallback;
LifecycleDisconnectedCallback? disconnectedCallback;
LifecycleAdoptedCallback? adoptedCallback;
LifecycleAttributeChangedCallback? attributeChangedCallback;
};

View file

@ -1,17 +1,5 @@
[adopted-callback.html]
type: testharness
[Inserting a custom element into the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
@ -24,18 +12,6 @@
[Inserting a custom element into a detached shadow tree that belongs to the document of the template elements must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in a new document must enqueue and invoke adoptedCallback]
expected: FAIL
@ -48,18 +24,6 @@
[Inserting a custom element into a detached shadow tree that belongs to a new document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
@ -72,18 +36,6 @@
[Inserting a custom element into a detached shadow tree that belongs to a cloned document must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
@ -96,18 +48,6 @@
[Inserting a custom element into a detached shadow tree that belongs to a document created by createHTMLDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
@ -120,18 +60,6 @@
[Inserting a custom element into a detached shadow tree that belongs to an HTML document created by createDocument must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
@ -144,18 +72,6 @@
[Inserting a custom element into a detached shadow tree that belongs to the document of an iframe must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving a custom element from the owner document into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting an ancestor of custom element into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL
[Moving an ancestor of custom element from the owner document into an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL
[Inserting a custom element into a shadow tree in an HTML document fetched by XHR must enqueue and invoke adoptedCallback]
expected: FAIL

View file

@ -1,10 +0,0 @@
[ChildNode.html]
type: testharness
[before on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[after on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[replaceWith on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL

View file

@ -2,7 +2,3 @@
type: testharness
[importNode on Document must construct a new custom element when importing a custom element]
expected: FAIL
[adoptNode on Document must enqueue an adopted reaction when importing a custom element]
expected: FAIL

View file

@ -20,12 +20,3 @@
[cloneNode on Node must enqueue an attributeChanged reaction when cloning an element only for observed attributes]
expected: FAIL
[insertBefore on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[appendChild on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[replaceChild on ChildNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL

View file

@ -1,8 +0,0 @@
[ParentNode.html]
type: testharness
[prepend on ParentNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[append on ParentNode must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL

View file

@ -8,7 +8,3 @@
[cloneContents on Range must enqueue an attributeChanged reaction when cloning an element only for observed attributes]
expected: FAIL
[insertNode on Range must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL