Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit ae7873fffd92848a17b668ffceb05eed367a3e6c 1 parent 888bbc2
Elijah Grey authored
View
45 COPYING.md
@@ -0,0 +1,45 @@
+This program is dual-licensed under the GNU GPL v3 and the X11/MIT license.
+
+X11/MIT license
+---------------
+
+Copyright (c) 2009 Elijah Grey
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+GNU GPL v3 license
+------------------
+
+Copyright (c) 2009 Elijah Grey
+
+This software is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
View
18 README.md
@@ -0,0 +1,18 @@
+Xccessors (cross-browser accessors) is an open source JavaScript shim that implements the legacy or standard methods for defining and looking up accessors (getters and setters) of objects.
+
+**Please note**: Internet Explorer 8 only supports getters and setters on the DOM and it’s prototypes (document, Element.prototype, etc.) and the window object. As long as you are setting an accessor on the window object, a DOM element, or a DOM prototype, Xccessors should work fine on every browser.
+
+Xccessors Standard
+------------------
+
+Xcessors Standard implements ECMAScript 5’s (formerly 3.1) accessors standard (Object.defineProperty and Object.getOwnPropertyDescriptor) in browsers that support the legacy non-standard accessor methods. Everything behaves according to the ECMAScript 5 standard except the configurable, enumerable, and writable options. It is impossible to implement these options without native support for Object.defineProperty. The good news is that you are never going to use them if you are changing a JavaScript program from the legacy to standard methods.
+
+Xccessors Legacy
+----------------
+
+Xccessors Legacy implements the legacy accessor methods (object.__define[GS]etter__ and object.__lookup[GS]etter__) in browsers that support the standard ECMAScript 5 accessor methods.
+
+License
+-------
+
+Xccessors is dual licensed under the GNU GPL v3 and the X11/MIT license.
View
52 xccessors-legacy.js
@@ -0,0 +1,52 @@
+/*
+* Xccessors Legacy v0.0.4: Cross-browser legacy non-standard accessors
+* http://code.eligrey.com/xccessors/legacy/
+*
+* By Elijah Grey, http://eligrey.com
+*
+* A shim that implements __defineGetter__, __defineSetter__, __lookupGetter__, and __lookupSetter__
+* in browsers that have ECMAScript 5 accessor support but not the legacy methods.
+*
+* Public Domain.
+* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
+*/
+(function () {
+ var defineProp = Object.defineProperty,
+ getProp = Object.getOwnPropertyDescriptor,
+
+ // methods being implemented
+ methods = ["__defineGetter__", "__defineSetter__", "__lookupGetter__", "__lookupSetter__"],
+
+ // objects to implement legacy methods onto their prototypes
+ extend = [Object, String, Array, Function, Boolean, Number,
+ RegExp, Date, Error, Element, Window, HTMLDocument], // Object.prototype[method] doesn't work on everything for IE
+
+ proto = "prototype";
+
+ function extendMethod(method, fun) {
+ if (!(method in {}))
+ for (var i=0; i<extend.length; i++)
+ extend[i][proto][method] = fun;
+ }
+
+ if (defineProp) {
+ extendMethod(methods[0], function (prop, fun) { // __defineGetter__
+ defineProp(this, prop, { get: fun });
+ });
+
+ extendMethod(methods[1], function (prop, fun) { // __defineSetter__
+ defineProp(this, prop, { set: fun });
+ });
+ }
+
+ if (getProp) {
+ extendMethod(methods[2], function (prop) { // __lookupGetter__
+ return getProp(this, prop).get ||
+ getProp(this.constructor[proto], prop).get; // look in prototype too
+ });
+ extendMethod(methods[3], function (prop) { // __lookupSetter__
+ return getProp(this, prop).set ||
+ getProp(this.constructor[proto], prop).set; // look in prototype too
+ });
+ }
+})();
View
93 xccessors-standard.js
@@ -0,0 +1,93 @@
+/*
+* Xccessors Standard v0.0.4: Cross-browser ECMAScript 5 accessors
+* http://code.eligrey.com/xccessors/standard/
+*
+* 2009-06-16
+*
+* By Elijah Grey, http://eligrey.com
+*
+* A shim that partially implements Object.defineProperty, Object.getOwnPropertyDescriptor, and Object.defineProperties
+* in browsers that have legacy __(define|lookup)[GS]etter__ support.
+*
+* License: GNU GPL v3 and the X11/MIT license
+* See http://eligrey.com/blog/about/license
+*/
+
+
+(function() {
+ var ObjectProto = Object.prototype,
+ hasOwnProp = function (obj, prop) {
+ return ObjectProto.hasOwnProperty.call(obj, prop);
+ };
+
+ if (ObjectProto.__defineGetter__ && ObjectProto.__defineSetter__ && ObjectProto.__lookupGetter__ && ObjectProto.__lookupSetter__) {
+
+ if (!Object.defineProperty) Object.defineProperty = function (obj, prop, descriptor) {
+ if (arguments.length < 3) // all arguments required
+ throw new TypeError("Argument not optional");
+
+ prop += ""; // convert prop to string
+
+ if (typeof descriptor == "object") { // check for descriptor object
+ if (hasOwnProp(descriptor, "value")) {
+ if (!ObjectProto.__lookupGetter__.call(obj, prop) && !ObjectProto.__lookupSetter__.call(obj, prop))
+ obj[prop] = descriptor.value; // data property defined and no pre-existing accessors
+
+ if((hasOwnProp(descriptor, "get") || hasOwnProp(descriptor, "set"))) // descriptor has a value prop but accessor already exists
+ throw new TypeError("Object doesn't support this action");
+ }
+
+ // can't implement these features so throw a RangeError if any are true
+ if (descriptor.writable === true || descriptor.enumerable === true || descriptor.configurable === true)
+ throw new RangeError("This implementation of Object.defineProperty does not support configurable, enumerable, or writable.");
+
+ else if (typeof descriptor.get == "function")
+ ObjectProto.__defineGetter__.call(obj, prop, descriptor.get);
+
+ if (typeof descriptor.set == "function")
+ ObjectProto.__defineSetter__.call(obj, prop, descriptor.set);
+ }
+ return obj;
+ };
+
+
+ if (!Object.getOwnPropertyDescriptor) Object.getOwnPropertyDescriptor = function (obj, prop) {
+ if (arguments.length < 2) // all arguments required
+ throw new TypeError("Argument not optional")
+
+ prop += ""; // convert prop to string
+
+ var descriptor = {configurable: true, enumerable: true, writable: true, value: undefined},
+ getter = ObjectProto.__lookupGetter__.call(obj, prop),
+ setter = ObjectProto.__lookupSetter__.call(obj, prop);
+
+ if (!hasOwnProp(obj, prop)) // property doesn't exist or is inherited
+ return descriptor;
+
+ if (!getter && !setter) { // not an accessor so return prop
+ descriptor.value = obj[prop];
+ return descriptor;
+ }
+
+ // there is an accessor, remove descriptor.writable; populate descriptor.get and descriptor.set
+ delete descriptor.writable;
+ delete descriptor.value;
+ descriptor.get = descriptor.set = undefined;
+
+ if (getter)
+ descriptor.get = getter;
+
+ if (setter)
+ descriptor.set = setter;
+
+ return descriptor;
+ };
+
+ if (!Object.defineProperties) Object.defineProperties = function (obj, props) {
+ for (var prop in props)
+ if (hasOwnProp(props, prop))
+ Object.defineProperty(obj, prop, props[prop]);
+ }
+
+ }
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.