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

Commit 8fb30dc

Browse files
author
Steve Orvell
committed
Merge pull request #316 from arv/fix-constructor-property
Set the constructor property on the prototype as required by WebIDL
2 parents b49a44a + 590d8b5 commit 8fb30dc

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/wrappers.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,19 @@ window.ShadowDOMPolyfill = {};
3030
throw new Error('Assertion failed');
3131
};
3232

33+
var defineProperty = Object.defineProperty;
34+
var getOwnPropertyNames = Object.getOwnPropertyNames;
35+
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
36+
3337
function mixin(to, from) {
34-
Object.getOwnPropertyNames(from).forEach(function(name) {
35-
Object.defineProperty(to, name,
36-
Object.getOwnPropertyDescriptor(from, name));
38+
getOwnPropertyNames(from).forEach(function(name) {
39+
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
3740
});
3841
return to;
3942
};
4043

4144
function mixinStatics(to, from) {
42-
Object.getOwnPropertyNames(from).forEach(function(name) {
45+
getOwnPropertyNames(from).forEach(function(name) {
4346
switch (name) {
4447
case 'arguments':
4548
case 'caller':
@@ -49,8 +52,7 @@ window.ShadowDOMPolyfill = {};
4952
case 'toString':
5053
return;
5154
}
52-
Object.defineProperty(to, name,
53-
Object.getOwnPropertyDescriptor(from, name));
55+
defineProperty(to, name, getOwnPropertyDescriptor(from, name));
5456
});
5557
return to;
5658
};
@@ -65,7 +67,7 @@ window.ShadowDOMPolyfill = {};
6567
// Mozilla's old DOM bindings are bretty busted:
6668
// https://bugzilla.mozilla.org/show_bug.cgi?id=855844
6769
// Make sure they are create before we start modifying things.
68-
Object.getOwnPropertyNames(window);
70+
getOwnPropertyNames(window);
6971

7072
function getWrapperConstructor(node) {
7173
var nativePrototype = node.__proto__ || Object.getPrototypeOf(node);
@@ -128,7 +130,7 @@ window.ShadowDOMPolyfill = {};
128130
}
129131

130132
function installProperty(source, target, allowMethod) {
131-
Object.getOwnPropertyNames(source).forEach(function(name) {
133+
getOwnPropertyNames(source).forEach(function(name) {
132134
if (name in target)
133135
return;
134136

@@ -138,7 +140,7 @@ window.ShadowDOMPolyfill = {};
138140
}
139141
var descriptor;
140142
try {
141-
descriptor = Object.getOwnPropertyDescriptor(source, name);
143+
descriptor = getOwnPropertyDescriptor(source, name);
142144
} catch (ex) {
143145
// JSC and V8 both use data properties instead of accessors which can
144146
// cause getting the property desciptor to throw an exception.
@@ -164,7 +166,7 @@ window.ShadowDOMPolyfill = {};
164166
setter = getSetter(name);
165167
}
166168

167-
Object.defineProperty(target, name, {
169+
defineProperty(target, name, {
168170
get: getter,
169171
set: setter,
170172
configurable: descriptor.configurable,
@@ -195,6 +197,12 @@ window.ShadowDOMPolyfill = {};
195197
addForwardingProperties(nativePrototype, wrapperPrototype);
196198
if (opt_instance)
197199
registerInstanceProperties(wrapperPrototype, opt_instance);
200+
defineProperty(wrapperPrototype, 'constructor', {
201+
value: wrapperConstructor,
202+
configurable: true,
203+
enumerable: false,
204+
writable: true
205+
});
198206
}
199207

200208
function isWrapperFor(wrapperConstructor, nativeConstructor) {
@@ -205,11 +213,7 @@ window.ShadowDOMPolyfill = {};
205213
/**
206214
* Creates a generic wrapper constructor based on |object| and its
207215
* constructor.
208-
* Sometimes the constructor does not have an associated instance
209-
* (CharacterData for example). In that case you can pass the constructor that
210-
* you want to map the object to using |opt_nativeConstructor|.
211216
* @param {Node} object
212-
* @param {Function=} opt_nativeConstructor
213217
* @return {Function} The generated constructor.
214218
*/
215219
function registerObject(object) {
@@ -322,7 +326,7 @@ window.ShadowDOMPolyfill = {};
322326
}
323327

324328
function defineGetter(constructor, name, getter) {
325-
Object.defineProperty(constructor.prototype, name, {
329+
defineProperty(constructor.prototype, name, {
326330
get: getter,
327331
configurable: true,
328332
enumerable: true

test/js/wrappers.js

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ suite('Wrapper creation', function() {
1717
assert.isTrue(Object.getPrototypeOf(br).hasOwnProperty('clear'));
1818
});
1919

20+
test('HTMLUnknownElement constructor', function() {
21+
var element = document.createElement('unknownelement');
22+
assert.instanceOf(element, HTMLUnknownElement);
23+
assert.equal(Object.getPrototypeOf(element), HTMLUnknownElement.prototype);
24+
assert.equal(Object.getPrototypeOf(element).constructor, HTMLUnknownElement);
25+
});
26+
2027
Object.keys(knownElements).forEach(function(tagName) {
2128
test(tagName, function() {
2229
var constructor = window[knownElements[tagName]];
@@ -26,6 +33,7 @@ suite('Wrapper creation', function() {
2633
var element = document.createElement(tagName);
2734
assert.instanceOf(element, constructor);
2835
assert.equal(Object.getPrototypeOf(element), constructor.prototype);
36+
assert.equal(Object.getPrototypeOf(element).constructor, constructor);
2937
});
3038
});
3139

0 commit comments

Comments
 (0)