Skip to content

Commit

Permalink
Remove __proto__. Fix `TypeError: Array.prototype.toString is not gen…
Browse files Browse the repository at this point in the history
…eric` in Node.js.

Should work in IE9 beta as well.
  • Loading branch information
NV committed Nov 19, 2010
1 parent 120908d commit 0eb3d4e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CSSOM.js is a pure JavaScript parser and a partial implementation of [CSS Object

## [Tests](http://nv.github.com/CSSOM/test/)

CSSOM.js uses ECMAScript 5 features (such as `Object.defineProperty`) and `__proto__`. It currently works in Google Chrome 6+, Safari 5, and Firefox 4 beta. Opera (including 11 alpha) does not support `Object.defineProperty`. IE (including 9 beta) does not support `__proto__`.
CSSOM.js uses ECMAScript 5 features such as `Object.defineProperty`. It currently works in Google Chrome 6+, Safari 5, Firefox 4 beta and IE9 beta. Opera (including 11 alpha) does not support `Object.defineProperty`.


## Build
Expand Down
3 changes: 0 additions & 3 deletions docs/parse.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@
if (!Object.defineProperty) {
errors.push("Object.defineProperty isn’t supported");
}
if (!({}).__proto__) {
errors.push("__proto__ isn’t supported");
}
if (errors.length) {
document.getElementById("message").innerHTML = errors.join("<br>");
document.body.className = "error";
Expand Down
18 changes: 9 additions & 9 deletions lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var CSSOM = {};
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
*/
CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){
this.length = 0;

// NON-STANDARD
Object.defineProperty(this, "importants", {
value: {},
Expand All @@ -18,10 +20,6 @@ CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){

CSSOM.CSSStyleDeclaration.prototype = {

// Inherit from Array to use splice and indexOf methods.
// __proto__ isn't a part of ECMAScript 5 spec. It doesn't work in IE, even in IE9.
__proto__: Array.prototype,

constructor: CSSOM.CSSStyleDeclaration,

/**
Expand All @@ -45,13 +43,15 @@ CSSOM.CSSStyleDeclaration.prototype = {
setProperty: function(name, value, priority) {
if (this[name]) {
// Property already exist. Overwrite it.
var index = this.indexOf(name);
var index = Array.prototype.indexOf.call(this, name);
if (index < 0) {
this.push(name);
this[this.length] = name;
this.length++;
}
} else {
// New property.
this.push(name);
this[this.length] = name;
this.length++;
}
this[name] = value;
this.importants[name] = !!priority;
Expand All @@ -68,15 +68,15 @@ CSSOM.CSSStyleDeclaration.prototype = {
if (!(name in this)) {
return ""
}
var index = this.indexOf(name);
var index = Array.prototype.indexOf.call(this, name);
if (index < 0) {
return ""
}
var prevValue = this[name];
this[name] = "";

// That's what WebKit and Opera do
this.splice(index, 1);
Array.prototype.splice.call(this, index, 1);

// That's what Firefox does
//this[index] = ""
Expand Down
16 changes: 9 additions & 7 deletions lib/MediaList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ var CSSOM = {};
* @constructor
* @see http://dev.w3.org/csswg/cssom/#the-medialist-interface
*/
CSSOM.MediaList = function MediaList(){};
CSSOM.MediaList = function MediaList(){
this.length = 0;
};

CSSOM.MediaList.prototype = {
__proto__: Array.prototype,

constructor: CSSOM.MediaList,

/**
* @return {string}
*/
get mediaText() {
return this.join(", ");
return Array.prototype.join.call(this, ", ");
},

/**
Expand All @@ -36,18 +37,19 @@ CSSOM.MediaList.prototype = {
* @param {string} medium
*/
appendMedium: function(medium) {
if (this.indexOf(medium) == -1) {
this.push(medium);
if (Array.prototype.indexOf.call(this, medium) == -1) {
this[this.length] = medium;
this.length++;
}
},

/**
* @param {string} medium
*/
deleteMedium: function(medium) {
var index = this.indexOf(medium);
var index = Array.prototype.indexOf.call(this, medium);
if (index != -1) {
this.splice(index, 1);
Array.prototype.splice.call(this, index, 1);
}
}

Expand Down
20 changes: 15 additions & 5 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ var TESTS = [
cssRules: [
{
selectorText: "*",
style: {}
style: {
length: 0
}
}
]
}
Expand All @@ -109,7 +111,9 @@ var TESTS = [
cssRules: [
{
selectorText: "*",
style: {}
style: {
length: 0
}
}
]
}
Expand All @@ -120,7 +124,9 @@ var TESTS = [
cssRules: [
{
selectorText: "* *",
style: {}
style: {
length: 0
}
}
]
}
Expand All @@ -131,7 +137,9 @@ var TESTS = [
cssRules: [
{
selectorText: "* *",
style: {}
style: {
length: 0
}
}
]
}
Expand Down Expand Up @@ -277,7 +285,9 @@ var TESTS = [
cssRules: [
{
selectorText: "a",
style: {}
style: {
length: 0
}
},
{
media: {
Expand Down

0 comments on commit 0eb3d4e

Please sign in to comment.