Skip to content

Commit

Permalink
connectedCallback is invoked during the removal of the element inside…
Browse files Browse the repository at this point in the history
… another element's connectedCallback

https://bugs.webkit.org/show_bug.cgi?id=183586
<rdar://problem/38403504>

Reviewed by Frédéric Wang.

Source/WebCore:

Align WebKit's behavior with Chrome/Firefox with regards to WICG/webcomponents#760

After much discussion, it's unclear that there is a clear path forward to fixing the oddness that
the presence of a custom element reaction changes the timing at which another reaction callback gets invoked.
So matching Chrome/Firefox behaviors in this case seems the path of the least resistance to interoperability.

Namely, this patch makes WebKit not insert a custom element to the appropriate element queue when the element
does not have a matching reaction callback. Put it another way, steps 3-5 in would be done before step 6 in:
https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-a-custom-element-callback-reaction
    1. Let definition be element's custom element definition.
    2. Let callback be the value of the entry in definition's lifecycle callbacks with key callbackName.
    3. If callback is null, then return
    4. If callbackName is "attributeChangedCallback", then:
        1. Let attributeName be the first element of args.
        2. If definition's observed attributes does not contain attributeName, then return.
    5. Add a new callback reaction to element's custom element reaction queue, with callback function callback
       and arguments args.
    6. Enqueue an element on the appropriate element queue given element.

Test: fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback.html

* dom/CustomElementReactionQueue.cpp:
(WebCore::CustomElementReactionQueue::enqueueElementUpgrade):
(WebCore::CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueDisconnectedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueAttributeChangedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueuePostUpgradeReactions):
(WebCore::CustomElementReactionQueue::enqueueElementOnAppropriateElementQueue): Renamed from ensureCurrentQueue.
* dom/CustomElementReactionQueue.h:

LayoutTests:

Added a W3C style testharness test.

* fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback-expected.txt: Added.
* fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback.html: Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@239096 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
rniwa@webkit.org committed Dec 12, 2018
1 parent 4654f1c commit a747fd9
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 24 deletions.
13 changes: 13 additions & 0 deletions LayoutTests/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
2018-12-10 Ryosuke Niwa <rniwa@webkit.org>

connectedCallback is invoked during the removal of the element inside another element's connectedCallback
https://bugs.webkit.org/show_bug.cgi?id=183586
<rdar://problem/38403504>

Reviewed by Frédéric Wang.

Added a W3C style testharness test.

* fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback-expected.txt: Added.
* fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback.html: Added.

2018-12-11 Justin Fan <justin_fan@apple.com>

[WebGPU] Implement WebGPUBuffer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

PASS Disconnecting an element with disconnectedCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the removal completes
PASS Disconnecting an element without disconnectedCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the removal completes
PASS Connecting a element with connectedCallback while it has a disconnectedCallback in its custom element reaction queue must result in disconnectedCallback getting invoked before the insertion completes
PASS Connecting an element without connectedCallback while it has a disconnectedCallback in its custom element reaction queue must not result in disconnectedCallback getting invoked before the insertion completes
PASS Adopting an element with adoptingCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the adoption completes
PASS Adopting an element without adoptingCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the adoption completes
PASS Setting an observed attribute on an element with attributeChangedCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the attribute change completes
PASS Setting an observed attribute on an element with attributeChangedCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the attribute change completes

Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<!DOCTYPE html>
<html>
<head>
<title>Custom Elements: must enqueue an element on the appropriate element queue after checking callback is null and the attribute name</title>
<meta name="author" title="Ryosuke Niwa" href="mailto:rniwa@webkit.org">
<meta name="assert" content="To enqueue a custom element callback reaction, the callback must be checked of being null and whether the attribute name is observed or not">
<link rel="help" content="https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-a-custom-element-callback-reaction">
<link rel="help" content="https://github.com/w3c/webcomponents/issues/760">
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../imported/w3c/web-platform-tests/custom-elements/resources/custom-elements-helpers.js"></script>
</head>
<body>
<script>

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
const child = this.firstChild;
child.remove();
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
connectedCallback() { logs.push('connected'); }
disconnectedCallback() { logs.push('disconnected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);

contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'connected', 'disconnected', 'end']);
}, 'Disconnecting an element with disconnectedCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the removal completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
const child = this.firstChild;
child.remove();
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
connectedCallback() { logs.push('connected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);

contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'end', 'connected']);
}, 'Disconnecting an element without disconnectedCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the removal completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
disconnectedCallback()
{
logs.push('begin');
contentDocument.body.appendChild(this.firstChild);
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
connectedCallback() { logs.push('connected'); }
disconnectedCallback() { logs.push('disconnected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
parent.remove();
assert_array_equals(logs, ['connected', 'begin', 'disconnected', 'connected', 'end']);
}, 'Connecting a element with connectedCallback while it has a disconnectedCallback in its custom element reaction queue must result in disconnectedCallback getting invoked before the insertion completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
disconnectedCallback()
{
logs.push('begin');
contentDocument.body.appendChild(this.firstChild);
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
disconnectedCallback() { logs.push('disconnected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
parent.remove();
assert_array_equals(logs, ['begin', 'end', 'disconnected']);
}, 'Connecting an element without connectedCallback while it has a disconnectedCallback in its custom element reaction queue must not result in disconnectedCallback getting invoked before the insertion completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
document.adoptNode(this.firstChild);
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
adoptedCallback() { logs.push('adopted'); }
connectedCallback() { logs.push('connected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'connected', 'adopted', 'end']);
}, 'Adopting an element with adoptingCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the adoption completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
document.adoptNode(this.firstChild);
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
connectedCallback() { logs.push('connected'); }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'end', 'connected']);
}, 'Adopting an element without adoptingCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the adoption completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
this.firstChild.setAttribute('title', 'foo');
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
attributeChangedCallback() { logs.push('attributeChanged'); }
connectedCallback() { logs.push('connected'); }
static get observedAttributes() { return ['title']; }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'connected', 'attributeChanged', 'end']);
}, 'Setting an observed attribute on an element with attributeChangedCallback while it has a connectedCallback in its custom element reaction queue must result in connectedCallback getting invoked before the attribute change completes');

