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