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

Allow value to merge from previous behavior property declaration. Fixes #5503 #5504

Merged
merged 3 commits into from
Mar 6, 2019
Merged
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
24 changes: 22 additions & 2 deletions lib/legacy/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ function flattenBehaviors(behaviors, list, exclude) {
return list;
}

/**
* Copies property descriptors from source to target, overwriting all fields
* of any previous descriptor for a property *except* for `value`, which is
* merged in from the target if it does not exist on the source.
*
* @param {*} target Target properties object
* @param {*} source Source properties object
*/
function mergeProperties(target, source) {
for (const p in source) {
const targetInfo = target[p];
const sourceInfo = source[p];
if (!('value' in sourceInfo) && targetInfo && ('value' in targetInfo)) {
target[p] = Object.assign({value: targetInfo.value}, sourceInfo);
} else {
target[p] = sourceInfo;
}
}
}

/* Note about construction and extension of legacy classes.
[Changed in Q4 2018 to optimize performance.]

Expand Down Expand Up @@ -227,10 +247,10 @@ function GenerateClassFromInfo(info, Base, behaviors) {
const properties = {};
if (behaviorList) {
for (let i=0; i < behaviorList.length; i++) {
Object.assign(properties, behaviorList[i].properties);
mergeProperties(properties, behaviorList[i].properties);
}
}
Object.assign(properties, info.properties);
mergeProperties(properties, info.properties);
return properties;
}

Expand Down
20 changes: 12 additions & 8 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,26 @@
behaviors: [
{
properties: {
foo: { value: true },
bar: { value: true}
foo: { value: 'a' },
bar: { value: 'a' },
ziz: { value: 'a' }
}
},
{
properties: {
foo: { value: true },
foo: { value: 'b' },
bar: String,
zot: {value: true}
zot: { value: 'b' },
ziz: { value: 'b' }
}
},

],

properties: {
foo: String,
zot: String
zot: String,
ziz: { value: 'c' }
}
});

Expand Down Expand Up @@ -587,9 +590,10 @@

test('behavior default values can be overridden', function() {
const el = fixture('override-default-value');
assert.notOk(el.foo);
assert.notOk(el.bar);
assert.notOk(el.zot);
assert.equal(el.foo, 'b');
assert.equal(el.bar, 'a');
assert.equal(el.zot, 'b');
assert.equal(el.ziz, 'c');
});

test('readOnly not applied when property was previously observed', function() {
Expand Down