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

#551 - V1 custom elements - missing functionality #552

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/CustomElements/v1/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ var CustomElementDefinition;
this._addNodes(doc.childNodes);
}

// http://w3c.github.io/webcomponents/spec/custom/#dom-customelementsregistry-get
get(localName) {
const def = this._definitions.get(localName);
return def ? def.constructor : undefined;
}

flush() {
this._handleMutations(this._observer.takeRecords());
}
Expand Down Expand Up @@ -237,11 +243,21 @@ var CustomElementDefinition;
new (definition.constructor)();
console.assert(this._newInstance == null);
}
if (definition.attributeChangedCallback && definition.observedAttributes.length > 0) {

var observedAttributes = definition.observedAttributes;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to lookup the property only once since there's multiple lookups below.

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

// Trigger attributeChangedCallback for existing attributes.
// http://w3c.github.io/webcomponents/spec/custom/#upgrades - part 8
observedAttributes.forEach(function (name) {
if (element.hasAttribute(name)) {
element.attributeChangedCallback(name, null, element.getAttribute(name));
}
});
}
}
Expand Down Expand Up @@ -296,7 +312,7 @@ var CustomElementDefinition;
var rawCreateElement = doc.createElement.bind(document);
doc._createElement = function(tagName, callConstructor) {
var customElements = win['customElements'];
var element = rawCreateElement.call(document, tagName);
var element = rawCreateElement(tagName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already bound to document.

var definition = customElements._definitions.get(tagName.toLowerCase());
if (definition) {
customElements._upgradeElement(element, definition, callConstructor);
Expand Down
28 changes: 28 additions & 0 deletions tests/CustomElements/v1/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ suite('customElements', function() {
done();
});

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

test('attributeChangedCallback for existing observed attributes', function (done) {
var changed = [];
class XBoo extends HTMLElement {
static observedAttributes () {
return ['test1'];
}
attributeChangedCallback(name, oldValue, newValue) {
changed.push({ name, oldValue, newValue });
}
}
var xboo = new XBoo();
xboo.setAttribute('test1', 'test1');
xboo.setAttribute('test2', 'test2');
customElements.flush();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed to be how other tests were initiating an upgrade pass. I'll get the tests running and then correct this if need be.

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

test('document.registerElement disconnectedCallbacks in prototype', function(done) {
var ready, inserted, removed;
class XBoo extends HTMLElement {
Expand Down