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

Commit

Permalink
Make name argument to document.register case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
dfreedm committed Nov 25, 2013
1 parent f374cb2 commit b316548
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ if (useNative) {
throw new Error('document.register: first argument (\'name\') must contain a dash (\'-\'). Argument provided was \'' + String(name) + '\'.');
}
// record name
definition.name = name;
definition.name = name.toLowerCase();
// must have a prototype, default to an extension of HTMLElement
// TODO(sjmiles): probably should throw if no prototype, check spec
if (!definition.prototype) {
Expand All @@ -110,7 +110,7 @@ if (useNative) {
throw new Error('Options missing required prototype property');
}
// elements may only be registered once
if (registry[name]) {
if (getRegisteredDefinition(name)) {
throw new Error('DuplicateDefinitionError: a type with name \'' + String(name) + '\' is already registered');
}
// ensure a lifecycle object so we don't have to null test it
Expand Down Expand Up @@ -144,7 +144,7 @@ if (useNative) {
}

function ancestry(extnds) {
var extendee = registry[extnds];
var extendee = getRegisteredDefinition(extnds);
if (extendee) {
return ancestry(extendee.extends).concat([extendee]);
}
Expand Down Expand Up @@ -307,8 +307,14 @@ if (useNative) {

var registry = {};

function getRegisteredDefinition(name) {
if (name) {
return registry[name.toLowerCase()];
}
}

function registerDefinition(name, definition) {
registry[name] = definition;
registry[name.toLowerCase()] = definition;
}

function generateConstructor(definition) {
Expand All @@ -320,7 +326,7 @@ if (useNative) {
function createElement(tag, typeExtension) {
// TODO(sjmiles): ignore 'tag' when using 'typeExtension', we could
// error check it, or perhaps there should only ever be one argument
var definition = registry[typeExtension || tag];
var definition = getRegisteredDefinition(typeExtension || tag);
if (definition) {
return new definition.ctor();
}
Expand All @@ -330,7 +336,7 @@ if (useNative) {
function upgradeElement(element) {
if (!element.__upgraded__ && (element.nodeType === Node.ELEMENT_NODE)) {
var type = element.getAttribute('is') || element.localName;
var definition = registry[type];
var definition = getRegisteredDefinition(type);
return definition && upgrade(element, definition);
}
}
Expand Down
31 changes: 24 additions & 7 deletions test/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
});

test('document.register requires name argument to be unique', function() {
var proto = {prototype: Object.create(HTMLElement.prototype)};
document.register('x-duplicate', proto);
try {
var proto = {prototype: Object.create(HTMLElement.prototype)};
document.register('x-duplicate', proto);
} catch(x) {
return;
}
assert.ok(false, 'document.register failed to throw when called multiple times with the same element name');
try {
document.register('x-duplicate', proto);
} catch(x) {
return;
}
assert.ok(false, 'document.register failed to throw when called multiple times with the same element name');
});

test('document.register create via new', function() {
Expand Down Expand Up @@ -74,6 +74,23 @@
assert.equal(xfoo.textContent, '[x-foo2]');
});

test('document.register treats names as case insensitive', function() {
var proto = {prototype: Object.create(HTMLElement.prototype)};
proto.prototype.isXCase = true;
var XCase = document.register('X-CASE', proto);
// createElement
var x = document.createElement('X-CASE');
assert.equal(x.isXCase, true);
x = document.createElement('x-case');
assert.equal(x.isXCase, true);
// upgrade
work.innerHTML = '<X-CASE></X-CASE><x-CaSe></x-CaSe>';
CustomElements.takeRecords();
assert.equal(work.firstChild.isXCase, true);
assert.equal(work.firstChild.nextSibling.isXCase, true);
});


test('document.register create multiple instances', function() {
var XFooPrototype = Object.create(HTMLElement.prototype);
XFooPrototype.bluate = function() {
Expand Down

0 comments on commit b316548

Please sign in to comment.