Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81141 views
1
/*********************************************************************
2
* This is a fork from the CSS Style Declaration part of
3
* https://github.com/NV/CSSOM
4
********************************************************************/
5
"use strict";
6
var CSSOM = require('cssom');
7
var fs = require('fs');
8
var path = require('path');
9
10
var camelToDashed = require('./parsers').camelToDashed;
11
var dashedToCamelCase = require('./parsers').dashedToCamelCase;
12
13
/**
14
* @constructor
15
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration
16
*/
17
var CSSStyleDeclaration = function CSSStyleDeclaration(onChangeCallback) {
18
this._values = {};
19
this._importants = {};
20
this._length = 0;
21
this._onChange = onChangeCallback || function () { return; };
22
};
23
CSSStyleDeclaration.prototype = {
24
constructor: CSSStyleDeclaration,
25
26
/**
27
*
28
* @param {string} name
29
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-getPropertyValue
30
* @return {string} the value of the property if it has been explicitly set for this declaration block.
31
* Returns the empty string if the property has not been set.
32
*/
33
getPropertyValue: function (name) {
34
if (!this._values.hasOwnProperty(name)) {
35
return "";
36
}
37
return this._values[name].toString();
38
},
39
40
/**
41
*
42
* @param {string} name
43
* @param {string} value
44
* @param {string} [priority=null] "important" or null
45
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-setProperty
46
*/
47
setProperty: function (name, value, priority) {
48
if (value === undefined) {
49
return;
50
}
51
if (value === null || value === '') {
52
this.removeProperty(name);
53
return;
54
}
55
var camel_case = dashedToCamelCase(name);
56
this[camel_case] = value;
57
this._importants[name] = priority;
58
},
59
_setProperty: function (name, value, priority) {
60
if (value === undefined) {
61
return;
62
}
63
if (value === null || value === '') {
64
this.removeProperty(name);
65
return;
66
}
67
if (this._values[name]) {
68
// Property already exist. Overwrite it.
69
var index = Array.prototype.indexOf.call(this, name);
70
if (index < 0) {
71
this[this._length] = name;
72
this._length++;
73
}
74
} else {
75
// New property.
76
this[this._length] = name;
77
this._length++;
78
}
79
this._values[name] = value;
80
this._importants[name] = priority;
81
this._onChange(this.cssText);
82
},
83
84
/**
85
*
86
* @param {string} name
87
* @see http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-removeProperty
88
* @return {string} the value of the property if it has been explicitly set for this declaration block.
89
* Returns the empty string if the property has not been set or the property name does not correspond to a known CSS property.
90
*/
91
removeProperty: function (name) {
92
if (!this._values.hasOwnProperty(name)) {
93
return "";
94
}
95
96
var prevValue = this._values[name];
97
delete this._values[name];
98
99
var index = Array.prototype.indexOf.call(this, name);
100
if (index < 0) {
101
return prevValue;
102
}
103
104
// That's what WebKit and Opera do
105
Array.prototype.splice.call(this, index, 1);
106
107
// That's what Firefox does
108
//this[index] = ""
109
110
this._onChange(this.cssText);
111
return prevValue;
112
},
113
114
115
/**
116
*
117
* @param {String} name
118
*/
119
getPropertyPriority: function (name) {
120
return this._importants[name] || "";
121
},
122
123
124
getPropertyCSSValue: function () {
125
//FIXME
126
return;
127
},
128
129
/**
130
* element.style.overflow = "auto"
131
* element.style.getPropertyShorthand("overflow-x")
132
* -> "overflow"
133
*/
134
getPropertyShorthand: function () {
135
//FIXME
136
return;
137
},
138
139
isPropertyImplicit: function () {
140
//FIXME
141
return;
142
},
143
144
/**
145
* http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSStyleDeclaration-item
146
*/
147
item: function (index) {
148
index = parseInt(index, 10);
149
if (index < 0 || index >= this._length) {
150
return '';
151
}
152
return this[index];
153
}
154
};
155
156
Object.defineProperties(CSSStyleDeclaration.prototype, {
157
cssText: {
158
get: function () {
159
var properties = [];
160
var i;
161
var name;
162
var value;
163
var priority;
164
for (i = 0; i < this._length; i++) {
165
name = this[i];
166
value = this.getPropertyValue(name);
167
priority = this.getPropertyPriority(name);
168
if (priority !== '') {
169
priority = " !" + priority;
170
}
171
properties.push([name, ': ', value, priority, ';'].join(''));
172
}
173
return properties.join(' ');
174
},
175
set: function (value) {
176
var i;
177
this._values = {};
178
Array.prototype.splice.call(this, 0, this._length);
179
this._importants = {};
180
var dummyRule;
181
try {
182
dummyRule = CSSOM.parse('#bogus{' + value + '}').cssRules[0].style;
183
} catch (err) {
184
// malformed css, just return
185
return;
186
}
187
var rule_length = dummyRule.length;
188
var name;
189
for (i = 0; i < rule_length; ++i) {
190
name = dummyRule[i];
191
this.setProperty(dummyRule[i], dummyRule.getPropertyValue(name), dummyRule.getPropertyPriority(name));
192
}
193
this._onChange(this.cssText);
194
},
195
enumerable: true,
196
configurable: true
197
},
198
parentRule: {
199
get: function () { return null; },
200
enumerable: true,
201
configurable: true
202
},
203
length: {
204
get: function () { return this._length; },
205
/**
206
* This deletes indices if the new length is less then the current
207
* length. If the new length is more, it does nothing, the new indices
208
* will be undefined until set.
209
**/
210
set: function (value) {
211
var i;
212
for (i = value; i < this._length; i++) {
213
delete this[i];
214
}
215
this._length = value;
216
},
217
enumerable: true,
218
configurable: true
219
},
220
'float': {
221
get: function () { return this.cssFloat; },
222
set: function (value) {
223
this.cssFloat = value;
224
},
225
enumerable: true,
226
configurable: true
227
}
228
});
229
230
require('./properties')(CSSStyleDeclaration.prototype);
231
232
exports.CSSStyleDeclaration = CSSStyleDeclaration;
233
234