-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.php
More file actions
169 lines (141 loc) · 5.36 KB
/
css.php
File metadata and controls
169 lines (141 loc) · 5.36 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
<?php
class PHPEffectiveCss {
public function apply_css(DOMDocument $xml, $css) {
$css = $this->preprocess_css($css);
$this->apply_styles($xml, $css);
}
protected function add_style($old_properties, $new_properties) {
$properties = array();
$styles = explode(';', $old_properties);
$styles = array_filter($styles);
foreach ($styles as $key => $value) {
list($name, $value) = explode(':', $value, 2);
$name = trim($name);
$properties[$name] = $value;
}
$styles = explode(';', $new_properties);
$styles = array_filter($styles);
foreach ($styles as $key => $value) {
list($name, $value) = explode(':', $value, 2);
$name = trim($name);
$properties[$name] = $value;
}
$result = '';
foreach ($properties as $name => $value) {
$result .= $name.':'.$value.';';
}
return $result;
}
protected function implode_recursive($glue, $array) {
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result[] = $this->implode_recursive($glue, $value);
} else {
$result[] = $value;
}
}
return implode($glue, $result);
}
protected function get_effective_styles($rules, $node) {
// calculate weights
$weight_css = array();
$style = '';
foreach ($rules as $rule) {
list($selector, $properties) = explode('|', $rule);
if (strpos($selector, '#') !== false) {
$weight_css['id'][$selector][] = $properties;
} elseif (strpos($selector, '.') !== false) {
$weight_css['class'][$selector][] = $properties;
} else {
$weight_css['generic'][$selector][] = $properties;
}
}
if (isset($weight_css['generic'])){
$style = $this->implode_recursive("\n", $weight_css['generic']);
}
if (isset($weight_css['class'])){
$style = $this->add_style($style, $this->implode_recursive("\n", $weight_css['class']));
}
if (isset($weight_css['id'])){
$style = $this->add_style($style, $this->implode_recursive("\n", $weight_css['id']));
}
$classes = explode(' ', $node->getAttribute('class'));
$classes = array_filter($classes);
foreach ($classes as $class) {
$class = '.' . $class;
if (isset($weight_css['class'][$class])) {
$style = $this->add_style($style, $this->implode_recursive("\n", $weight_css['class'][$class]));
}
}
$id = $node->getAttribute('id');
if ($id) {
$id = '#' . $id;
if (isset($weight_css['id'][$id])) {
$style = $this->add_style($style, implode("\n", $weight_css['id'][$id]));
}
}
return $style;
}
protected function apply_styles($xml, $css) {
$guid = 0;
$styles = array();
$engine = new SelectorDOM($xml);
$processed_nodes = array();
preg_match_all('#(.*){([^}]+)}#isU', $css, $rules);
foreach ($rules[1] as $key => $rule) {
$properties = $rules[2][$key];
if (empty($properties)) {
continue;
}
$rule = trim($rule);
$rule = preg_replace("/\s{2,}/", '', $rule);
$rule = trim($rule, '}');
$nodes = @$engine->select($rule, $as_array = false);
if ($nodes === false) {
continue;
}
for ($i = 0; $i < $nodes->length; $i++) {
$node = $nodes->item($i);
$id = $node->getAttribute('data-guid');
if (!$id) {
$id = ++$guid;
$node->setAttribute('data-guid', $id);
}
$styles[$id][] = $rule . '|' .$properties;
$processed_nodes[] = $node;
}
}
foreach ($processed_nodes as $node) {
$guid = $node->getAttribute('data-guid');
if (!$guid) {
continue;
}
$css = $this->get_effective_styles($styles[$guid], $node);
$node->setAttribute('style', $css);
$node->removeAttribute('data-guid');
}
}
protected function preprocess_css($content) {
// normalize spaces
$content = preg_replace('#\s+#', ' ', $content);
// remove comments
$content = preg_replace('#/\*(.*)\*/#isU', '', $content);
// remove space after column, so you have the same parsing
$content = str_replace(': ', ':', $content);
// break all rules applied to multiple selectors to ones applied to single one
// eg a, b {display: block;} becomes
// a {display: block}
// b {display: block}
preg_match_all('#(.*){([^}]+)}#isU', $content, $rules);
$ruleset = array();
foreach ($rules[1] as $key => $rule) {
$selectors = explode(',', $rule);
$selectors = array_map('trim', $selectors);
foreach ($selectors as $selector) {
$ruleset[] = "$selector {" . $rules[2][$key] ."}";
}
}
return implode("\n", $ruleset);
}
}