Skip to content

Commit b967c5e

Browse files
author
Steven Orvell
committed
Fixes #4550. Ensure that detached cannot run before an element is “readied.” This fixes an issue that allowed an element with disable-upgrade to process the detached callback.
1 parent e6c94cc commit b967c5e

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/mini/ready.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(function() {
3333

3434
var baseAttachedCallback = Polymer.Base.attachedCallback;
35+
var baseDetachedCallback = Polymer.Base.detachedCallback;
3536

3637
Polymer.Base._addFeature({
3738

@@ -152,6 +153,11 @@
152153
this._attachedPending = false;
153154
this.attachedCallback();
154155
}
156+
// only call detached if the element is actually detached
157+
if (this._detachedPending && !Polymer.dom(document.body).deepContains(this)) {
158+
this._attachedPending = false;
159+
this.detachedCallback();
160+
}
155161
},
156162

157163
// for system overriding
@@ -175,8 +181,27 @@
175181
} else {
176182
this._attachedPending = true;
177183
}
184+
},
185+
186+
/**
187+
* Polymer library implementation of the Custom Elements `detachedCallback`.
188+
*
189+
* Note, users should not override `detachedCallback`, and instead should
190+
* implement the `detached` method on Polymer elements to receive
191+
* detached-time callbacks.
192+
*
193+
* @protected
194+
*/
195+
detachedCallback: function() {
196+
if (this._readied) {
197+
baseDetachedCallback.call(this);
198+
} else {
199+
this._detachedPending = true;
200+
}
178201
}
179202

203+
204+
180205
});
181206

182207
})();

test/unit/element-disable-upgrade.html

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,42 @@
3232
</script>
3333
</dom-module>
3434

35+
<dom-module id="x-attach">
36+
<template>
37+
<style>
38+
:host {
39+
display: block;
40+
}
41+
</style>
42+
<div id="child">Live!</div>
43+
</template>
44+
<script>
45+
HTMLImports.whenReady(function() {
46+
Polymer({
47+
is: 'x-attach',
48+
attached: function() {
49+
this._wasAttached = true;
50+
},
51+
detached: function() {
52+
this._wasDetached = true;
53+
}
54+
});
55+
});
56+
</script>
57+
</dom-module>
58+
3559
<test-fixture id="simple">
3660
<template>
3761
<x-lazy disable-upgrade></x-lazy>
3862
</template>
3963
</test-fixture>
4064

65+
<test-fixture id="attach">
66+
<template>
67+
<x-attach disable-upgrade></x-attach>
68+
</template>
69+
</test-fixture>
70+
4171
<dom-module id="x-complicated-child">
4272
<script>
4373
HTMLImports.whenReady(function() {
@@ -141,6 +171,40 @@
141171
assert.ok(el.$.child);
142172
});
143173
});
174+
suite('disableUpgrade attach/detach', function() {
175+
var el;
176+
setup(function() {
177+
el = fixture('attach');
178+
});
179+
test('attached does not fire when element is not yet enabled', function() {
180+
assert.notOk(el._wasAttached);
181+
el.removeAttribute('disable-upgrade');
182+
assert.ok(el._wasAttached);
183+
});
184+
test('detached does not fire when element is not yet enabled', function() {
185+
el.parentNode.removeChild(el);
186+
Polymer.dom.flush();
187+
assert.notOk(el._wasAttached);
188+
assert.notOk(el._wasDetached);
189+
el.removeAttribute('disable-upgrade');
190+
assert.ok(el._wasAttached);
191+
assert.ok(el._wasDetached);
192+
});
193+
test('detached does not fire when element is detached/attached when not yet enabled', function() {
194+
var parent = el.parentNode;
195+
parent.removeChild(el);
196+
Polymer.dom.flush();
197+
assert.notOk(el._wasAttached);
198+
assert.notOk(el._wasDetached);
199+
parent.appendChild(el);
200+
Polymer.dom.flush();
201+
assert.notOk(el._wasAttached);
202+
assert.notOk(el._wasDetached);
203+
el.removeAttribute('disable-upgrade');
204+
assert.ok(el._wasAttached);
205+
assert.notOk(el._wasDetached);
206+
});
207+
});
144208
suite('disableUpgrade and Databinding', function() {
145209
test('binding to disable-upgrade with true', function() {
146210
var el = fixture('databind-lazy');

0 commit comments

Comments
 (0)