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

Commit 6a6e388

Browse files
author
Steve Orvell
committed
Merge pull request #201 from arv/document-register-deeper
Add support for deeper object structures in document.register
2 parents 509abfa + b64d32d commit 6a6e388

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/wrappers/Document.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,19 @@
111111
throw new Error('NotSupportedError');
112112
}
113113

114-
// This is a simplification. We require that the proto is an element
115-
// that has already been registered.
116-
114+
// Find first object on the prototype chain that already have a native
115+
// prototype. Keep track of all the objects before that so we can create
116+
// a similar structure for the native case.
117117
var proto = Object.getPrototypeOf(prototype);
118-
var nativePrototype = scope.nativePrototypeTable.get(proto);
118+
var nativePrototype;
119+
var prototypes = [];
120+
while (proto) {
121+
nativePrototype = scope.nativePrototypeTable.get(proto);
122+
if (nativePrototype)
123+
break;
124+
prototypes.push(proto);
125+
proto = Object.getPrototypeOf(proto);
126+
}
119127

120128
if (!nativePrototype) {
121129
// TODO(arv): DOMException
@@ -127,6 +135,9 @@
127135
// passed into register is used as the wrapper prototype.
128136

129137
var newPrototype = Object.create(nativePrototype);
138+
for (var i = prototypes.length - 1; i >= 0; i--) {
139+
newPrototype = Object.create(newPrototype);
140+
}
130141

131142
// Add callbacks if present.
132143
// Names are taken from:

test/js/Document.js

+42-5
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,44 @@ htmlSuite('Document', function() {
310310
assert.equal(b2.getName(), 'b');
311311
});
312312

313+
test('document.register deeper', function() {
314+
if (!document.register)
315+
return;
316+
317+
function C() {}
318+
C.prototype = {
319+
__proto__: HTMLElement.prototype
320+
};
321+
322+
function B() {}
323+
B.prototype = {
324+
__proto__: C.prototype
325+
};
326+
327+
function A() {}
328+
A.prototype = {
329+
__proto__: B.prototype
330+
};
331+
332+
A = document.register('x-a5', A);
333+
334+
var a1 = document.createElement('x-a5');
335+
assert.equal('x-a5', a1.localName);
336+
assert.equal(a1.__proto__, A.prototype);
337+
assert.equal(a1.__proto__.__proto__, B.prototype);
338+
assert.equal(a1.__proto__.__proto__.__proto__, C.prototype);
339+
assert.equal(a1.__proto__.__proto__.__proto__.__proto__,
340+
HTMLElement.prototype);
341+
342+
var a2 = new A();
343+
assert.equal('x-a5', a2.localName);
344+
assert.equal(a2.__proto__, A.prototype);
345+
assert.equal(a2.__proto__.__proto__, B.prototype);
346+
assert.equal(a2.__proto__.__proto__.__proto__, C.prototype);
347+
assert.equal(a2.__proto__.__proto__.__proto__.__proto__,
348+
HTMLElement.prototype);
349+
});
350+
313351
test('document.register createdCallback', function() {
314352
if (!document.register)
315353
return;
@@ -328,7 +366,7 @@ htmlSuite('Document', function() {
328366
}
329367
}
330368

331-
A = document.register('x-aa', A);
369+
A = document.register('x-a2', A);
332370

333371
var a = new A;
334372
assert.equal(createdCalls, 1);
@@ -358,7 +396,7 @@ htmlSuite('Document', function() {
358396
}
359397
}
360398

361-
A = document.register('x-aaa', A);
399+
A = document.register('x-a3', A);
362400

363401
var a = new A;
364402
document.body.appendChild(a);
@@ -367,8 +405,7 @@ htmlSuite('Document', function() {
367405
assert.equal(leftDocumentCalls, 1);
368406
});
369407

370-
test('document.register attributeChangedCallback',
371-
function() {
408+
test('document.register attributeChangedCallback', function() {
372409
if (!document.register)
373410
return;
374411

@@ -398,7 +435,7 @@ htmlSuite('Document', function() {
398435
}
399436
}
400437

401-
A = document.register('x-aaaa', A);
438+
A = document.register('x-a4', A);
402439

403440
var a = new A;
404441
assert.equal(attributeChangedCalls, 0);

0 commit comments

Comments
 (0)