Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
displayproperties.php
1<?php
2
3namespace Bitrix\Main\Web\DOM;
4
6{
7 const DISPLAY = 'display';
8 const FONT = 'font';
9
10 const DISPLAY_HIDDEN = 'hidden';
11 const DISPLAY_BLOCK = 'block';
12 const DISPLAY_INLINE = 'inline';
13
14 const FONT_NORMAL = 'normal';
15 const FONT_BOLD = 'bold';
16 const FONT_ITALIC = 'italic';
17 const FONT_UNDERLINED = 'underlined';
18 const FONT_DELETED = 'deleted';
19
20 protected $properties = [];
21
22 public function __construct(Node $node, array $properties = [], array $defaultProperties = [])
23 {
24 $this->properties = array_merge(
25 $this->getDefaultProperties(),
26 $defaultProperties,
27 $this->getNodeProperties($node),
29 );
30 }
31
35 public function isHidden()
36 {
37 return isset($this->properties[static::DISPLAY]) && $this->properties[static::DISPLAY] === static::DISPLAY_HIDDEN;
38 }
39
43 public function isDisplayBlock()
44 {
45 return isset($this->properties[static::DISPLAY]) && $this->properties[static::DISPLAY] === static::DISPLAY_BLOCK;
46 }
47
51 public function isDisplayInline()
52 {
53 return isset($this->properties[static::DISPLAY]) && $this->properties[static::DISPLAY] === static::DISPLAY_INLINE;
54 }
55
59 public function isFontBold()
60 {
61 return (
62 isset($this->properties[static::FONT][static::FONT_BOLD])
63 && $this->properties[static::FONT][static::FONT_BOLD] === true
64 );
65 }
66
70 public function isFontItalic()
71 {
72 return (
73 isset($this->properties[static::FONT][static::FONT_ITALIC])
74 && $this->properties[static::FONT][static::FONT_ITALIC] === true
75 );
76 }
77
81 public function isFontUnderlined()
82 {
83 return (
84 isset($this->properties[static::FONT][static::FONT_UNDERLINED])
85 && $this->properties[static::FONT][static::FONT_UNDERLINED] === true
86 );
87 }
88
92 public function isFontDeleted()
93 {
94 return (
95 isset($this->properties[static::FONT][static::FONT_DELETED])
96 && $this->properties[static::FONT][static::FONT_DELETED] === true
97 );
98 }
99
103 public function getProperties()
104 {
105 return $this->properties;
106 }
107
111 protected function getDefaultProperties()
112 {
113 return [
114 static::DISPLAY => static::DISPLAY_INLINE,
115 'font' => [],
116 ];
117 }
118
125 protected function isHiddenNode(Node $node)
126 {
127 static $hiddenNodeNames = [
128 '#comment' => true, 'STYLE' => true, 'SCRIPT' => true,
129 ];
130
131 return isset($hiddenNodeNames[$node->getNodeName()]);
132 }
133
140 protected function isBlockTag($tagName)
141 {
142 $blockTagNames = [
143 'address' => true, 'article' => true, 'aside' => true, 'blockquote' => true, 'details' => true,
144 'dialog' => true, 'dd' => true, 'div' => true, 'dl' => true, 'dt' => true, 'fieldset' => true,
145 'figcaption' => true, 'figure' => true, 'footer' => true, 'form' => true, 'h1' => true, 'h2' => true,
146 'h3' => true, 'h4' => true, 'h5' => true, 'h6' => true, 'header' => true, 'hgroup' => true, 'hr' => true,
147 'li' => true, 'main' => true, 'nav' => true, 'ol' => true, 'p' => true, 'pre' => true, 'section' => true, 'table' => true,
148 'ul' => true,
149 ];
150
151 return isset($blockTagNames[mb_strtolower($tagName)]);
152 }
153
160 protected function isBoldTag($tagName)
161 {
162 $boldTagNames = [
163 'b' => true, 'mark' => true, 'em' => true, 'strong' => true, 'h1' => true, 'h2' => true, 'h3' => true,
164 'h4' => true, 'h5' => true, 'h6' => true,
165 ];
166
167 return isset($boldTagNames[mb_strtolower($tagName)]);
168 }
169
176 protected function isItalicTag($tagName)
177 {
178 $italicTagNames = [
179 'i' => true, 'cite' => true, 'dfn' => true,
180 ];
181
182 return isset($italicTagNames[mb_strtolower($tagName)]);
183 }
184
191 protected function isUnderlinedTag($tagName)
192 {
193 return mb_strtolower($tagName) == 'u';
194 }
195
202 protected function isDeletedTag($tagName)
203 {
204 $deletedTagNames = [
205 'del' => true, 's' => true,
206 ];
207
208 return isset($deletedTagNames[mb_strtolower($tagName)]);
209 }
210
215 protected function getNodeProperties(Node $node)
216 {
217 $result = [];
218
219 if($this->isHiddenNode($node))
220 {
221 $result[static::DISPLAY] = static::DISPLAY_HIDDEN;
222 return $result;
223 }
224
225 if($node instanceof Element)
226 {
227 $styles = $node->getStyle();
228 $display = false;
229 $font = [];
230 if($styles)
231 {
232 $stylePairs = explode(';', $styles);
233 foreach($stylePairs as $pair)
234 {
235 list($name, $value) = explode(':', $pair);
236 if($name && $value)
237 {
238 $name = trim($name);
239 $value = trim($value);
240 if($name == static::DISPLAY)
241 {
242 if($value == 'none')
243 {
244 $display = static::DISPLAY_HIDDEN;
245 }
246 elseif($value == 'block')
247 {
248 $display = static::DISPLAY_BLOCK;
249 }
250 elseif($value == 'inline')
251 {
252 $display = static::DISPLAY_INLINE;
253 }
254 }
255 elseif($name == 'font-weight')
256 {
257 if(intval($value) > 500 || $value == 'bold')
258 {
259 $font[static::FONT_BOLD] = true;
260 }
261 elseif(intval($value) < 500 || $value == 'normal')
262 {
263 $font[static::FONT_BOLD] = false;
264 }
265 }
266 elseif($name == 'font-style')
267 {
268 if($value == 'italic' || mb_strpos($value, 'oblique') === 0)
269 {
270 $font[static::FONT_ITALIC] = true;
271 }
272 elseif($value == 'normal')
273 {
274 $font[static::FONT_ITALIC] = false;
275 }
276 }
277 elseif($name == 'text-decoration')
278 {
279 if(strpos($value, 'underline') !== false)
280 {
281 $font[static::FONT_UNDERLINED] = true;
282 }
283 if(strpos($value, 'line-through') !== false)
284 {
285 $font[static::FONT_DELETED] = true;
286 }
287 if($value == 'none')
288 {
289 $font[static::FONT_UNDERLINED] = false;
290 $font[static::FONT_DELETED] = false;
291 }
292 }
293 }
294 }
295 }
296 if($display == static::DISPLAY_HIDDEN)
297 {
298 $result[static::DISPLAY] = $display;
299 return $result;
300 }
301 if(!$display && $this->isBlockTag($node->getTagName()))
302 {
303 $display = static::DISPLAY_BLOCK;
304 }
305
306 if(!isset($font[static::FONT_BOLD]) && $this->isBoldTag($node->getTagName()))
307 {
308 $font[static::FONT_BOLD] = true;
309 }
310 if(!isset($font[static::FONT_ITALIC]) && $this->isItalicTag($node->getTagName()))
311 {
312 $font[static::FONT_ITALIC] = true;
313 }
314 if(!isset($font[static::FONT_UNDERLINED]) && $this->isUnderlinedTag($node->getTagName()))
315 {
316 $font[static::FONT_UNDERLINED] = true;
317 }
318 if($this->isDeletedTag($node->getTagName()))
319 {
320 $font[static::FONT_DELETED] = true;
321 }
322
323 if($display)
324 {
325 $result[static::DISPLAY] = $display;
326 }
327 $result['font'] = $font;
328 }
329
330 return $result;
331 }
332}
__construct(Node $node, array $properties=[], array $defaultProperties=[])