source: frontend/node_modules/cssstyle/lib/CSSStyleDeclaration.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

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