Skip to content

Commit

Permalink
Add tests for connected/disconnected while disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Jul 17, 2019
1 parent 933995a commit 658c885
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 13 deletions.
10 changes: 8 additions & 2 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN

import { LegacyElementMixin } from './legacy-element-mixin.js';
import { legacyOptimizations } from '../utils/settings.js';
import { wrap } from '../utils/wrap.js';

const lifecycleProps = {
attached: true,
Expand Down Expand Up @@ -250,6 +251,8 @@ function GenerateClassFromInfo(info, Base, behaviors) {
}

// Prevent element from enabling properties when it's upgrade disabled.
// Normally overriding connectedCallback would be enough, but dom-* elements
// enable elements early.
/** @override */
_enableProperties() {
if (!this.__isUpgradeDisabled) {
Expand All @@ -269,10 +272,10 @@ function GenerateClassFromInfo(info, Base, behaviors) {
if (name == DISABLED_ATTR) {
// When disable-upgrade is removed, intialize properties and
// provoke connectedCallback if the element is already connected.
if (!this.__dataEnabled && value == null) {
if (this.__isUpgradeDisabled && value == null) {
super._initializeProperties();
this.__isUpgradeDisabled = false;
if (this.isConnected) {
if (wrap(this).isConnected) {
super.connectedCallback();
}
}
Expand All @@ -283,6 +286,7 @@ function GenerateClassFromInfo(info, Base, behaviors) {
}

// Prevent element from connecting when it's upgrade disabled.
// This prevents user code in `attached` from being called.
/** @override */
connectedCallback() {
if (!this.__isUpgradeDisabled) {
Expand All @@ -291,6 +295,8 @@ function GenerateClassFromInfo(info, Base, behaviors) {
}

// Prevent element from disconnecting when it's upgrade disabled.
// This avoids allowing user code `detached` from being called without a
// paired call to `attached`.
/** @override */
disconnectedCallback() {
if (!this.__isUpgradeDisabled) {
Expand Down
16 changes: 10 additions & 6 deletions lib/mixins/disable-upgrade-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* rights grant found at http://polymer.github.io/PATENTS.txt
*/
import { ElementMixin } from './element-mixin.js';

import { dedupingMixin } from '../utils/mixin.js';
import { wrap } from '../utils/wrap.js';

const DISABLED_ATTR = 'disable-upgrade';

Expand Down Expand Up @@ -83,14 +83,15 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
// Prevent element from initializing properties when it's upgrade disabled.
/** @override */
_initializeProperties() {
if (!this.hasAttribute(DISABLED_ATTR)) {
super._initializeProperties();
} else {
if (this.hasAttribute(DISABLED_ATTR)) {
this.__isUpgradeDisabled = true;
} else {
super._initializeProperties();
}
}

// Prevent element from enabling properties when it's upgrade disabled.
// Normally overriding connectedCallback would be enough, but dom-* elements
/** @override */
_enableProperties() {
if (!this.__isUpgradeDisabled) {
Expand All @@ -110,10 +111,10 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
if (name == DISABLED_ATTR) {
// When disable-upgrade is removed, intialize properties and
// provoke connectedCallback if the element is already connected.
if (!this.__dataEnabled && value == null) {
if (this.__isUpgradeDisabled && value == null) {
super._initializeProperties();
this.__isUpgradeDisabled = false;
if (this.isConnected) {
if (wrap(this).isConnected) {
super.connectedCallback();
}
}
Expand All @@ -124,6 +125,7 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
}

// Prevent element from connecting when it's upgrade disabled.
// This prevents user code in `attached` from being called.
/** @override */
connectedCallback() {
if (!this.__isUpgradeDisabled) {
Expand All @@ -132,6 +134,8 @@ export const DisableUpgradeMixin = dedupingMixin((base) => {
}

// Prevent element from disconnecting when it's upgrade disabled.
// This avoids allowing user code `detached` from being called without a
// paired call to `attached`.
/** @override */
disconnectedCallback() {
if (!this.__isUpgradeDisabled) {
Expand Down
Loading

0 comments on commit 658c885

Please sign in to comment.