Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider a class "registered" when it is the top-most prototype #5569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,23 @@ function GenerateClassFromInfo(info, Base, behaviors) {
// only proceed if the generated class' prototype has not been registered.
const generatedProto = PolymerGenerated.prototype;
if (!generatedProto.hasOwnProperty('__hasRegisterFinished')) {
generatedProto.__hasRegisterFinished = true;
// make sure legacy lifecycle is called on the *element*'s prototype
// and not the generated class prototype; if the element has been
// extended, these are *not* the same.
const proto = Object.getPrototypeOf(this);
// Only set flag when generated prototype itself is registered,
// as this element may be extended from, and needs to run `registered`
// on all behaviors on the subclass as well.
if (proto === generatedProto) {
generatedProto.__hasRegisterFinished = true;
}
// ensure superclass is registered first.
super._registered();
// copy properties onto the generated class lazily if we're optimizing,
if (legacyOptimizations) {
if (legacyOptimizations && !Object.hasOwnProperty(generatedProto, '__hasCopiedProperties')) {
generatedProto.__hasCopiedProperties = true;
copyPropertiesToProto(generatedProto);
}
// make sure legacy lifecycle is called on the *element*'s prototype
// and not the generated class prototype; if the element has been
// extended, these are *not* the same.
const proto = Object.getPrototypeOf(this);
let list = lifecycle.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
Expand Down
40 changes: 40 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,23 @@
}
});

const inheritedBehavior = {
registered() {
this.foo = {};
}
};

Polymer({
is: 'x-base-behavior',
behaviors: [inheritedBehavior],
hasFoo() {
return Boolean(this.foo);
}
});

class XInherit extends customElements.get('x-base-behavior') {}
customElements.define('x-inherit-behavior', XInherit);

</script>

<test-fixture id="single">
Expand Down Expand Up @@ -525,6 +542,18 @@
</template>
</test-fixture>

<test-fixture id="sub-behavior">
<template>
<x-inherit-behavior></x-inherit-behavior>
</template>
</test-fixture>

<test-fixture id="sup-behavior">
<template>
<x-base-behavior></x-base-behavior>
</template>
</test-fixture>

<script type="module">
import { Polymer } from '../../polymer-legacy.js';

Expand Down Expand Up @@ -792,6 +821,17 @@

});

suite('inherited behaviors', function() {

test('behaviors are set up correctly on legacy element when subclass boots first', function() {
const sub = fixture('sub-behavior');
const sup = fixture('sup-behavior');
assert(sub.hasFoo(), 'subclass should have registered property defined');
assert(sup.hasFoo(), 'superclass should have registered property defined');
});

});

</script>

</body>
Expand Down
18 changes: 16 additions & 2 deletions test/unit/mixin-behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,17 @@

const Base = mixinBehaviors([{
registered() {
this.usedExtendedProto = this.canUseExtendedProto;
this.usedExtendedProto = Boolean(this.canUseExtendedProto);
}
}], PolymerElement);

customElements.define('registered-super', Base);

customElements.define('registered-proto', mixinBehaviors([
{canUseExtendedProto: true}
], Base));


</script>

<script type="module">
Expand Down Expand Up @@ -485,6 +488,12 @@
</template>
</test-fixture>

<test-fixture id="registered-super">
<template>
<registered-super></registered-super>
</template>
</test-fixture>

<test-fixture id="extended-observed-attributes">
<template>
<extended-observed-attributes b1="b1" e1="e1" b2="b2" e2="e2"></extended-observed-attributes>
Expand Down Expand Up @@ -554,7 +563,7 @@

test('extending element with behaviors with registered properly registers', function() {
var el = fixture('registered-ext');
assert.equal(el.registeredCount, 4);
assert.equal(el.registeredCount, 7);
assert.equal(el.registeredBehaviors.length, 3);
assert.equal(el.registeredBehaviors, el.behaviors);
assert.deepEqual(el.registeredProps, [true, true, true]);
Expand All @@ -571,6 +580,11 @@
assert.isTrue(el.usedExtendedProto);
});

test('registered called on super-class prototype when extended', function() {
const el = fixture('registered-super');
assert.isFalse(el.usedExtendedProto);
});

});

suite('behavior lifecycle', function() {
Expand Down