UNPKG

3.59 kBJavaScriptView Raw
1//.CommonJS
2var CSSOM = {};
3///CommonJS
4
5
6/**
7 * @constructor
8 * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
9 */
10CSSOM.CSSStyleDeclaration = function CSSStyleDeclaration(){
11 this.length = 0;
12 this.parentRule = null;
13
14 // NON-STANDARD
15 this._importants = {};
16};
17
18
19CSSOM.CSSStyleDeclaration.prototype = {
20
21 constructor: CSSOM.CSSStyleDeclaration,
22
23 /**
24 *
25 * @param {string} name
26 * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-getPropertyValue
27 * @return {string} the value of the property if it has been explicitly set for this declaration block.
28 * Returns the empty string if the property has not been set.
29 */
30 getPropertyValue: function(name) {
31 return this[name] || "";
32 },
33
34 /**
35 *
36 * @param {string} name
37 * @param {string} value
38 * @param {string} [priority=null] "important" or null
39 * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty
40 */
41 setProperty: function(name, value, priority) {
42 if (this[name]) {
43 // Property already exist. Overwrite it.
44 var index = Array.prototype.indexOf.call(this, name);
45 if (index < 0) {
46 this[this.length] = name;
47 this.length++;
48 }
49 } else {
50 // New property.
51 this[this.length] = name;
52 this.length++;
53 }
54 this[name] = value + "";
55 this._importants[name] = priority;
56 },
57
58 /**
59 *
60 * @param {string} name
61 * @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-removeProperty
62 * @return {string} the value of the property if it has been explicitly set for this declaration block.
63 * Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.
64 */
65 removeProperty: function(name) {
66 if (!(name in this)) {
67 return "";
68 }
69 var index = Array.prototype.indexOf.call(this, name);
70 if (index < 0) {
71 return "";
72 }
73 var prevValue = this[name];
74 this[name] = "";
75
76 // That's what WebKit and Opera do
77 Array.prototype.splice.call(this, index, 1);
78
79 // That's what Firefox does
80 //this[index] = ""
81
82 return prevValue;
83 },
84
85 getPropertyCSSValue: function() {
86 //FIXME
87 },
88
89 /**
90 *
91 * @param {String} name
92 */
93 getPropertyPriority: function(name) {
94 return this._importants[name] || "";
95 },
96
97
98 /**
99 * element.style.overflow = "auto"
100 * element.style.getPropertyShorthand("overflow-x")
101 * -> "overflow"
102 */
103 getPropertyShorthand: function() {
104 //FIXME
105 },
106
107 isPropertyImplicit: function() {
108 //FIXME
109 },
110
111 // Doesn't work in IE < 9
112 get cssText(){
113 var properties = [];
114 for (var i=0, length=this.length; i < length; ++i) {
115 var name = this[i];
116 var value = this.getPropertyValue(name);
117 var priority = this.getPropertyPriority(name);
118 if (priority) {
119 priority = " !" + priority;
120 }
121 properties[i] = name + ": " + value + priority + ";";
122 }
123 return properties.join(" ");
124 },
125
126 set cssText(text){
127 var i, name;
128 for (i = this.length; i--;) {
129 name = this[i];
130 this[name] = "";
131 }
132 Array.prototype.splice.call(this, 0, this.length);
133 this._importants = {};
134
135 var dummyRule = CSSOM.parse('#bogus{' + text + '}').cssRules[0].style;
136 var length = dummyRule.length;
137 for (i = 0; i < length; ++i) {
138 name = dummyRule[i];
139 this.setProperty(dummyRule[i], dummyRule.getPropertyValue(name), dummyRule.getPropertyPriority(name));
140 }
141 }
142};
143
144
145//.CommonJS
146exports.CSSStyleDeclaration = CSSOM.CSSStyleDeclaration;
147CSSOM.parse = require('./parse').parse; // Cannot be included sooner due to the mutual dependency between parse.js and CSSStyleDeclaration.js
148///CommonJS