Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
styleinliner.php
1<?php
2namespace Bitrix\Main\Web\DOM;
3
5{
11 public static function inlineDocument(Document $document, array $styleList = null)
12 {
13 if(!$styleList)
14 {
15 $styleList = CssParser::parseDocument($document, true);
16 }
17
18 foreach($styleList as $rule)
19 {
20 $nodeList = $document->querySelectorAll($rule['SELECTOR']);
21 foreach($nodeList as $node)
22 {
23 static::setStyle($node, $rule['STYLE'], true);
24 }
25 }
26 }
27
28 public static function inlineHtml($html, array $styleList = null)
29 {
30 $document = new Document;
31 $document->loadHTML($html);
32
33 static::inlineDocument($document, $styleList);
34
35 return $document->saveHTML();
36 }
37
38 /*
39 * @param Element $node
40 * @param bool $singleStyle
41 * @return array
42 */
43 public static function getStyle(Element $node, $singleStyle = true)
44 {
45 $styleList = array();
46 $style = $node->getAttribute("style");
47 if($style)
48 {
49 $styleList = CssParser::getDeclarationArray($style, $singleStyle);
50 }
51
52 return $styleList;
53 }
54
55 /*
56 * @param Element $node
57 * @param array $styleList
58 * @param bool $append
59 * @return void
60 */
61 public static function setStyle(Element $node, $styleList, $append = false)
62 {
63 if($append)
64 {
65 if(empty($styleList))
66 {
67 return;
68 }
69
70 $result = static::getStyle($node);
71 foreach($styleList as $k => $v)
72 {
73 if("!important" !== mb_substr(mb_strtolower($styleList[$k]), -10))
74 {
75 if(array_key_exists($k, $result))
76 {
77 continue;
78 }
79 }
80
81 $result[$k] = $v;
82 }
83 }
84 else
85 {
86 $result = $styleList;
87 }
88
89 $node->setAttribute("style", CssParser::getDeclarationString($result));
90 }
91}
static getDeclarationString($declarationList)
static getDeclarationArray($declarationBlock, $singleStyle=true)
Definition cssparser.php:92
static parseDocument(Document $document, $sort=false)
Definition cssparser.php:7
setAttribute($name, $value)
Definition element.php:202
querySelectorAll($queryString)
Definition node.php:394
static inlineDocument(Document $document, array $styleList=null)
static setStyle(Element $node, $styleList, $append=false)
static inlineHtml($html, array $styleList=null)
static getStyle(Element $node, $singleStyle=true)