Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #85 from jakemac53/DOMTokenList
Browse files Browse the repository at this point in the history
patch DOMTokenList instead of using a wrapper
  • Loading branch information
John Messerly committed Dec 15, 2014
2 parents dd49569 + c2465dd commit 816b985
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
56 changes: 20 additions & 36 deletions src/ShadowDOM/wrappers/DOMTokenList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,34 @@
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/


// NOTE: Set the 'ownerElement_' property on a DOMTokenList to make invalidation
// happen. This is pretty hacky but we only have to do it in one place
// (Element.js) currently so it seems like the least bad option.
(function(scope) {
'use strict';

var setWrapper = scope.setWrapper;
var unsafeUnwrap = scope.unsafeUnwrap;

function invalidateClass(el) {
scope.invalidateRendererBasedOnAttribute(el, 'class');
}

function DOMTokenList(impl, ownerElement) {
setWrapper(impl, this);
this.ownerElement_ = ownerElement;
}
var oldAdd = DOMTokenList.prototype.add;
DOMTokenList.prototype.add = function() {
oldAdd.apply(this, arguments);
this.ownerElement_ && invalidateClass(this.ownerElement_);
};

var oldRemove = DOMTokenList.prototype.remove;
DOMTokenList.prototype.remove = function() {
oldRemove.apply(this, arguments);
this.ownerElement_ && invalidateClass(this.ownerElement_);
};

DOMTokenList.prototype = {
constructor: DOMTokenList,
get length() {
return unsafeUnwrap(this).length;
},
item: function(index) {
return unsafeUnwrap(this).item(index);
},
contains: function(token) {
return unsafeUnwrap(this).contains(token);
},
add: function() {
unsafeUnwrap(this).add.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
},
remove: function() {
unsafeUnwrap(this).remove.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
},
toggle: function(token) {
var rv = unsafeUnwrap(this).toggle.apply(unsafeUnwrap(this), arguments);
invalidateClass(this.ownerElement_);
return rv;
},
toString: function() {
return unsafeUnwrap(this).toString();
}
var oldToggle = DOMTokenList.prototype.toggle;
DOMTokenList.prototype.toggle = function(token) {
var rv = oldToggle.apply(this, arguments);
this.ownerElement_ && invalidateClass(this.ownerElement_);
return rv;
};

scope.wrappers.DOMTokenList = DOMTokenList;
})(window.ShadowDOMPolyfill);
6 changes: 3 additions & 3 deletions src/ShadowDOM/wrappers/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
var ChildNodeInterface = scope.ChildNodeInterface;
var GetElementsByInterface = scope.GetElementsByInterface;
var Node = scope.wrappers.Node;
var DOMTokenList = scope.wrappers.DOMTokenList;
var ParentNodeInterface = scope.ParentNodeInterface;
var SelectorsInterface = scope.SelectorsInterface;
var addWrapNodeListMethod = scope.addWrapNodeListMethod;
Expand Down Expand Up @@ -106,8 +105,9 @@
get classList() {
var list = classListTable.get(this);
if (!list) {
classListTable.set(this,
list = new DOMTokenList(unsafeUnwrap(this).classList, this));
list = unsafeUnwrap(this).classList;
list.ownerElement_ = this;
classListTable.set(this, list);
}
return list;
},
Expand Down
10 changes: 9 additions & 1 deletion tests/ShadowDOM/js/DOMTokenList.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ suite('DOMTokenList', function() {
test('contains', function() {
var div = document.createElement('div');
var classList = div.classList;
assert.isFalse(classList.contains());
assert.isFalse(classList.contains('a'));
div.className = 'a';
assert.isTrue(classList.contains('a'));
Expand Down Expand Up @@ -110,4 +109,13 @@ suite('DOMTokenList', function() {
div.className = 'b a';
assert.equal(classList.toString(), 'b a');
});

test('index', function() {
var div = document.createElement('div');
var classList = div.classList;
classList.add('a');
classList.add('b');
assert.equal(classList[0], 'a');
assert.equal(classList[1], 'b');
});
});

0 comments on commit 816b985

Please sign in to comment.