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

Commit

Permalink
Update support for CustomElements: change document.register to docume…
Browse files Browse the repository at this point in the history
…nt.registerElement; entered/leftViewCallbacks to attached/detachedCallbacks
  • Loading branch information
sorvell committed Jan 6, 2014
1 parent 496f0a7 commit 16f97d5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 40 deletions.
15 changes: 8 additions & 7 deletions src/wrappers/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
}
});

if (document.register) {
var originalRegister = document.register;
Document.prototype.register = function(tagName, object) {
if (document.registerElement) {
var originalRegisterElement = document.registerElement;
Document.prototype.registerElement = function(tagName, object) {
var prototype = object.prototype;

// If we already used the object as a prototype for another custom
Expand Down Expand Up @@ -152,8 +152,8 @@
// and not from the spec since the spec is out of date.
[
'createdCallback',
'enteredViewCallback',
'leftViewCallback',
'attachedCallback',
'detachedCallback',
'attributeChangedCallback',
].forEach(function(name) {
var f = prototype[name];
Expand Down Expand Up @@ -190,14 +190,15 @@
scope.nativePrototypeTable.set(prototype, newPrototype);

// registration is synchronous so do it last
var nativeConstructor = originalRegister.call(unwrap(this), tagName, p);
var nativeConstructor = originalRegisterElement.call(unwrap(this),
tagName, p);
return CustomElementConstructor;
};

forwardMethodsToWrapper([
window.HTMLDocument || window.Document, // Gecko adds these to HTMLDocument
], [
'register',
'registerElement',
]);
}

Expand Down
66 changes: 33 additions & 33 deletions test/js/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,16 @@ htmlSuite('Document', function() {
assert.isTrue(document.contains(document.querySelector('html')));
});

test('document.register', function() {
if (!document.register)
test('document.registerElement', function() {
if (!document.registerElement)
return;

var aPrototype = Object.create(HTMLElement.prototype);
aPrototype.getName = function() {
return 'a';
};

var A = document.register('x-a', {prototype: aPrototype});
var A = document.registerElement('x-a', {prototype: aPrototype});

var a1 = document.createElement('x-a');
assert.equal('x-a', a1.localName);
Expand All @@ -328,7 +328,7 @@ htmlSuite('Document', function() {
return 'b';
};

var B = document.register('x-b', {prototype: bPrototype});
var B = document.registerElement('x-b', {prototype: bPrototype});

var b1 = document.createElement('x-b');
assert.equal('x-b', b1.localName);
Expand All @@ -347,16 +347,16 @@ htmlSuite('Document', function() {
assert.equal(b2.getName(), 'b');
});

test('document.register type extension', function() {
if (!document.register)
test('document.registerElement type extension', function() {
if (!document.registerElement)
return;

var aPrototype = Object.create(HTMLSpanElement.prototype);
aPrototype.getName = function() {
return 'a';
};

var A = document.register('x-a-span',
var A = document.registerElement('x-a-span',
{prototype: aPrototype, extends: 'span'});

var a1 = document.createElement('span', 'x-a-span');
Expand All @@ -376,8 +376,8 @@ htmlSuite('Document', function() {
assert.equal(a2.getName(), 'a');
});

test('document.register deeper', function() {
if (!document.register)
test('document.registerElement deeper', function() {
if (!document.registerElement)
return;

function C() {}
Expand All @@ -395,7 +395,7 @@ htmlSuite('Document', function() {
__proto__: B.prototype
};

A = document.register('x-a5', A);
A = document.registerElement('x-a5', A);

var a1 = document.createElement('x-a5');
assert.equal('x-a5', a1.localName);
Expand All @@ -414,8 +414,8 @@ htmlSuite('Document', function() {
HTMLElement.prototype);
});

test('document.register createdCallback', function() {
if (!document.register)
test('document.registerElement createdCallback', function() {
if (!document.registerElement)
return;

var self;
Expand All @@ -432,15 +432,15 @@ htmlSuite('Document', function() {
}
};

A = document.register('x-a2', A);
A = document.registerElement('x-a2', A);

var a = new A;
assert.equal(createdCalls, 1);
assert.equal(self, a);
});

test('document.register createdCallback upgrade', function() {
if (!document.register)
test('document.registerElement createdCallback upgrade', function() {
if (!document.registerElement)
return;

div = document.body.appendChild(document.createElement('div'));
Expand All @@ -456,43 +456,43 @@ htmlSuite('Document', function() {
isCustom: true
};

A = document.register('x-a2-1', A);
A = document.registerElement('x-a2-1', A);
});

test('document.register enteredViewCallback, leftViewCallback',
test('document.registerElement attachedCallback, detachedCallback',
function() {
if (!document.register)
if (!document.registerElement)
return;

var enteredViewCalls = 0;
var leftViewCalls = 0;
var attachedCalls = 0;
var detachedCalls = 0;

function A() {}
A.prototype = {
__proto__: HTMLElement.prototype,
enteredViewCallback: function() {
enteredViewCalls++;
attachedCallback: function() {
attachedCalls++;
assert.instanceOf(this, A);
assert.equal(a, this);
},
leftViewCallback: function() {
leftViewCalls++;
detachedCallback: function() {
detachedCalls++;
assert.instanceOf(this, A);
assert.equal(a, this);
}
};

A = document.register('x-a3', A);
A = document.registerElement('x-a3', A);

var a = new A;
document.body.appendChild(a);
assert.equal(enteredViewCalls, 1);
assert.equal(attachedCalls, 1);
document.body.removeChild(a);
assert.equal(leftViewCalls, 1);
assert.equal(detachedCalls, 1);
});

test('document.register attributeChangedCallback', function() {
if (!document.register)
test('document.registerElement attributeChangedCallback', function() {
if (!document.registerElement)
return;

var attributeChangedCalls = 0;
Expand Down Expand Up @@ -521,7 +521,7 @@ htmlSuite('Document', function() {
}
};

A = document.register('x-a4', A);
A = document.registerElement('x-a4', A);

var a = new A;
assert.equal(attributeChangedCalls, 0);
Expand All @@ -533,8 +533,8 @@ htmlSuite('Document', function() {
assert.equal(attributeChangedCalls, 3);
});

test('document.register get reference, upgrade, rewrap', function() {
if (!document.register)
test('document.registerElement get reference, upgrade, rewrap', function() {
if (!document.registerElement)
return;

div = document.body.appendChild(document.createElement('div'));
Expand All @@ -548,7 +548,7 @@ htmlSuite('Document', function() {
isCustom: true
};

A = document.register('x-a6', A);
A = document.registerElement('x-a6', A);
// re-wrap after registration to update wrapper
ShadowDOMPolyfill.rewrap(ShadowDOMPolyfill.unwrap(div.firstChild));
assert.isTrue(div.firstChild.isCustom);
Expand Down

0 comments on commit 16f97d5

Please sign in to comment.