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

Commit

Permalink
#554 - upgrade step for existing attributes (#558)
Browse files Browse the repository at this point in the history
Fixes #554. Add upgrade step for calling attributeChangedCallback for all existing attributes.
  • Loading branch information
treshugart authored and justinfagnani committed Aug 22, 2016
1 parent d935de0 commit 53a9cdf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/CustomElements/v1/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,21 @@ var CustomElementDefinition;
new (definition.constructor)();
console.assert(this._newInstance == null);
}
if (definition.attributeChangedCallback && definition.observedAttributes.length > 0) {

var observedAttributes = definition.observedAttributes;
if (definition.attributeChangedCallback && observedAttributes.length > 0) {
this._attributeObserver.observe(element, {
attributes: true,
attributeOldValue: true,
attributeFilter: definition.observedAttributes,
attributeFilter: observedAttributes,
});

// Trigger attributeChangedCallback for existing attributes.
// https://html.spec.whatwg.org/multipage/scripting.html#upgrades - part 1
observedAttributes.forEach(function (name) {
if (element.hasAttribute(name)) {
element.attributeChangedCallback(name, null, element.getAttribute(name));
}
});
}
}
Expand Down
37 changes: 35 additions & 2 deletions tests/CustomElements/v1/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,42 @@ suite('customElements', function() {
done();
});

test('attributeChangedCallback for existing observed attributes', function (done) {
var changed = [];
class XBoo extends HTMLElement {
static get observedAttributes () {
return ['test1'];
}
attributeChangedCallback(name, oldValue, newValue) {
changed.push({
name: name,
oldValue: oldValue,
newValue: newValue
});
}
connectedCallback() {
this.innerHTML = 'testing';
}
}

var element = document.createElement('x-boo-at1');
element.setAttribute('test1', 'test1');
element.setAttribute('test2', 'test2');
work.appendChild(element);

customElements.define('x-boo-at1', XBoo);
customElements.flush();

assert.equal(changed.length, 1, 'should only trigger for observed attributes');
assert.equal(changed[0].name, 'test1', 'name');
assert.equal(changed[0].oldValue, null, 'oldValue');
assert.equal(changed[0].newValue, 'test1', 'newValue');
done();
});

test('customElements.get', function (done) {
class XBoo extensd HTMLElement {}
customElements.define('x-boo-get', XGetTest);
class XBoo extends HTMLElement {}
customElements.define('x-boo-get', XBoo);
assert.equal(XBoo, customElements.get('x-boo-get'));
done();
});
Expand Down

0 comments on commit 53a9cdf

Please sign in to comment.