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

Commit

Permalink
Prevent elements to be registered using reserved names
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeno Rocha committed Mar 24, 2014
1 parent 7be9e6e commit 53212c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/CustomElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if (useNative) {
scope.upgradeDocument = nop;
scope.upgradeDocumentTree = nop;
scope.takeRecords = nop;
scope.reservedTagList = [];

} else {

Expand Down Expand Up @@ -106,6 +107,10 @@ if (useNative) {
// offer guidance)
throw new Error('document.registerElement: first argument (\'name\') must contain a dash (\'-\'). Argument provided was \'' + String(name) + '\'.');
}
// prevent registering reserved names
if (isReservedTag(name)) {
throw new Error('Failed to execute \'registerElement\' on \'Document\': Registration failed for type \'' + String(name) + '\'. The type name is invalid.');
}
// elements may only be registered once
if (getRegisteredDefinition(name)) {
throw new Error('DuplicateDefinitionError: a type with name \'' + String(name) + '\' is already registered');
Expand Down Expand Up @@ -149,6 +154,19 @@ if (useNative) {
return definition.ctor;
}

function isReservedTag(name) {
for (var i = 0; i < reservedTagList.length; i++) {
if (name === reservedTagList[i]) {
return true;
}
}
}

var reservedTagList = [
'annotation-xml', 'color-profile', 'font-face', 'font-face-src',
'font-face-uri', 'font-face-format', 'font-face-name', 'missing-glyph'
];

function ancestry(extnds) {
var extendee = getRegisteredDefinition(extnds);
if (extendee) {
Expand Down Expand Up @@ -445,6 +463,7 @@ if (!Object.__proto__ && !useNative) {

// exports
scope.instanceof = isInstance;
scope.reservedTagList = reservedTagList;

// bc
document.register = document.registerElement;
Expand Down
9 changes: 9 additions & 0 deletions test/js/customElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
assert.ok(false, 'document.registerElement failed to throw when given no arguments');
});

test('document.registerElement requires name argument to not conflict with a reserved name', function() {
try {
document.registerElement('font-face', {prototype: Object.create(HTMLElement.prototype)});
} catch(x) {
return;
}
assert.ok(false, 'Failed to execute \'registerElement\' on \'Document\': Registration failed for type \'font-face\'. The type name is invalid.');
});

test('document.registerElement requires name argument to be unique', function() {
var proto = {prototype: Object.create(HTMLElement.prototype)};
document.registerElement('x-duplicate', proto);
Expand Down

0 comments on commit 53212c7

Please sign in to comment.