Debian Codegraph @

mixmark-io-domino/lib/CSSStyleDeclaration.js

raw symbols SCIP
1 "use strict";
2
3 const { parse } = require('./style_parser');
4
5 module.exports = function (elt) {
6 const style = new CSSStyleDeclaration(elt)
7 const handler = {
8 get: function(target, property) {
9 return property in target ? target[property] : target.getPropertyValue(dasherizeProperty(property));
10 },
11 has: function(target, key) {
12 return true;
13 },
14 set: function(target, property, value) {
15 if (property in target) {
16 target[property] = value;
17 } else {
18 target.setProperty(dasherizeProperty(property), value ?? undefined);
19 }
20
21 return true;
22 }
23 };
24
25 return new Proxy(style, handler);
26 };
27
28 function dasherizeProperty(property) {
29 return property.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
30 }
31
32
33 function CSSStyleDeclaration(elt) {
34 this._element = elt;
35 }
36
37 const IMPORTANT_BANG = '!important';
38
39 // Utility function for parsing style declarations
40 // Pass in a string like "margin-left: 5px; border-style: solid"
41 // and this function returns an object like
42 // {"margin-left":"5px", "border-style":"solid"}
43 function parseStyles(value) {
44 const result = {
45 property: {},
46 priority: {},
47 }
48
49 if (!value) {
50 return result;
51 }
52
53 const styleValues = parse(value);
54 if (styleValues.length < 2) {
55 return result;
56 }
57
58 for (let i = 0; i < styleValues.length; i += 2) {
59 const name = styleValues[i];
60 let value = styleValues[i+1];
61
62 if (value.endsWith(IMPORTANT_BANG)) {
63 result.priority[name] = 'important';
64 value = value.slice(0, -IMPORTANT_BANG.length).trim();
65 }
66
67 result.property[name] = value;
68 }
69
70 return result;
71 }
72
73 var NO_CHANGE = {}; // Private marker object
74
75 CSSStyleDeclaration.prototype = Object.create(Object.prototype, {
76
77 // Return the parsed form of the element's style attribute.
78 // If the element's style attribute has never been parsed
79 // or if it has changed since the last parse, then reparse it
80 // Note that the styles don't get parsed until they're actually needed
81 _parsed: { get: function() {
82 if (!this._parsedStyles || this.cssText !== this._lastParsedText) {
83 var text = this.cssText;
84 this._parsedStyles = parseStyles(text);
85 this._lastParsedText = text;
86 delete this._names;
87 }
88 return this._parsedStyles;
89 }},
90
91 // Call this method any time the parsed representation of the
92 // style changes. It converts the style properties to a string and
93 // sets cssText and the element's style attribute
94 _serialize: { value: function() {
95 var styles = this._parsed;
96 var s = "";
97
98 for(var name in styles.property) {
99 if (s) s += " ";
100 s += name + ": " + styles.property[name];
101 if (styles.priority[name]) {
102 s += " !" + styles.priority[name];
103 }
104 s += ";";
105 }
106
107 this.cssText = s; // also sets the style attribute
108 this._lastParsedText = s; // so we don't reparse
109 delete this._names;
110 }},
111
112 cssText: {
113 get: function() {
114 // XXX: this is a CSSStyleDeclaration for an element.
115 // A different impl might be necessary for a set of styles
116 // associated returned by getComputedStyle(), e.g.
117 return this._element.getAttribute("style");
118 },
119 set: function(value) {
120 // XXX: I should parse and serialize the value to
121 // normalize it and remove errors. FF and chrome do that.
122 this._element.setAttribute("style", value);
123 }
124 },
125
126 length: { get: function() {
127 if (!this._names)
128 this._names = Object.getOwnPropertyNames(this._parsed.property);
129 return this._names.length;
130 }},
131
132 item: { value: function(n) {
133 if (!this._names)
134 this._names = Object.getOwnPropertyNames(this._parsed.property);
135 return this._names[n];
136 }},
137
138 getPropertyValue: { value: function(property) {
139 property = property.toLowerCase();
140 return this._parsed.property[property] || "";
141 }},
142
143 getPropertyPriority: { value: function(property) {
144 property = property.toLowerCase();
145 return this._parsed.priority[property] || "";
146 }},
147
148 setProperty: { value: function(property, value, priority) {
149 property = property.toLowerCase();
150 if (value === null || value === undefined) {
151 value = "";
152 }
153 if (priority === null || priority === undefined) {
154 priority = "";
155 }
156
157 // String coercion
158 if (value !== NO_CHANGE) {
159 value = "" + value;
160 }
161
162 value = value.trim();
163 if (value === "") {
164 this.removeProperty(property);
165 return;
166 }
167
168 if (priority !== "" && priority !== NO_CHANGE &&
169 !/^important$/i.test(priority)) {
170 return;
171 }
172
173 var styles = this._parsed;
174 if (value === NO_CHANGE) {
175 if (!styles.property[property]) {
176 return; // Not a valid property name.
177 }
178 if (priority !== "") {
179 styles.priority[property] = "important";
180 } else {
181 delete styles.priority[property];
182 }
183 } else {
184 // We don't just accept the property value. Instead
185 // we parse it to ensure that it is something valid.
186 // If it contains a semicolon it is invalid
187 if (value.indexOf(";") !== -1) return;
188
189 var newprops = parseStyles(property + ":" + value);
190 if (Object.getOwnPropertyNames(newprops.property).length === 0) {
191 return; // no valid property found
192 }
193 if (Object.getOwnPropertyNames(newprops.priority).length !== 0) {
194 return; // if the value included '!important' it wasn't valid.
195 }
196
197 // XXX handle shorthand properties
198
199 for (var p in newprops.property) {
200 styles.property[p] = newprops.property[p];
201 if (priority === NO_CHANGE) {
202 continue;
203 } else if (priority !== "") {
204 styles.priority[p] = "important";
205 } else if (styles.priority[p]) {
206 delete styles.priority[p];
207 }
208 }
209 }
210
211 // Serialize and update cssText and element.style!
212 this._serialize();
213 }},
214
215 setPropertyValue: { value: function(property, value) {
216 return this.setProperty(property, value, NO_CHANGE);
217 }},
218
219 setPropertyPriority: { value: function(property, priority) {
220 return this.setProperty(property, NO_CHANGE, priority);
221 }},
222
223 removeProperty: { value: function(property) {
224 property = property.toLowerCase();
225 var styles = this._parsed;
226 if (property in styles.property) {
227 delete styles.property[property];
228 delete styles.priority[property];
229
230 // Serialize and update cssText and element.style!
231 this._serialize();
232 }
233 }},
234 });