css.js
18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* Copyright © 2016-2020 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable */
/* jshint unused:false */
/* global base64_decode, CSSWizardView, window, console, jQuery */
var fi = function() {
this.cssImportStatements = [];
this.cssKeyframeStatements = [];
this.cssRegex = new RegExp("([\\s\\S]*?){([\\s\\S]*?)}", "gi");
this.cssMediaQueryRegex = "((@media [\\s\\S]*?){([\\s\\S]*?}\\s*?)})";
this.cssKeyframeRegex = "((@.*?keyframes [\\s\\S]*?){([\\s\\S]*?}\\s*?)})";
this.combinedCSSRegex = "((\\s*?@media[\\s\\S]*?){([\\s\\S]*?)}\\s*?})|(([\\s\\S]*?){([\\s\\S]*?)})"; //to match css & media queries together
this.cssCommentsRegex = "(\\/\\*[\\s\\S]*?\\*\\/)";
this.cssImportStatementRegex = new RegExp("@import .*?;", "gi");
};
/*
Strip outs css comments and returns cleaned css string
@param css, the original css string to be stipped out of comments
@return cleanedCSS contains no css comments
*/
fi.prototype.stripComments = function(cssString) {
var regex = new RegExp(this.cssCommentsRegex, "gi");
return cssString.replace(regex, "");
};
/*
Parses given css string, and returns css object
keys as selectors and values are css rules
eliminates all css comments before parsing
@param source css string to be parsed
@return object css
*/
fi.prototype.parseCSS = function(source) {
if (source === undefined) {
return [];
}
var css = [];
//strip out comments
//source = this.stripComments(source);
//get import statements
while (true) {
var imports = this.cssImportStatementRegex.exec(source);
if (imports !== null) {
this.cssImportStatements.push(imports[0]);
css.push({
selector: "@imports",
type: "imports",
styles: imports[0],
});
} else {
break;
}
}
source = source.replace(this.cssImportStatementRegex, "");
//get keyframe statements
var keyframesRegex = new RegExp(this.cssKeyframeRegex, "gi");
var arr;
while (true) {
arr = keyframesRegex.exec(source);
if (arr === null) {
break;
}
css.push({
selector: "@keyframes",
type: "keyframes",
styles: arr[0],
});
}
source = source.replace(keyframesRegex, "");
//unified regex
var unified = new RegExp(this.combinedCSSRegex, "gi");
while (true) {
arr = unified.exec(source);
if (arr === null) {
break;
}
var selector = "";
if (arr[2] === undefined) {
selector = arr[5].split("\r\n").join("\n").trim();
} else {
selector = arr[2].split("\r\n").join("\n").trim();
}
/*
fetch comments and associate it with current selector
*/
var commentsRegex = new RegExp(this.cssCommentsRegex, "gi");
var comments = commentsRegex.exec(selector);
if (comments !== null) {
selector = selector.replace(commentsRegex, "").trim();
}
//determine the type
if (selector.indexOf("@media") !== -1) {
//we have a media query
var cssObject = {
selector: selector,
type: "media",
subStyles: this.parseCSS(arr[3] + "\n}"), //recursively parse media query inner css
};
if (comments !== null) {
cssObject.comments = comments[0];
}
css.push(cssObject);
} else {
//we have standart css
var rules = this.parseRules(arr[6]);
var style = {
selector: selector,
rules: rules,
};
if (selector === "@font-face") {
style.type = "font-face";
}
if (comments !== null) {
style.comments = comments[0];
}
css.push(style);
}
}
return css;
};
/*
parses given string containing css directives
and returns an array of objects containing ruleName:ruleValue pairs
@param rules, css directive string example
\n\ncolor:white;\n font-size:18px;\n
*/
fi.prototype.parseRules = function(rules) {
//convert all windows style line endings to unix style line endings
rules = rules.split("\r\n").join("\n");
var ret = [];
// Split all rules but keep semicolon for base64 url data
rules = rules.split(/;(?![^\(]*\))/);
//proccess rules line by line
for (var i = 0; i < rules.length; i++) {
var line = rules[i];
//determine if line is a valid css directive, ie color:white;
line = line.trim();
if (line.indexOf(":") !== -1) {
//line contains :
line = line.split(":");
var cssDirective = line[0].trim();
var cssValue = line.slice(1).join(":").trim();
//more checks
if (cssDirective.length < 1 || cssValue.length < 1) {
continue; //there is no css directive or value that is of length 1 or 0
// PLAIN WRONG WHAT ABOUT margin:0; ?
}
//push rule
ret.push({
directive: cssDirective,
value: cssValue,
});
} else {
//if there is no ':', but what if it was mis splitted value which starts with base64
if (line.trim().substr(0, 7) == "base64,") { //hack :)
ret[ret.length - 1].value += line.trim();
} else {
//add rule, even if it is defective
if (line.length > 0) {
ret.push({
directive: "",
value: line,
defective: true,
});
}
}
}
}
return ret; //we are done!
};
/*
just returns the rule having given directive
if not found returns false;
*/
fi.prototype.findCorrespondingRule = function(rules, directive, value) {
if (value === undefined) {
value = false;
}
var ret = false;
for (var i = 0; i < rules.length; i++) {
if (rules[i].directive == directive) {
ret = rules[i];
if (value === rules[i].value) {
break;
}
}
}
return ret;
};
/*
Finds styles that have given selector, compress them,
and returns them
*/
fi.prototype.findBySelector = function(cssObjectArray, selector, contains) {
if (contains === undefined) {
contains = false;
}
var found = [];
for (var i = 0; i < cssObjectArray.length; i++) {
if (contains === false) {
if (cssObjectArray[i].selector === selector) {
found.push(cssObjectArray[i]);
}
} else {
if (cssObjectArray[i].selector.indexOf(selector) !== -1) {
found.push(cssObjectArray[i]);
}
}
}
if (found.length < 2) {
return found;
} else {
var base = found[0];
for (i = 1; i < found.length; i++) {
this.intelligentCSSPush([base], found[i]);
}
return [base]; //we are done!! all properties merged into base!
}
};
/*
deletes cssObjects having given selector, and returns new array
*/
fi.prototype.deleteBySelector = function(cssObjectArray, selector) {
var ret = [];
for (var i = 0; i < cssObjectArray.length; i++) {
if (cssObjectArray[i].selector !== selector) {
ret.push(cssObjectArray[i]);
}
}
return ret;
};
/*
Compresses given cssObjectArray and tries to minimize
selector redundence.
*/
fi.prototype.compressCSS = function(cssObjectArray) {
var compressed = [];
var done = {};
for (var i = 0; i < cssObjectArray.length; i++) {
var obj = cssObjectArray[i];
if (done[obj.selector] === true) {
continue;
}
var found = this.findBySelector(cssObjectArray, obj.selector); //found compressed
if (found.length !== 0) {
compressed.push(found[0]);
done[obj.selector] = true;
}
}
return compressed;
};
/*
Received 2 css objects with following structure
{
rules : [{directive:"", value:""}, {directive:"", value:""}, ...]
selector : "SOMESELECTOR"
}
returns the changed(new,removed,updated) values on css1 parameter, on same structure
if two css objects are the same, then returns false
if a css directive exists in css1 and css2, and its value is different, it is included in diff
if a css directive exists in css1 and not css2, it is then included in diff
if a css directive exists in css2 but not css1, then it is deleted in css1, it would be included in diff but will be marked as type='DELETED'
@object css1 css object
@object css2 css object
@return diff css object contains changed values in css1 in regards to css2 see test input output in /test/data/css.js
*/
fi.prototype.cssDiff = function(css1, css2) {
if (css1.selector !== css2.selector) {
return false;
}
//if one of them is media query return false, because diff function can not operate on media queries
if ((css1.type === "media" || css2.type === "media")) {
return false;
}
var diff = {
selector: css1.selector,
rules: [],
};
var rule1, rule2;
for (var i = 0; i < css1.rules.length; i++) {
rule1 = css1.rules[i];
//find rule2 which has the same directive as rule1
rule2 = this.findCorrespondingRule(css2.rules, rule1.directive, rule1.value);
if (rule2 === false) {
//rule1 is a new rule in css1
diff.rules.push(rule1);
} else {
//rule2 was found only push if its value is different too
if (rule1.value !== rule2.value) {
diff.rules.push(rule1);
}
}
}
//now for rules exists in css2 but not in css1, which means deleted rules
for (var ii = 0; ii < css2.rules.length; ii++) {
rule2 = css2.rules[ii];
//find rule2 which has the same directive as rule1
rule1 = this.findCorrespondingRule(css1.rules, rule2.directive);
if (rule1 === false) {
//rule1 is a new rule
rule2.type = "DELETED"; //mark it as a deleted rule, so that other merge operations could be true
diff.rules.push(rule2);
}
}
if (diff.rules.length === 0) {
return false;
}
return diff;
};
/*
Merges 2 different css objects together
using intelligentCSSPush,
@param cssObjectArray, target css object array
@param newArray, source array that will be pushed into cssObjectArray parameter
@param reverse, [optional], if given true, first parameter will be traversed on reversed order
effectively giving priority to the styles in newArray
*/
fi.prototype.intelligentMerge = function(cssObjectArray, newArray, reverse) {
if (reverse === undefined) {
reverse = false;
}
for (var i = 0; i < newArray.length; i++) {
this.intelligentCSSPush(cssObjectArray, newArray[i], reverse);
}
for (i = 0; i < cssObjectArray.length; i++) {
var cobj = cssObjectArray[i];
if (cobj.type === "media" || (cobj.type === "keyframes")) {
continue;
}
cobj.rules = this.compactRules(cobj.rules);
}
};
/*
inserts new css objects into a bigger css object
with same selectors groupped together
@param cssObjectArray, array of bigger css object to be pushed into
@param minimalObject, single css object
@param reverse [optional] default is false, if given, cssObjectArray will be reversly traversed
resulting more priority in minimalObject's styles
*/
fi.prototype.intelligentCSSPush = function(cssObjectArray, minimalObject, reverse) {
var pushSelector = minimalObject.selector;
//find correct selector if not found just push minimalObject into cssObject
var cssObject = false;
if (reverse === undefined) {
reverse = false;
}
if (reverse === false) {
for (var i = 0; i < cssObjectArray.length; i++) {
if (cssObjectArray[i].selector === minimalObject.selector) {
cssObject = cssObjectArray[i];
break;
}
}
} else {
for (var j = cssObjectArray.length - 1; j > -1; j--) {
if (cssObjectArray[j].selector === minimalObject.selector) {
cssObject = cssObjectArray[j];
break;
}
}
}
if (cssObject === false) {
cssObjectArray.push(minimalObject); //just push, because cssSelector is new
} else {
if (minimalObject.type !== "media") {
for (var ii = 0; ii < minimalObject.rules.length; ii++) {
var rule = minimalObject.rules[ii];
//find rule inside cssObject
var oldRule = this.findCorrespondingRule(cssObject.rules, rule.directive);
if (oldRule === false) {
cssObject.rules.push(rule);
} else if (rule.type == "DELETED") {
oldRule.type = "DELETED";
} else {
//rule found just update value
oldRule.value = rule.value;
}
}
} else {
cssObject.subStyles = minimalObject.subStyles; //TODO, make this intelligent too
}
}
};
/*
filter outs rule objects whose type param equal to DELETED
@param rules, array of rules
@returns rules array, compacted by deleting all unneccessary rules
*/
fi.prototype.compactRules = function(rules) {
var newRules = [];
for (var i = 0; i < rules.length; i++) {
if (rules[i].type !== "DELETED") {
newRules.push(rules[i]);
}
}
return newRules;
};
/*
computes string for ace editor using this.css or given cssBase optional parameter
@param [optional] cssBase, if given computes cssString from cssObject array
*/
fi.prototype.getCSSForEditor = function(cssBase, depth) {
if (depth === undefined) {
depth = 0;
}
var ret = "";
if (cssBase === undefined) {
cssBase = this.css;
}
//append imports
for (var i = 0; i < cssBase.length; i++) {
if (cssBase[i].type == "imports") {
ret += cssBase[i].styles + "\n\n";
}
}
for (i = 0; i < cssBase.length; i++) {
var tmp = cssBase[i];
if (tmp.selector === undefined) { //temporarily omit media queries
continue;
}
var comments = "";
if (tmp.comments !== undefined) {
comments = tmp.comments + "\n";
}
if (tmp.type == "media") { //also put media queries to output
ret += comments + tmp.selector + "{\n";
ret += this.getCSSForEditor(tmp.subStyles, depth + 1);
ret += "}\n\n";
} else if (tmp.type !== "keyframes" && tmp.type !== "imports") {
ret += this.getSpaces(depth) + comments + tmp.selector + " {\n";
ret += this.getCSSOfRules(tmp.rules, depth + 1);
ret += this.getSpaces(depth) + "}\n\n";
}
}
//append keyFrames
for (i = 0; i < cssBase.length; i++) {
if (cssBase[i].type == "keyframes") {
ret += cssBase[i].styles + "\n\n";
}
}
return ret;
};
fi.prototype.getImports = function(cssObjectArray) {
var imps = [];
for (var i = 0; i < cssObjectArray.length; i++) {
if (cssObjectArray[i].type == "imports") {
imps.push(cssObjectArray[i].styles);
}
}
return imps;
};
/*
given rules array, returns visually formatted css string
to be used inside editor
*/
fi.prototype.getCSSOfRules = function(rules, depth) {
var ret = "";
for (var i = 0; i < rules.length; i++) {
if (rules[i] === undefined) {
continue;
}
if (rules[i].defective === undefined) {
ret += this.getSpaces(depth) + rules[i].directive + " : " + rules[i].value + ";\n";
} else {
ret += this.getSpaces(depth) + rules[i].value + ";\n";
}
}
return ret || "\n";
};
/*
A very simple helper function returns number of spaces appended in a single string,
the number depends input parameter, namely input*2
*/
fi.prototype.getSpaces = function(num) {
var ret = "";
for (var i = 0; i < num * 4; i++) {
ret += " ";
}
return ret;
};
/*
Given css string or objectArray, parses it and then for every selector,
prepends this.cssPreviewNamespace to prevent css collision issues
@returns css string in which this.cssPreviewNamespace prepended
*/
fi.prototype.applyNamespacing = function(css, forcedNamespace) {
var cssObjectArray = css;
var namespaceClass = "." + this.cssPreviewNamespace;
if (forcedNamespace !== undefined) {
namespaceClass = forcedNamespace;
}
if (typeof css === "string") {
cssObjectArray = this.parseCSS(css);
}
for (var i = 0; i < cssObjectArray.length; i++) {
var obj = cssObjectArray[i];
//bypass namespacing for @font-face @keyframes @import
if (obj.selector.indexOf("@font-face") > -1 || obj.selector.indexOf("keyframes") > -1 || obj.selector.indexOf("@import") > -1 || obj.selector.indexOf(".form-all") > -1 || obj.selector.indexOf("#stage") > -1) {
continue;
}
if (obj.type !== "media") {
var selector = obj.selector.split(",");
var newSelector = [];
for (var j = 0; j < selector.length; j++) {
if (selector[j].indexOf(".supernova") === -1) { //do not apply namespacing to selectors including supernova
newSelector.push(namespaceClass + " " + selector[j]);
} else {
newSelector.push(selector[j]);
}
}
obj.selector = newSelector.join(",");
} else {
obj.subStyles = this.applyNamespacing(obj.subStyles, forcedNamespace); //handle media queries as well
}
}
return cssObjectArray;
};
/*
given css string or object array, clears possible namespacing from
all of the selectors inside the css
*/
fi.prototype.clearNamespacing = function(css, returnObj) {
if (returnObj === undefined) {
returnObj = false;
}
var cssObjectArray = css;
var namespaceClass = "." + this.cssPreviewNamespace;
if (typeof css === "string") {
cssObjectArray = this.parseCSS(css);
}
for (var i = 0; i < cssObjectArray.length; i++) {
var obj = cssObjectArray[i];
if (obj.type !== "media") {
var selector = obj.selector.split(",");
var newSelector = [];
for (var j = 0; j < selector.length; j++) {
newSelector.push(selector[j].split(namespaceClass + " ").join(""));
}
obj.selector = newSelector.join(",");
} else {
obj.subStyles = this.clearNamespacing(obj.subStyles, true); //handle media queries as well
}
}
if (returnObj === false) {
return this.getCSSForEditor(cssObjectArray);
} else {
return cssObjectArray;
}
};
/*
creates a new style tag (also destroys the previous one)
and injects given css string into that css tag
*/
fi.prototype.createStyleElement = function(id, css, format) {
if (format === undefined) {
format = false;
}
if (this.testMode === false && format !== "nonamespace") {
//apply namespacing classes
css = this.applyNamespacing(css);
}
if (typeof css != "string") {
css = this.getCSSForEditor(css);
}
//apply formatting for css
if (format === true) {
css = this.getCSSForEditor(this.parseCSS(css));
}
if (this.testMode !== false) {
return this.testMode("create style #" + id, css); //if test mode, just pass result to callback
}
var __el = document.getElementById(id);
if (__el) {
__el.parentNode.removeChild(__el);
}
var head = document.head || document.getElementsByTagName("head")[0],
style = document.createElement("style");
style.id = id;
style.type = "text/css";
head.appendChild(style);
if (style.styleSheet && !style.sheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
};
export default fi;
/* eslint-enable */