test_with_window((contentWindow, contentDocument) => {
class ParentElement extends contentWindow.HTMLElement {
connectedCallback()
{
logs.push('begin');
this.firstChild.setAttribute('lang', 'en');
logs.push('end');
}
}
contentWindow.customElements.define('parent-element', ParentElement);

const logs = [];
class ChildElement extends contentWindow.HTMLElement {
attributeChangedCallback() { logs.push('attributeChanged'); }
connectedCallback() { logs.push('connected'); }
static get observedAttributes() { return ['title']; }
}
contentWindow.customElements.define('child-element', ChildElement);

const parent = new ParentElement;
const child = new ChildElement;
parent.appendChild(child);
contentDocument.body.appendChild(parent);
assert_array_equals(logs, ['begin', 'end', 'connected']);
}, 'Setting an observed attribute on an element with attributeChangedCallback while it has a connectedCallback in its custom element reaction queue must not result in connectedCallback getting invoked before the attribute change completes');

</script>
</body>
</html>
39 changes: 39 additions & 0 deletions Source/WebCore/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
2018-12-10 Ryosuke Niwa <rniwa@webkit.org>

connectedCallback is invoked during the removal of the element inside another element's connectedCallback
https://bugs.webkit.org/show_bug.cgi?id=183586
<rdar://problem/38403504>

Reviewed by Frédéric Wang.

Align WebKit's behavior with Chrome/Firefox with regards to https://github.com/w3c/webcomponents/issues/760

After much discussion, it's unclear that there is a clear path forward to fixing the oddness that
the presence of a custom element reaction changes the timing at which another reaction callback gets invoked.
So matching Chrome/Firefox behaviors in this case seems the path of the least resistance to interoperability.

Namely, this patch makes WebKit not insert a custom element to the appropriate element queue when the element
does not have a matching reaction callback. Put it another way, steps 3-5 in would be done before step 6 in:
https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-a-custom-element-callback-reaction
1. Let definition be element's custom element definition.
2. Let callback be the value of the entry in definition's lifecycle callbacks with key callbackName.
3. If callback is null, then return
4. If callbackName is "attributeChangedCallback", then:
1. Let attributeName be the first element of args.
2. If definition's observed attributes does not contain attributeName, then return.
5. Add a new callback reaction to element's custom element reaction queue, with callback function callback
and arguments args.
6. Enqueue an element on the appropriate element queue given element.

Test: fast/custom-elements/enqueue-custom-element-callback-reactions-inside-another-callback.html

* dom/CustomElementReactionQueue.cpp:
(WebCore::CustomElementReactionQueue::enqueueElementUpgrade):
(WebCore::CustomElementReactionQueue::enqueueConnectedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueDisconnectedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueAdoptedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueueAttributeChangedCallbackIfNeeded):
(WebCore::CustomElementReactionQueue::enqueuePostUpgradeReactions):
(WebCore::CustomElementReactionQueue::enqueueElementOnAppropriateElementQueue): Renamed from ensureCurrentQueue.
* dom/CustomElementReactionQueue.h:

2018-12-11 Justin Fan <justin_fan@apple.com>

[WebGPU] Implement WebGPUBuffer, and some nullibility consistency in WebGPU
Expand Down
Loading

0 comments on commit a747fd9

Please sign in to comment.