Skip to content

Commit

Permalink
Another attempt to fix #5 using @hax's technique.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 22, 2011
1 parent ad15283 commit 37305bc
Showing 1 changed file with 36 additions and 12 deletions.
48 changes: 36 additions & 12 deletions es5-shim.js
Expand Up @@ -537,19 +537,37 @@ if (!Object.create) {
}

// ES5 15.2.3.6
var oldDefineProperty = Object.defineProperty,
defineProperty = oldDefineProperty;
if (defineProperty) {
// detect IE 8's DOM-only implementation of defineProperty;
var subject = {};

// Patch for WebKit and IE8 standard mode
// Designed by hax <hax.github.com>
// related issue: https://github.com/kriskowal/es5-shim/issues#issue/5
// IE8 Reference:
// http://msdn.microsoft.com/en-us/library/dd282900.aspx
// http://msdn.microsoft.com/en-us/library/dd229916.aspx
// WebKit Bugs:
// https://bugs.webkit.org/show_bug.cgi?id=36423

function doesDefinePropertyWork(object) {
try {
Object.defineProperty(subject, "", {});
defineProperty = "" in subject;
} catch(e) {
defineProperty = false;
Object.defineProperty(object, "sentinel", {});
return "sentinel" in object;
} catch (exception) {
// returns falsy
}
}

// check whether defineProperty works if it's given. Otherwise,
// shim partially.
if (Object.defineProperty) {
var definePropertyWorksOnObject = doesDefinePropertyWork({});
var definePropertyWorksOnDom = typeof document == "undefined" ||
doesDefinePropertyWork(document.createElement("div"));
if (!definePropertyWorksOnObject || !definePropertyWorksOnDom) {
var definePropertyFallback = Object.defineProperty;
}
}
if (!defineProperty) {

if (!Object.defineProperty || definePropertyFallback) {
var ERR_NON_OBJECT_DESCRIPTOR = "Property description must be an object: ";
var ERR_NON_OBJECT_TARGET = "Object.defineProperty called on non-object: "
var ERR_ACCESSORS_NOT_SUPPORTED = "getters & setters can not be defined " +
Expand All @@ -560,10 +578,16 @@ if (!defineProperty) {
throw new TypeError(ERR_NON_OBJECT_TARGET + object);
if ((typeof descriptor != "object" && typeof descriptor != "function") || descriptor === null)
throw new TypeError(ERR_NON_OBJECT_DESCRIPTOR + descriptor);

// make a valiant attempt to use the real defineProperty
// for I8's DOM elements.
if (oldDefineProperty && object.nodeType)
return oldDefineProperty(object, property, descriptor);
if (definePropertyFallback) {
try {
return definePropertyFallback.call(Object, object, property, descriptor);
} catch (exception) {
// try the shim if the real one doesn't work
}
}

// If it's a data property.
if (owns(descriptor, "value")) {
Expand Down

0 comments on commit 37305bc

Please sign in to comment.