Skip to content

Commit

Permalink
Avoid copying certain properties from behaviors
Browse files Browse the repository at this point in the history
This better matches what Polymer 1.x did for:
* hostAttributes
* listeners
* properties
* observers
  • Loading branch information
Steven Orvell committed Nov 9, 2018
1 parent d5e0043 commit cf30a8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,39 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN

import { LegacyElementMixin } from './legacy-element-mixin.js';

const metaProps = {
const lifecycleProps = {
attached: true,
detached: true,
ready: true,
created: true,
beforeRegister: true,
registered: true,
attributeChanged: true,
listeners: true,
hostAttributes: true
};

const excludeProps = Object.assign({
const excludeOnInfo = {
attached: true,
detached: true,
ready: true,
created: true,
beforeRegister: true,
registered: true,
attributeChanged: true,
behaviors: true
}, metaProps);
};

const lifecycleProps = Object.assign({
const excludeOnBehaviors = Object.assign({
listeners: true,
hostAttributes: true
}, metaProps);
hostAttributes: true,
properties: true,
observers: true,
}, excludeOnInfo);



function copyProperties(source, target) {
function copyProperties(source, target, excludeProps) {
for (let p in source) {
// NOTE: cannot copy `excludeProps` methods onto prototype at least because
// `super.ready` must be called and is not included in the user fn.
Expand Down Expand Up @@ -93,12 +106,12 @@ export function mixinBehaviors(behaviors, klass) {
// (again same as 1.x)
function applyBehaviors(proto, behaviors, lifecycle) {
for (let i=0; i<behaviors.length; i++) {
applyInfo(proto, behaviors[i], lifecycle);
applyInfo(proto, behaviors[i], lifecycle, excludeOnBehaviors);
}
}

function applyInfo(proto, info, lifecycle) {
copyProperties(info, proto);
function applyInfo(proto, info, lifecycle, excludeProps) {
copyProperties(info, proto, excludeProps);
for (let p in lifecycleProps) {
if (info[p]) {
lifecycle[p] = lifecycle[p] || [];
Expand Down Expand Up @@ -278,7 +291,7 @@ function GenerateClassFromInfo(info, Base, behaviors) {
if (behaviorList) {
applyBehaviors(proto, behaviorList, lifecycle);
}
applyInfo(proto, info, lifecycle);
applyInfo(proto, info, lifecycle, excludeOnInfo);
let list = lifecycle.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
}
},

observers: [],

_simpleProperty: 'A',

hostAttributes: {
Expand Down Expand Up @@ -372,6 +374,11 @@
}
});

Polymer({
is: 'behavior-properties',
behaviors: [window.BehaviorA]
});

</script>

<test-fixture id="single">
Expand Down Expand Up @@ -434,6 +441,12 @@
</template>
</test-fixture>

<test-fixture id="behavior-properties">
<template>
<behavior-properties></behavior-properties>
</template>
</test-fixture>

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

Expand Down Expand Up @@ -480,6 +493,14 @@
assert.equal(el.computeA, true);
});

test('special properties not copied from behavior to element', function() {
const el = fixture('behavior-properties');
assert.notOk(el.properties);
assert.notOk(el.observers);
assert.notOk(el.hostAttributes);
assert.notOk(el.listeners);
});

});

suite('behavior.registered', function() {
Expand Down

0 comments on commit cf30a8c

Please sign in to comment.