-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
vcssom.js
97 lines (85 loc) · 3.02 KB
/
vcssom.js
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
'use strict';
var removeRule = require('./vcssom/removeRule').removeRule;
exports.addon = function (renderer) {
// VCSSOM support only browser environment.
if (!renderer.client) return;
if (process.env.NODE_ENV !== 'production') {
require('./__dev__/warnOnMissingDependencies')('cssom', renderer, ['createRule']); // cssom
}
var kebab = renderer.kebab;
function VRule (selector, prelude) {
this.rule = renderer.createRule(selector, prelude);
this.decl = {};
}
VRule.prototype.diff = function (newDecl) {
var oldDecl = this.decl;
var style = this.rule.style;
var property;
for (property in oldDecl)
if (newDecl[property] === undefined)
style.removeProperty(property);
for (property in newDecl)
if (newDecl[property] !== oldDecl[property])
style.setProperty(kebab(property), newDecl[property]);
this.decl = newDecl;
};
VRule.prototype.del = function () {
removeRule(this.rule);
};
function VSheet () {
/**
* {
* '<at-rule-prelude>': {
* '<selector>': {
* color: 'red
* }
* }
* }
*/
this.tree = {};
}
VSheet.prototype.diff = function (newTree) {
var oldTree = this.tree;
// Remove media queries not present in new tree.
for (var prelude in oldTree) {
if (newTree[prelude] === undefined) {
var rules = oldTree[prelude];
for (var selector in rules)
rules[selector].del();
}
}
for (var prelude in newTree) {
if (oldTree[prelude] === undefined) {
// Whole media query is new.
for (var selector in newTree[prelude]) {
var rule = new VRule(selector, prelude);
rule.diff(newTree[prelude][selector]);
newTree[prelude][selector] = rule;
}
} else {
// Old tree already has rules with this media query.
var oldRules = oldTree[prelude];
var newRules = newTree[prelude];
// Remove rules not present in new tree.
for (var selector in oldRules)
if (!newRules[selector])
oldRules[selector].del();
// Apply new rules.
for (var selector in newRules) {
var rule = oldRules[selector];
if (rule) {
rule.diff(newRules[selector]);
newRules[selector] = rule;
} else {
rule = new VRule(selector, prelude);
rule.diff(newRules[selector]);
newRules[selector] = rule;
}
}
}
}
this.tree = newTree;
};
renderer.VRule = VRule;
renderer.VSheet = VSheet;
};