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

Commit 8ee223e

Browse files
committed
Avoid conflicts with using Functions as Custom Element definitions
Function.name is readonly, use defition.elementName to store tagname Add test Fixes #362
1 parent a2039d9 commit 8ee223e

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/CustomElements.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ if (useNative) {
112112
throw new Error('Options missing required prototype property');
113113
}
114114
// record name
115-
definition.name = name.toLowerCase();
115+
definition.elementName = name.toLowerCase();
116116
// ensure a lifecycle object so we don't have to null test it
117117
definition.lifecycle = definition.lifecycle || {};
118118
// build a list of ancestral custom elements (for native base detection)
@@ -128,7 +128,7 @@ if (useNative) {
128128
// overrides to implement attributeChanged callback
129129
overrideAttributeApi(definition.prototype);
130130
// 7.1.5: Register the DEFINITION with DOCUMENT
131-
registerDefinition(definition.name, definition);
131+
registerDefinition(definition.elementName, definition);
132132
// 7.1.7. Run custom element constructor generation algorithm with PROTOTYPE
133133
// 7.1.8. Return the output of the previous step.
134134
definition.ctor = generateConstructor(definition);
@@ -161,10 +161,10 @@ if (useNative) {
161161
baseTag = a.is && a.tag;
162162
}
163163
// our tag is our baseTag, if it exists, and otherwise just our name
164-
definition.tag = baseTag || definition.name;
164+
definition.tag = baseTag || definition.elementName;
165165
if (baseTag) {
166166
// if there is a base tag, use secondary 'is' specifier
167-
definition.is = definition.name;
167+
definition.is = definition.elementName;
168168
}
169169
}
170170

test/js/customElements.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,24 @@
243243
xboo.setAttribute('foo', 'zot');
244244
});
245245

246+
test('document.register can use Functions as definitions', function() {
247+
// function used as Custom Element defintion
248+
function A$A() {
249+
this.alive = true;
250+
}
251+
A$A.prototype = Object.create(HTMLElement.prototype);
252+
// bind createdCallback to function body
253+
A$A.prototype.createdCallback = A$A;
254+
A$A = document.register('a-a', A$A);
255+
// test via new
256+
var a = new A$A();
257+
assert.equal(a.alive, true);
258+
// test via parser upgrade
259+
work.innerHTML = '<a-a></a-a>';
260+
CustomElements.takeRecords();
261+
assert.equal(work.firstElementChild.alive, true);
262+
});
263+
246264
test('node.cloneNode upgrades', function(done) {
247265
var XBooPrototype = Object.create(HTMLElement.prototype);
248266
XBooPrototype.createdCallback = function() {

0 commit comments

Comments
 (0)