Merge pull request #3386 from TimvdLippe/behavior-unique-array

Make behaviors array unique
This commit is contained in:
Daniel Freedman 2016-02-16 11:02:16 -08:00
commit 3a6703f530
2 changed files with 27 additions and 10 deletions

View File

@ -65,6 +65,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}, },
_desugarSomeBehaviors: function(behaviors) { _desugarSomeBehaviors: function(behaviors) {
var behaviorSet = [];
// iteration 1 // iteration 1
behaviors = this._flattenBehaviorsList(behaviors); behaviors = this._flattenBehaviorsList(behaviors);
// iteration 2 // iteration 2
@ -72,9 +73,13 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
// `_mixinBehavior` has _first property wins_ behavior, this is done // `_mixinBehavior` has _first property wins_ behavior, this is done
// to optimize # of calls to `_copyOwnProperty` // to optimize # of calls to `_copyOwnProperty`
for (var i=behaviors.length-1; i>=0; i--) { for (var i=behaviors.length-1; i>=0; i--) {
this._mixinBehavior(behaviors[i]); var b = behaviors[i];
if (behaviorSet.indexOf(b) === -1) {
this._mixinBehavior(b);
behaviorSet.unshift(b);
}
} }
return behaviors; return behaviorSet;
}, },
_flattenBehaviorsList: function(behaviors) { _flattenBehaviorsList: function(behaviors) {
@ -141,7 +146,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}); });
// special properties on behaviors are not mixed in and are instead // special properties on behaviors are not mixed in and are instead
// either processed specially (e.g. listeners, properties) or available // either processed specially (e.g. listeners, properties) or available
// for calling via doBehavior (e.g. created, ready) // for calling via doBehavior (e.g. created, ready)
Polymer.Base._behaviorProperties = { Polymer.Base._behaviorProperties = {

View File

@ -133,21 +133,33 @@ suite('multi-behaviors element', function() {
assert.match(message, /behavior is null/); assert.match(message, /behavior is null/);
warned = true; warned = true;
}; };
Polymer({ Polymer({
is: 'behavior-null',
behaviors: [ behaviors: [
null null
], ]
is: 'behavior-null'
}); });
assert.equal(warned, true, 'Null behaviour should generate warning'); assert.equal(warned, true, 'Null behaviour should generate warning');
Polymer.Base._warn = oldWarn; Polymer.Base._warn = oldWarn;
}); });
test('behavior array is unique', function() {
Polymer({
is: 'behavior-unique',
behaviors: [Polymer.BehaviorA, Polymer.BehaviorA]
});
assert.equal(document.createElement('behavior-unique').behaviors.length, 1);
});
test('duplicate behaviors keep last behavior', function() {
Polymer({
is: 'behavior-unique-last-behavior',
behaviors: [[Polymer.BehaviorA, Polymer.BehaviorB], Polymer.BehaviorA]
});
var behaviors = document.createElement('behavior-unique-last-behavior').behaviors;
assert.deepEqual(behaviors, [Polymer.BehaviorB, Polymer.BehaviorA]);
});
}); });