Skip to content

Commit

Permalink
Merge pull request #3019 from Polymer/3018-kschaaf-avoid-throw
Browse files Browse the repository at this point in the history
Avoid throwing with invalid keys/paths. Fixes #3018.
  • Loading branch information
Steve Orvell committed Nov 19, 2015
2 parents cfa6d51 + 108b7f9 commit 4d45842
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
36 changes: 19 additions & 17 deletions src/lib/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
},

removeKey: function(key) {
key = this._parseKey(key);
this._removeFromMap(this.store[key]);
delete this.store[key];
if (key = this._parseKey(key)) {
this._removeFromMap(this.store[key]);
delete this.store[key];
}
},

_removeFromMap: function(item) {
Expand Down Expand Up @@ -88,29 +89,30 @@
},

_parseKey: function(key) {
if (key[0] == '#') {
if (key && key[0] == '#') {
return key.slice(1);
}
throw new Error('unexpected key ' + key);
},

setItem: function(key, item) {
key = this._parseKey(key);
var old = this.store[key];
if (old) {
this._removeFromMap(old);
}
if (item && typeof item == 'object') {
this.omap.set(item, key);
} else {
this.pmap[item] = key;
if (key = this._parseKey(key)) {
var old = this.store[key];
if (old) {
this._removeFromMap(old);
}
if (item && typeof item == 'object') {
this.omap.set(item, key);
} else {
this.pmap[item] = key;
}
this.store[key] = item;
}
this.store[key] = item;
},

getItem: function(key) {
key = this._parseKey(key);
return this.store[key];
if (key = this._parseKey(key)) {
return this.store[key];
}
},

getItems: function() {
Expand Down
4 changes: 3 additions & 1 deletion src/standard/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@
var info = {};
this._get(path, this, info);
// Notify change to key-based path
this._notifyPath(info.path, value, fromAbove);
if (info.path) {
this._notifyPath(info.path, value, fromAbove);
}
},

// Note: this implemetation only accepts key-based array paths
Expand Down
12 changes: 12 additions & 0 deletions test/unit/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,18 @@
assert.equal(el.get('nested.again.again.wayOverThere'), 99);
});

test('notifyPath calls observer', function() {
el.a = {b: {c: true}};
sinon.spy(el, 'aChanged');
el.notifyPath('a.b.c', el.a.b.c);
assert.isTrue(el.aChanged.calledOnce);
assert.equal(el.get('a.b.c'), true);
});

test('notifyPath a non-extistant does nothing', function() {
el.notifyPath('does.not.exist', true);
});

test('get array', function() {
el.arrayChanged = function() {};
el.array = [1, 2, 3];
Expand Down

0 comments on commit 4d45842

Please sign in to comment.