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

Commit

Permalink
Set the constructor property on the prototype as required by WebIDL
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Nov 18, 2013
1 parent f061a57 commit 590d8b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ window.ShadowDOMPolyfill = {};
throw new Error('Assertion failed');
};

var defineProperty = Object.defineProperty;
var getOwnPropertyNames = Object.getOwnPropertyNames;
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;

function mixin(to, from) {
Object.getOwnPropertyNames(from).forEach(function(name) {
Object.defineProperty(to, name,
Object.getOwnPropertyDescriptor(from, name));
getOwnPropertyNames(from).forEach(function(name) {
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
});
return to;
};

function mixinStatics(to, from) {
Object.getOwnPropertyNames(from).forEach(function(name) {
getOwnPropertyNames(from).forEach(function(name) {
switch (name) {
case 'arguments':
case 'caller':
Expand All @@ -49,8 +52,7 @@ window.ShadowDOMPolyfill = {};
case 'toString':
return;
}
Object.defineProperty(to, name,
Object.getOwnPropertyDescriptor(from, name));
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
});
return to;
};
Expand All @@ -65,7 +67,7 @@ window.ShadowDOMPolyfill = {};
// Mozilla's old DOM bindings are bretty busted:
// https://bugzilla.mozilla.org/show_bug.cgi?id=855844
// Make sure they are create before we start modifying things.
Object.getOwnPropertyNames(window);
getOwnPropertyNames(window);

function getWrapperConstructor(node) {
var nativePrototype = node.__proto__ || Object.getPrototypeOf(node);
Expand Down Expand Up @@ -128,7 +130,7 @@ window.ShadowDOMPolyfill = {};
}

function installProperty(source, target, allowMethod) {
Object.getOwnPropertyNames(source).forEach(function(name) {
getOwnPropertyNames(source).forEach(function(name) {
if (name in target)
return;

Expand All @@ -138,7 +140,7 @@ window.ShadowDOMPolyfill = {};
}
var descriptor;
try {
descriptor = Object.getOwnPropertyDescriptor(source, name);
descriptor = getOwnPropertyDescriptor(source, name);
} catch (ex) {
// JSC and V8 both use data properties instead of accessors which can
// cause getting the property desciptor to throw an exception.
Expand All @@ -164,7 +166,7 @@ window.ShadowDOMPolyfill = {};
setter = getSetter(name);
}

Object.defineProperty(target, name, {
defineProperty(target, name, {
get: getter,
set: setter,
configurable: descriptor.configurable,
Expand Down Expand Up @@ -195,6 +197,12 @@ window.ShadowDOMPolyfill = {};
addForwardingProperties(nativePrototype, wrapperPrototype);
if (opt_instance)
registerInstanceProperties(wrapperPrototype, opt_instance);
defineProperty(wrapperPrototype, 'constructor', {
value: wrapperConstructor,
configurable: true,
enumerable: false,
writable: true
});
}

function isWrapperFor(wrapperConstructor, nativeConstructor) {
Expand All @@ -205,11 +213,7 @@ window.ShadowDOMPolyfill = {};
/**
* Creates a generic wrapper constructor based on |object| and its
* constructor.
* Sometimes the constructor does not have an associated instance
* (CharacterData for example). In that case you can pass the constructor that
* you want to map the object to using |opt_nativeConstructor|.
* @param {Node} object
* @param {Function=} opt_nativeConstructor
* @return {Function} The generated constructor.
*/
function registerObject(object) {
Expand Down Expand Up @@ -322,7 +326,7 @@ window.ShadowDOMPolyfill = {};
}

function defineGetter(constructor, name, getter) {
Object.defineProperty(constructor.prototype, name, {
defineProperty(constructor.prototype, name, {
get: getter,
configurable: true,
enumerable: true
Expand Down
8 changes: 8 additions & 0 deletions test/js/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ suite('Wrapper creation', function() {
assert.isTrue(Object.getPrototypeOf(br).hasOwnProperty('clear'));
});

test('HTMLUnknownElement constructor', function() {
var element = document.createElement('unknownelement');
assert.instanceOf(element, HTMLUnknownElement);
assert.equal(Object.getPrototypeOf(element), HTMLUnknownElement.prototype);
assert.equal(Object.getPrototypeOf(element).constructor, HTMLUnknownElement);
});

Object.keys(knownElements).forEach(function(tagName) {
test(tagName, function() {
var constructor = window[knownElements[tagName]];
Expand All @@ -26,6 +33,7 @@ suite('Wrapper creation', function() {
var element = document.createElement(tagName);
assert.instanceOf(element, constructor);
assert.equal(Object.getPrototypeOf(element), constructor.prototype);
assert.equal(Object.getPrototypeOf(element).constructor, constructor);
});
});

Expand Down

0 comments on commit 590d8b5

Please sign in to comment.