Bug 1121994 - Implement adopted callback for custom elements. r=smaug

This commit is contained in:
John Dai 2017-10-02 00:42:00 -04:00
Родитель 5d2b4f7c18
Коммит 25f8ec6929
16 изменённых файлов: 77 добавлений и 140 удалений

Просмотреть файл

@ -48,7 +48,7 @@ CustomElementCallback::Call()
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);
@ -61,6 +61,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);
@ -323,7 +327,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");
@ -351,6 +357,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();
@ -379,6 +391,9 @@ CustomElementRegistry::CreateCustomElementCallback(
callback->SetArgs(*aArgs);
}
if (aAdoptedCallbackArgs) {
callback->SetAdoptedCallbackArgs(*aAdoptedCallbackArgs);
}
return Move(callback);
}
@ -388,7 +403,7 @@ CustomElementRegistry::SyncInvokeReactions(nsIDocument::ElementCallbackType aTyp
CustomElementDefinition* aDefinition)
{
auto callback = CreateCustomElementCallback(aType, aCustomElement, nullptr,
aDefinition);
nullptr, aDefinition);
if (!callback) {
return;
}
@ -407,6 +422,7 @@ CustomElementRegistry::SyncInvokeReactions(nsIDocument::ElementCallbackType aTyp
CustomElementRegistry::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition)
{
CustomElementDefinition* definition = aDefinition;
@ -419,7 +435,8 @@ CustomElementRegistry::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType
}
auto callback =
CreateCustomElementCallback(aType, aCustomElement, aArgs, definition);
CreateCustomElementCallback(aType, aCustomElement, aArgs,
aAdoptedCallbackArgs, definition);
if (!callback) {
return;
}
@ -935,7 +952,7 @@ CustomElementRegistry::Upgrade(Element* aElement,
};
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAttributeChanged,
aElement,
&args, aDefinition);
&args, nullptr, aDefinition);
}
}
}
@ -943,7 +960,7 @@ CustomElementRegistry::Upgrade(Element* aElement,
// Step 4.
if (aElement->IsInComposedDoc()) {
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eConnected, aElement,
nullptr, aDefinition);
nullptr, nullptr, aDefinition);
}
// Step 5.
@ -967,7 +984,8 @@ CustomElementRegistry::Upgrade(Element* aElement,
// This is for old spec.
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eCreated,
aElement, nullptr, aDefinition);
aElement, nullptr,
nullptr, aDefinition);
}
//-----------------------------------------------------
@ -1150,6 +1168,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

Просмотреть файл

@ -40,6 +40,12 @@ struct LifecycleCallbackArgs
nsString namespaceURI;
};
struct LifecycleAdoptedCallbackArgs
{
nsCOMPtr<nsIDocument> mOldDocument;
nsCOMPtr<nsIDocument> mNewDocument;
};
class CustomElementCallback
{
public:
@ -56,6 +62,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;
@ -65,6 +78,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;
@ -374,6 +388,7 @@ public:
static void EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
Element* aCustomElement,
LifecycleCallbackArgs* aArgs,
LifecycleAdoptedCallbackArgs* aAdoptedCallbackArgs,
CustomElementDefinition* aDefinition);
void GetCustomPrototype(nsIAtom* aAtom,
@ -394,7 +409,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

Просмотреть файл

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

Просмотреть файл

@ -10180,6 +10180,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.
@ -10188,6 +10189,7 @@ nsContentUtils::EnqueueLifecycleCallback(nsIDocument::ElementCallbackType aType,
}
CustomElementRegistry::EnqueueLifecycleCallback(aType, aCustomElement, aArgs,
aAdoptedCallbackArgs,
aDefinition);
}

Просмотреть файл

@ -136,6 +136,7 @@ class EventTarget;
class IPCDataTransfer;
class IPCDataTransferItem;
struct LifecycleCallbackArgs;
struct LifecycleAdoptedCallbackArgs;
class NodeInfo;
class nsIContentChild;
class nsIContentParent;
@ -3016,6 +3017,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,

Просмотреть файл

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

Просмотреть файл

@ -527,6 +527,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) {

Просмотреть файл

@ -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;
};

Просмотреть файл

@ -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

Просмотреть файл

@ -1,5 +0,0 @@
[custom-element-reaction-queue.html]
type: testharness
[Mutating another custom element inside adopted callback must invoke all pending callbacks on the mutated element]
expected: FAIL

Просмотреть файл

@ -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

Просмотреть файл

@ -3,9 +3,6 @@
[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
[importNode on Document must construct a new custom element when importing a custom element from a template]
expected: FAIL

Просмотреть файл

@ -19,9 +19,6 @@
expected: FAIL
[undefined must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[insertAdjacentElement on Element must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[innerHTML on Element must enqueue a connected reaction for a newly constructed custom element]

Просмотреть файл

@ -8,12 +8,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

Просмотреть файл

@ -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

Просмотреть файл

@ -9,12 +9,6 @@
[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
[surroundContents on Range must enqueue a disconnected reaction, an adopted reaction, and a connected reaction when the custom element was in another document]
expected: FAIL
[createContextualFragment on Range must construct a custom element]
expected: FAIL