Skip to content

Commit

Permalink
Make behaviors array unique
Browse files Browse the repository at this point in the history
  • Loading branch information
TimvdLippe committed Feb 6, 2016
1 parent 9968266 commit 4cde38a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/micro/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,21 @@
},

_desugarSomeBehaviors: function(behaviors) {
var behaviorSet = [];
// iteration 1
behaviors = this._flattenBehaviorsList(behaviors);
// iteration 2
// traverse the behaviors in _reverse_ order (youngest first) because
// `_mixinBehavior` has _first property wins_ behavior, this is done
// to optimize # of calls to `_copyOwnProperty`
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) {
Expand Down Expand Up @@ -141,7 +146,7 @@

});

// 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
// for calling via doBehavior (e.g. created, ready)
Polymer.Base._behaviorProperties = {
Expand Down
26 changes: 19 additions & 7 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,33 @@
assert.match(message, /behavior is null/);
warned = true;
};

Polymer({

is: 'behavior-null',
behaviors: [
null
],

is: 'behavior-null'

]
});

assert.equal(warned, true, 'Null behaviour should generate warning');
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]);
});

});


Expand Down

0 comments on commit 4cde38a

Please sign in to comment.