Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Avoid conflicts with using Functions as Custom Element definitions
Browse files Browse the repository at this point in the history
Function.name is readonly, use defition.elementName to store tagname
Add test
Fixes #362
  • Loading branch information
dfreedm committed Dec 16, 2013
1 parent a2039d9 commit 8ee223e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ if (useNative) {
throw new Error('Options missing required prototype property');
}
// record name
definition.name = name.toLowerCase();
definition.elementName = name.toLowerCase();
// ensure a lifecycle object so we don't have to null test it
definition.lifecycle = definition.lifecycle || {};
// build a list of ancestral custom elements (for native base detection)
Expand All @@ -128,7 +128,7 @@ if (useNative) {
// overrides to implement attributeChanged callback
overrideAttributeApi(definition.prototype);
// 7.1.5: Register the DEFINITION with DOCUMENT
registerDefinition(definition.name, definition);
registerDefinition(definition.elementName, definition);
// 7.1.7. Run custom element constructor generation algorithm with PROTOTYPE
// 7.1.8. Return the output of the previous step.
definition.ctor = generateConstructor(definition);
Expand Down Expand Up @@ -161,10 +161,10 @@ if (useNative) {
baseTag = a.is && a.tag;
}
// our tag is our baseTag, if it exists, and otherwise just our name
definition.tag = baseTag || definition.name;
definition.tag = baseTag || definition.elementName;
if (baseTag) {
// if there is a base tag, use secondary 'is' specifier
definition.is = definition.name;
definition.is = definition.elementName;
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@
xboo.setAttribute('foo', 'zot');
});

test('document.register can use Functions as definitions', function() {
// function used as Custom Element defintion
function A$A() {
this.alive = true;
}
A$A.prototype = Object.create(HTMLElement.prototype);
// bind createdCallback to function body
A$A.prototype.createdCallback = A$A;
A$A = document.register('a-a', A$A);
// test via new
var a = new A$A();
assert.equal(a.alive, true);
// test via parser upgrade
work.innerHTML = '<a-a></a-a>';
CustomElements.takeRecords();
assert.equal(work.firstElementChild.alive, true);
});

test('node.cloneNode upgrades', function(done) {
var XBooPrototype = Object.create(HTMLElement.prototype);
XBooPrototype.createdCallback = function() {
Expand Down

0 comments on commit 8ee223e

Please sign in to comment.