Repository navigation
Expand file tree
/
Copy pathCssManager.php
More file actions
234 lines (212 loc) · 5.55 KB
/
Copy pathCssManager.php
File metadata and controls
234 lines (212 loc) · 5.55 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Mpdf;
use Mpdf\Css\CssMerger;
use Mpdf\Exception\InvalidArgumentException;
use Mpdf\Utils\Arrays;
use Mpdf\Css\CssParser;
class CssManager
{
/**
* @var \Mpdf\Css\CssParser
*/
private $cssParser;
/**
* @var \Mpdf\Css\CssMerger
*/
private $cssMerger;
/**
* Main CSS property storage array.
*
* Stores CSS properties for simple selectors (depth 1).
* Format:
* [
* 'P' => [
* 'COLOR' => '#FF0000',
* 'FONT-SIZE' => '12pt',
* ],
* 'CLASS>>MYCLASS' => [
* 'BORDER' => '1px solid black',
* ],
* ...
* ]
*
* @var array
*/
public $CSS = [];
/**
* CSS cascade storage for table elements.
*
* Stores cascaded CSS properties specifically for table elements (TABLE, THEAD, TBODY, TFOOT, TR, TH, TD).
* Format is a nested array mirroring the selector hierarchy.
* Example for "DIV TABLE TD":
* [
* 'DIV' => [
* 'TABLE' => [
* 'TD' => [
* 'BORDER' => '1px solid green',
* 'depth' => 3
* ]
* ]
* ]
* ]
*
* @var array
*/
public $tablecascadeCSS = [];
/**
* Cascading CSS property storage.
*
* Stores CSS properties for nested/cascaded selectors (depth > 1).
* Format is a nested array mirroring the selector hierarchy.
* Example for "DIV.myclass P":
* [
* 'DIV' => [
* 'CLASS>>MYCLASS' => [
* 'P' => [
* 'COLOR' => '#0000FF',
* 'depth' => 3
* ]
* ]
* ]
* ]
*
* @var array
*/
public $cascadeCSS = [];
/**
* @var int Table CSS cascade level counter
*/
public $tbCSSlvl = 0;
/**
* CssManager constructor.
*
* Initializes the CSS manager with required dependencies and sets up
* internal storage structures for CSS properties and cascading.
*
* @param CssParser $cssParser
* @param CssMerger $cssMerger
*/
public function __construct(CssParser $cssParser, CssMerger $cssMerger)
{
$this->cssParser = $cssParser;
$this->cssMerger = $cssMerger;
$this->cssMerger->setCssManager($this);
}
/**
* Read and parse CSS from HTML content.
*
* Extracts CSS from style tags, link tags, and @import statements within HTML.
* Processes external stylesheets, resolves URLs, handles media queries, and
* parses all CSS rules into the internal CSS storage structure.
*
* @param string $html HTML content containing CSS
* @return string HTML with CSS content removed
*/
public function readCss($html)
{
if (!is_array($this->cascadeCSS)) {
$this->cascadeCSS = [];
}
$html = $this->cssParser->parse($html);
$this->CSS = Arrays::uniqueRecursiveMerge($this->CSS, $this->cssParser->getCss());
$this->cascadeCSS = Arrays::uniqueRecursiveMerge($this->cascadeCSS, $this->cssParser->getCascadeCss());
return $html;
}
/**
* Parse inline CSS style attribute.
*
* @param string $html CSS string from style attribute
* @return array Parsed CSS properties
*/
public function readInlineCss($html)
{
return $this->cssParser->parseInlineCss($html);
}
/**
* Merge CSS properties for an HTML element.
*
* Main method for applying CSS to an element. Combines CSS from multiple sources
* including default styles, stylesheets, inline styles, and inherited properties.
* Handles inheritance type (BLOCK, INLINE, TABLE, TOPTABLE) and applies
* appropriate cascading rules.
*
* @param string $inherit Inheritance context (BLOCK, INLINE, TABLE, TOPTABLE)
* @param string $tag HTML tag name
* @param array $attr HTML attributes including CLASS, ID, STYLE
* @return array Merged CSS properties array
*/
public function mergeCss($inherit, $tag, $attr)
{
return $this->cssMerger->merge($inherit, $tag, $attr);
}
/**
* Preview block-level CSS without creating the block.
*
* Looks ahead to determine what CSS would be applied to a block element
* without actually creating it. Used for planning layout and spacing.
*
* @param string $tag HTML tag name
* @param array $attr HTML attributes array
* @return array CSS properties that would be applied
*/
public function previewBlockCss($tag, $attr)
{
return $this->cssMerger->previewBlockCss($tag, $attr);
}
public function getUsedClassNames()
{
return $this->cssParser->getUsedClassNames();
}
public function getMaxClassDepth()
{
return $this->cssParser->getMaxClassDepth();
}
/**
* Parse box-shadow CSS property.
*
* Converts box-shadow CSS property string into array format used internally.
* Handles multiple shadows, inset shadows, blur, spread, and colors.
*
* @param string $value Box-shadow property value
* @return array Array of shadow definitions
*/
public function setCssBoxShadow($value)
{
return $this->cssParser->parseBoxShadow($value);
}
/**
* Parse text-shadow CSS property.
*
* Converts text-shadow CSS property string into array format used internally.
* Handles multiple shadows, blur, and colors.
*
* @param string $value Text-shadow property value
* @return array Array of text shadow definitions
*/
public function setCssTextShadow($value)
{
return $this->cssParser->parseTextShadow($value);
}
/**
* Set border dominance level for a specific side.
*
* @param string $side T|R|B|L
* @param int $val Dominance value
* @throws InvalidArgumentException
* @return void
*/
public function setBorderDominance($side, $val)
{
$this->cssMerger->setBorderDominance($side, $val);
}
/**
* Get border dominance level for a specific side.
*
* @param string $side T|R|B|L
* @return int Dominance value
*/
public function getBorderDominance($side)
{
return $this->cssMerger->getBorderDominance($side);
}
}