Skip to content

Commit

Permalink
Fixes #4550. Ensure that detached cannot run before an element is “…
Browse files Browse the repository at this point in the history
…readied.” This fixes an issue that allowed an element with `disable-upgrade` to process the `detached` callback.
  • Loading branch information
Steven Orvell committed May 17, 2017
1 parent e6c94cc commit b967c5e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/mini/ready.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
(function() {

var baseAttachedCallback = Polymer.Base.attachedCallback;
var baseDetachedCallback = Polymer.Base.detachedCallback;

Polymer.Base._addFeature({

Expand Down Expand Up @@ -152,6 +153,11 @@
this._attachedPending = false;
this.attachedCallback();
}
// only call detached if the element is actually detached
if (this._detachedPending && !Polymer.dom(document.body).deepContains(this)) {
this._attachedPending = false;
this.detachedCallback();
}
},

// for system overriding
Expand All @@ -175,8 +181,27 @@
} else {
this._attachedPending = true;
}
},

/**
* Polymer library implementation of the Custom Elements `detachedCallback`.
*
* Note, users should not override `detachedCallback`, and instead should
* implement the `detached` method on Polymer elements to receive
* detached-time callbacks.
*
* @protected
*/
detachedCallback: function() {
if (this._readied) {
baseDetachedCallback.call(this);
} else {
this._detachedPending = true;
}
}



});

})();
Expand Down
64 changes: 64 additions & 0 deletions test/unit/element-disable-upgrade.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,42 @@
</script>
</dom-module>

<dom-module id="x-attach">
<template>
<style>
:host {
display: block;
}
</style>
<div id="child">Live!</div>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-attach',
attached: function() {
this._wasAttached = true;
},
detached: function() {
this._wasDetached = true;
}
});
});
</script>
</dom-module>

<test-fixture id="simple">
<template>
<x-lazy disable-upgrade></x-lazy>
</template>
</test-fixture>

<test-fixture id="attach">
<template>
<x-attach disable-upgrade></x-attach>
</template>
</test-fixture>

<dom-module id="x-complicated-child">
<script>
HTMLImports.whenReady(function() {
Expand Down Expand Up @@ -141,6 +171,40 @@
assert.ok(el.$.child);
});
});
suite('disableUpgrade attach/detach', function() {
var el;
setup(function() {
el = fixture('attach');
});
test('attached does not fire when element is not yet enabled', function() {
assert.notOk(el._wasAttached);
el.removeAttribute('disable-upgrade');
assert.ok(el._wasAttached);
});
test('detached does not fire when element is not yet enabled', function() {
el.parentNode.removeChild(el);
Polymer.dom.flush();
assert.notOk(el._wasAttached);
assert.notOk(el._wasDetached);
el.removeAttribute('disable-upgrade');
assert.ok(el._wasAttached);
assert.ok(el._wasDetached);
});
test('detached does not fire when element is detached/attached when not yet enabled', function() {
var parent = el.parentNode;
parent.removeChild(el);
Polymer.dom.flush();
assert.notOk(el._wasAttached);
assert.notOk(el._wasDetached);
parent.appendChild(el);
Polymer.dom.flush();
assert.notOk(el._wasAttached);
assert.notOk(el._wasDetached);
el.removeAttribute('disable-upgrade');
assert.ok(el._wasAttached);
assert.notOk(el._wasDetached);
});
});
suite('disableUpgrade and Databinding', function() {
test('binding to disable-upgrade with true', function() {
var el = fixture('databind-lazy');
Expand Down

0 comments on commit b967c5e

Please sign in to comment.