Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cssparser.php
1<?php
2namespace Bitrix\Main\Web\DOM;
3
4
6{
7 public static function parseDocument(Document $document, $sort = false)
8 {
9 $css = static::findDocumentCss($document);
10
11 return static::parse($css, $sort);
12 }
13
14 public static function parse($css, $sort = false)
15 {
16 $result = static::parseCss($css);
17 if($sort)
18 {
19 return static::sortSelectors($result);
20 }
21 else
22 {
23 return $result;
24 }
25 }
26
27 public static function parseCss($css)
28 {
29 $result = array();
30
31 // remove comments
32 $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','', $css);
33 // remove keyframes rules
34 $css = preg_replace('/@[-|keyframes].*?\{.*?\}[ \r\n]*?/s', '', $css);
35 $css = trim($css);
36 foreach(explode("}", $css) as $declarationBlock)
37 {
38 $declarationBlock = trim($declarationBlock);
39 if(!$declarationBlock)
40 {
41 continue;
42 }
43
44 $declarationBlockExploded = explode("{", $declarationBlock);
45 $selectorList = $declarationBlockExploded[0];
46 $declaration = $declarationBlockExploded[1];
47 $declaration = trim(trim($declaration), ";");
48
49 foreach(explode(',', $selectorList) as $selector)
50 {
51 $selector = trim($selector);
52 $result[] = array(
53 'SELECTOR' => $selector,
54 'STYLE' => static::getDeclarationArray($declaration),
55 );
56 }
57 }
58
59 return $result;
60 }
61
66 public static function findDocumentCss(Document $document)
67 {
68 if(!$document->getHead())
69 {
70 return '';
71 }
72
73 if(!$document->getHead()->hasChildNodes())
74 {
75 return '';
76 }
77
78 $cssList = array();
79 foreach($document->getHead()->getChildNodes() as $child)
80 {
82 if($child->getNodeName() === "STYLE" && $child->getAttribute('media') !== 'print')
83 {
84 $cssList[] = $child->getTextContent();
85 //$child->getParentNode()->removeChild($child);
86 }
87 }
88
89 return implode("\n", $cssList);
90 }
91
92 public static function getDeclarationArray($declarationBlock, $singleStyle = true)
93 {
94 $styleList = array();
95 $declarationBlock = trim($declarationBlock);
96 if($declarationBlock)
97 {
98 // fix image urls in data:URL format with base64 encoding
99 $declarationBlock = str_replace(';base64', '__base64', $declarationBlock);
100 foreach(explode(";", $declarationBlock) as $declaration)
101 {
102 $declaration = str_replace('__base64', ';base64', $declaration);
103 $declaration = trim($declaration);
104 if(!$declaration)
105 {
106 continue;
107 }
108
109 // check declaration
110 if (!preg_match('#^([-a-z0-9\*]+):(.*)$#i', $declaration, $matches))
111 {
112 continue;
113 }
114
115 if(!isset($matches[0], $matches[1], $matches[2]))
116 {
117 continue;
118 }
119
120 $matches[1] = trim($matches[1]);
121
122 if ($singleStyle)
123 {
124 $styleList[$matches[1]] = trim($matches[2]);
125 }
126 else
127 {
128 if (!isset($styleList[$matches[1]]))
129 {
130 $styleList[$matches[1]] = [];
131 }
132 $styleList[$matches[1]][] = trim($matches[2]);
133 }
134 }
135 }
136
137 return $styleList;
138 }
139
140 public static function getDeclarationString($declarationList)
141 {
142 $result = '';
143 foreach($declarationList as $property => $value)
144 {
145 if (is_array($value))
146 {
147 foreach ($value as $valueChunk)
148 {
149 $result .= trim($property) . ': ' . trim($valueChunk) . ';';
150 }
151 }
152 else
153 {
154 $result .= trim($property) . ': ' . trim($value) . ';';
155 }
156 }
157
158 return $result;
159 }
160
161
162 public static function sortSelectors($styleList)
163 {
164 foreach($styleList as $k => $v)
165 {
166 $styleList[$k]['SORT'] = static::getSelectorSort($v['SELECTOR']);
167 $styleList[$k]['SORT'][] = $k;
168 }
169
170 usort($styleList, function ($first, $second)
171 {
172 $a = $first['SORT'];
173 $b = $second['SORT'];
174
175 for($i = 0; $i < 4; $i++)
176 {
177 if($a[$i] !== $b[$i])
178 {
179 return $a[$i] < $b[$i] ? -1 : 1;
180 }
181 }
182
183 return -1; // last class have more priority
184 });
185
186 foreach($styleList as $k => $v)
187 {
188 unset($styleList[$k]['SORT']);
189 }
190
191 return array_reverse($styleList);
192 }
193
194 public static function getSelectorSort($selector)
195 {
196 return array(
197 preg_match_all('/#\w/i', $selector, $result),
198 preg_match_all('/\.\w/i', $selector, $result),
199 preg_match_all('/^\w|\ \w|\‍(\w|\:[^not]/i', $selector, $result)
200 );
201 }
202}
static getSelectorSort($selector)
static sortSelectors($styleList)
static getDeclarationString($declarationList)
static parse($css, $sort=false)
Definition cssparser.php:14
static getDeclarationArray($declarationBlock, $singleStyle=true)
Definition cssparser.php:92
static parseDocument(Document $document, $sort=false)
Definition cssparser.php:7