Bug 1102502 - Fire custom elements attachedCallback when elements are created and inserted into the document by the parser. r=smaug

This commit is contained in:
William Chen 2014-12-22 22:35:32 -08:00
parent 86409607b7
commit f20229f5d0
5 changed files with 62 additions and 30 deletions

View File

@ -445,13 +445,27 @@ CustomElementCallback::Call()
ErrorResult rv;
switch (mType) {
case nsIDocument::eCreated:
{
// For the duration of this callback invocation, the element is being created
// flag must be set to true.
mOwnerData->mElementIsBeingCreated = true;
// The callback hasn't actually been invoked yet, but we need to flip
// this now in order to enqueue the attached callback. This is a spec
// bug (w3c bug 27437).
mOwnerData->mCreatedCallbackInvoked = true;
// If ELEMENT is in a document and this document has a browsing context,
// enqueue attached callback for ELEMENT.
nsIDocument* document = mThisObject->GetUncomposedDoc();
if (document && document->GetDocShell()) {
document->EnqueueLifecycleCallback(nsIDocument::eAttached, mThisObject);
}
static_cast<LifecycleCreatedCallback *>(mCallback.get())->Call(mThisObject, rv);
mOwnerData->mElementIsBeingCreated = false;
break;
}
case nsIDocument::eAttached:
static_cast<LifecycleAttachedCallback *>(mCallback.get())->Call(mThisObject, rv);
break;
@ -6274,16 +6288,6 @@ nsDocument::RegisterElement(JSContext* aCx, const nsAString& aType,
}
EnqueueLifecycleCallback(nsIDocument::eCreated, elem, nullptr, definition);
//XXXsmaug It is unclear if we should use GetComposedDoc() here.
if (elem->GetUncomposedDoc()) {
// Normally callbacks can not be enqueued until the created
// callback has been invoked, however, the attached callback
// in element upgrade is an exception so pretend the created
// callback has been invoked.
elem->GetCustomElementData()->mCreatedCallbackInvoked = true;
EnqueueLifecycleCallback(nsIDocument::eAttached, elem, nullptr, definition);
}
}
}

View File

@ -6,6 +6,7 @@ support-files =
[test_bug1017896.html]
[test_content_element.html]
[test_custom_element_adopt_callbacks.html]
[test_custom_element_callback_innerhtml.html]
[test_custom_element_clone_callbacks.html]
[test_custom_element_clone_callbacks_extended.html]
[test_nested_content_element.html]

View File

@ -0,0 +1,47 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1102502
-->
<head>
<title>Test for attached callback for element created in the document by the parser</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1102502">Bug 1102502</a>
<div id="container"></div>
<script>
SimpleTest.waitForExplicitFinish();
var attachedCallbackCount = 0;
var p = Object.create(HTMLElement.prototype);
p.createdCallback = function() {
ok(true, "createdCallback called.");
};
p.attachedCallback = function() {
ok(true, "attachedCallback should be called when the parser creates an element in the document.");
attachedCallbackCount++;
// attachedCallback should be called twice, once for the element created for innerHTML and
// once for the element created in this document.
if (attachedCallbackCount == 2) {
SimpleTest.finish();
}
}
document.registerElement("x-foo", { prototype: p });
var container = document.getElementById("container");
container.innerHTML = '<x-foo></x-foo>';
</script>
<x-foo></x-foo>
</body>
</html>

View File

@ -1,11 +0,0 @@
[attached-callback-test.html]
type: testharness
[Test attached callback if custom element is created via innerHTML property before registration. Document has no browsing context]
expected: FAIL
[Test attached callback if custom element is unregistered]
expected: FAIL
[Test attached callback. Registered element is created via innerHTML property. Document has browsing context]
expected: FAIL

View File

@ -1,17 +1,8 @@
[created-callback-invocation-order-test.html]
type: testharness
[Test attached callback is enqueued after created callback]
expected: FAIL
[Test attributeChanged callback is not enqueued before created callback started. Document has browsing context]
expected: FAIL
[Test attached callback is enqueued after created callback, but before created callback had started]
expected: FAIL
[Test attached callback is enqueued after created callback had started]
expected: FAIL
[Test detached callback is enqueued after created callback had started]
expected: FAIL