Skip to content

Commit

Permalink
Behavior property copying fixes
Browse files Browse the repository at this point in the history
* ensure element has `is` on prototype early as this is sometimes checked in user code.
* ensure properties copied onto elements from info/behaviors are forced to configurable so they can be re-configured by later behaviors.
* add `_noAccessors` optimization for faster property copying
  • Loading branch information
Steven Orvell committed Nov 10, 2018
1 parent 83c0e9d commit fbf827d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
beforeRegister: true,
registered: true,
attributeChanged: true,
behaviors: true
behaviors: true,
_noAccessors: true
};

const excludeOnBehaviors = Object.assign({
Expand All @@ -45,13 +46,19 @@
}, excludeOnInfo);

function copyProperties(source, target, excludeProps) {
const noAccessors = source._noAccessors;
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.
if (!(p in excludeProps)) {
let pd = Object.getOwnPropertyDescriptor(source, p);
if (pd) {
Object.defineProperty(target, p, pd);
if (noAccessors) {
target[p] = source[p];
} else {
let pd = Object.getOwnPropertyDescriptor(source, p);
if (pd) {
// ensure property is configurable so that a later behavior can
// re-configure it.
pd.configurable = true;
Object.defineProperty(target, p, pd);
}
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,20 @@
behaviors: [window.BehaviorA]
});

Polymer({
is: 'no-accessors-behavior',
behaviors: [{
_noAccessors: true,
properties: {
nug: String
},
foo: function() {},
bar: true
}],
_noAccessors: true,
zot: 'zot'
});

});
</script>

Expand Down Expand Up @@ -447,6 +461,12 @@
</template>
</test-fixture>

<test-fixture id="no-accessors-behavior">
<template>
<no-accessors-behavior></no-accessors-behavior>
</template>
</test-fixture>

<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -500,6 +520,15 @@
assert.notOk(el.listeners);
});

test('properties on objects marked with `_noAccessors` are copied to class', function() {
const el = fixture('no-accessors-behavior');
assert.ok(el.foo);
assert.isTrue(el.bar);
assert.equal(el.zot, 'zot');
el.setAttribute('nug', 'nug');
assert.equal(el.nug, 'nug');
});

});

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

0 comments on commit fbf827d

Please sign in to comment.