PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.24
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.24
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
woocommerce-pos / vendor_prefixed / dompdf / dompdf / src / Css / AttributeTranslator.php

AttributeTranslator.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.24, at vendor_prefixed/dompdf/dompdf/src/Css/AttributeTranslator.php

496 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @package dompdf
5 * @link https://github.com/dompdf/dompdf
6 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7 */
8 namespace WCPOS\Vendor\Dompdf\Css;
9
10 use WCPOS\Vendor\Dompdf\Frame;
11 use WCPOS\Vendor\Dompdf\Helpers;
12 /**
13 * Translates HTML 4.0 attributes into CSS rules
14 *
15 * @package dompdf
16 */
17 class AttributeTranslator
18 {
19 static $_style_attr = "_html_style_attribute";
20 // Munged data originally from
21 // http://www.w3.org/TR/REC-html40/index/attributes.html
22 // http://www.cs.tut.fi/~jkorpela/html2css.html
23 private static $__ATTRIBUTE_LOOKUP = [
24 //'caption' => array ( 'align' => '', ),
25 'img' => ['align' => ['bottom' => 'vertical-align: baseline;', 'middle' => 'vertical-align: middle;', 'top' => 'vertical-align: top;', 'left' => 'float: left;', 'right' => 'float: right;'], 'border' => 'border: %0.2Fpx solid;', 'height' => '_set_px_height', 'hspace' => 'padding-left: %1$0.2Fpx; padding-right: %1$0.2Fpx;', 'vspace' => 'padding-top: %1$0.2Fpx; padding-bottom: %1$0.2Fpx;', 'width' => '_set_px_width'],
26 'table' => [
27 'align' => ['left' => 'margin-left: 0; margin-right: auto;', 'center' => 'margin-left: auto; margin-right: auto;', 'right' => 'margin-left: auto; margin-right: 0;'],
28 'bgcolor' => 'background-color: %s;',
29 'border' => '_set_table_border',
30 'cellpadding' => '_set_table_cellpadding',
31 //'border-spacing: %0.2F; border-collapse: separate;',
32 'cellspacing' => '_set_table_cellspacing',
33 'frame' => ['void' => 'border-style: none;', 'above' => 'border-top-style: solid;', 'below' => 'border-bottom-style: solid;', 'hsides' => 'border-left-style: solid; border-right-style: solid;', 'vsides' => 'border-top-style: solid; border-bottom-style: solid;', 'lhs' => 'border-left-style: solid;', 'rhs' => 'border-right-style: solid;', 'box' => 'border-style: solid;', 'border' => 'border-style: solid;'],
34 'rules' => '_set_table_rules',
35 'width' => 'width: %s;',
36 ],
37 'hr' => [
38 'align' => '_set_hr_align',
39 // Need to grab width to set 'left' & 'right' correctly
40 'noshade' => 'border-style: solid;',
41 'size' => '_set_hr_size',
42 //'border-width: %0.2F px;',
43 'width' => 'width: %s;',
44 ],
45 'div' => ['align' => 'text-align: %s;'],
46 'h1' => ['align' => 'text-align: %s;'],
47 'h2' => ['align' => 'text-align: %s;'],
48 'h3' => ['align' => 'text-align: %s;'],
49 'h4' => ['align' => 'text-align: %s;'],
50 'h5' => ['align' => 'text-align: %s;'],
51 'h6' => ['align' => 'text-align: %s;'],
52 //TODO: translate more form element attributes
53 'input' => ['size' => '_set_input_width'],
54 'p' => ['align' => 'text-align: %s;'],
55 // 'col' => array(
56 // 'align' => '',
57 // 'valign' => '',
58 // ),
59 // 'colgroup' => array(
60 // 'align' => '',
61 // 'valign' => '',
62 // ),
63 'tbody' => ['align' => '_set_table_row_align', 'valign' => '_set_table_row_valign'],
64 'td' => ['align' => 'text-align: %s;', 'bgcolor' => '_set_background_color', 'height' => 'height: %s;', 'nowrap' => 'white-space: nowrap;', 'valign' => 'vertical-align: %s;', 'width' => 'width: %s;'],
65 'tfoot' => ['align' => '_set_table_row_align', 'valign' => '_set_table_row_valign'],
66 'th' => ['align' => 'text-align: %s;', 'bgcolor' => '_set_background_color', 'height' => 'height: %s;', 'nowrap' => 'white-space: nowrap;', 'valign' => 'vertical-align: %s;', 'width' => 'width: %s;'],
67 'thead' => ['align' => '_set_table_row_align', 'valign' => '_set_table_row_valign'],
68 'tr' => ['align' => '_set_table_row_align', 'bgcolor' => '_set_table_row_bgcolor', 'valign' => '_set_table_row_valign'],
69 'body' => ['background' => 'background-image: url(%s);', 'bgcolor' => '_set_background_color', 'link' => '_set_body_link', 'text' => '_set_color'],
70 'br' => ['clear' => 'clear: %s;'],
71 'basefont' => ['color' => '_set_color', 'face' => 'font-family: %s;', 'size' => '_set_basefont_size'],
72 'font' => ['color' => '_set_color', 'face' => 'font-family: %s;', 'size' => '_set_font_size'],
73 'dir' => ['compact' => 'margin: 0.5em 0;'],
74 'dl' => ['compact' => 'margin: 0.5em 0;'],
75 'menu' => ['compact' => 'margin: 0.5em 0;'],
76 'ol' => ['compact' => 'margin: 0.5em 0;', 'start' => 'counter-reset: -dompdf-default-counter %d;', 'type' => '_set_list_style_type'],
77 'ul' => ['compact' => 'margin: 0.5em 0;', 'type' => '_set_list_style_type'],
78 'li' => ['type' => '_set_list_style_type', 'value' => 'counter-reset: -dompdf-default-counter %d;'],
79 'pre' => ['width' => 'width: %s;'],
80 ];
81 protected static $_last_basefont_size = 3;
82 protected static $_font_size_lookup = [
83 // For basefont support
84 -3 => "4pt",
85 -2 => "5pt",
86 -1 => "6pt",
87 0 => "7pt",
88 1 => "8pt",
89 2 => "10pt",
90 3 => "12pt",
91 4 => "14pt",
92 5 => "18pt",
93 6 => "24pt",
94 7 => "34pt",
95 // For basefont support
96 8 => "48pt",
97 9 => "44pt",
98 10 => "52pt",
99 11 => "60pt",
100 ];
101 /**
102 * @param Frame $frame
103 */
104 static function translate_attributes(Frame $frame)
105 {
106 $node = $frame->get_node();
107 $tag = $node->nodeName;
108 if (!isset(self::$__ATTRIBUTE_LOOKUP[$tag])) {
109 return;
110 }
111 $valid_attrs = self::$__ATTRIBUTE_LOOKUP[$tag];
112 $attrs = $node->attributes;
113 $style = \rtrim($node->getAttribute(self::$_style_attr), "; ");
114 if ($style != "") {
115 $style .= ";";
116 }
117 foreach ($attrs as $attr => $attr_node) {
118 if (!isset($valid_attrs[$attr])) {
119 continue;
120 }
121 $value = $attr_node->value;
122 $target = $valid_attrs[$attr];
123 // Look up $value in $target, if $target is an array:
124 if (\is_array($target)) {
125 if (isset($target[$value])) {
126 $style .= " " . self::_resolve_target($node, $target[$value], $value);
127 }
128 } else {
129 // otherwise use target directly
130 $style .= " " . self::_resolve_target($node, $target, $value);
131 }
132 }
133 if (!\is_null($style)) {
134 $style = \ltrim($style);
135 $node->setAttribute(self::$_style_attr, $style);
136 }
137 }
138 /**
139 * @param \DOMNode $node
140 * @param string $target
141 * @param string $value
142 *
143 * @return string
144 */
145 protected static function _resolve_target(\DOMNode $node, $target, $value)
146 {
147 if ($target[0] === "_") {
148 return self::$target($node, $value);
149 }
150 return $value ? \sprintf($target, $value) : "";
151 }
152 /**
153 * @param \DOMElement $node
154 * @param string $new_style
155 */
156 static function append_style(\DOMElement $node, $new_style)
157 {
158 $style = \rtrim($node->getAttribute(self::$_style_attr), ";");
159 $style .= $new_style;
160 $style = \ltrim($style, ";");
161 $node->setAttribute(self::$_style_attr, $style);
162 }
163 /**
164 * @param \DOMNode $node
165 *
166 * @return \DOMNodeList|\DOMElement[]
167 */
168 protected static function get_cell_list(\DOMNode $node)
169 {
170 $xpath = new \DOMXpath($node->ownerDocument);
171 switch ($node->nodeName) {
172 default:
173 case "table":
174 $query = "tr/td | thead/tr/td | tbody/tr/td | tfoot/tr/td | tr/th | thead/tr/th | tbody/tr/th | tfoot/tr/th";
175 break;
176 case "tbody":
177 case "tfoot":
178 case "thead":
179 $query = "tr/td | tr/th";
180 break;
181 case "tr":
182 $query = "td | th";
183 break;
184 }
185 return $xpath->query($query, $node);
186 }
187 /**
188 * @param string $value
189 *
190 * @return string
191 */
192 protected static function _get_valid_color($value)
193 {
194 if (\preg_match('/^#?([0-9A-F]{6})$/i', $value, $matches)) {
195 $value = "#{$matches[1]}";
196 }
197 return $value;
198 }
199 /**
200 * @param \DOMElement $node
201 * @param string $value
202 *
203 * @return string
204 */
205 protected static function _set_color(\DOMElement $node, $value)
206 {
207 $value = self::_get_valid_color($value);
208 return "color: {$value};";
209 }
210 /**
211 * @param \DOMElement $node
212 * @param string $value
213 *
214 * @return string
215 */
216 protected static function _set_background_color(\DOMElement $node, $value)
217 {
218 $value = self::_get_valid_color($value);
219 return "background-color: {$value};";
220 }
221 protected static function _set_px_width(\DOMElement $node, string $value) : string
222 {
223 $v = \trim($value);
224 if (Helpers::is_percent($v)) {
225 return \sprintf("width: %s;", $v);
226 }
227 if (\is_numeric(\mb_substr($v, 0, 1))) {
228 return \sprintf("width: %spx;", (float) $v);
229 }
230 return "";
231 }
232 protected static function _set_px_height(\DOMElement $node, string $value) : string
233 {
234 $v = \trim($value);
235 if (Helpers::is_percent($v)) {
236 return \sprintf("height: %s;", $v);
237 }
238 if (\is_numeric(\mb_substr($v, 0, 1))) {
239 return \sprintf("height: %spx;", (float) $v);
240 }
241 return "";
242 }
243 /**
244 * @param \DOMElement $node
245 * @param string $value
246 *
247 * @return null
248 */
249 protected static function _set_table_cellpadding(\DOMElement $node, $value)
250 {
251 $cell_list = self::get_cell_list($node);
252 foreach ($cell_list as $cell) {
253 self::append_style($cell, "; padding: {$value}px;");
254 }
255 return null;
256 }
257 /**
258 * @param \DOMElement $node
259 * @param string $value
260 *
261 * @return string
262 */
263 protected static function _set_table_border(\DOMElement $node, $value)
264 {
265 return "border-width: {$value}" . "px;";
266 }
267 /**
268 * @param \DOMElement $node
269 * @param string $value
270 *
271 * @return string
272 */
273 protected static function _set_table_cellspacing(\DOMElement $node, $value)
274 {
275 $style = \rtrim($node->getAttribute(self::$_style_attr), ";");
276 if ($value == 0) {
277 $style .= "; border-collapse: collapse;";
278 } else {
279 $style .= "; border-spacing: {$value}px; border-collapse: separate;";
280 }
281 return \ltrim($style, ";");
282 }
283 /**
284 * @param \DOMElement $node
285 * @param string $value
286 *
287 * @return null|string
288 */
289 protected static function _set_table_rules(\DOMElement $node, $value)
290 {
291 $new_style = "; border-collapse: collapse;";
292 switch ($value) {
293 case "none":
294 $new_style .= "border-style: none;";
295 break;
296 case "groups":
297 // FIXME: unsupported
298 return null;
299 case "rows":
300 $new_style .= "border-style: solid none solid none; border-width: 1px; ";
301 break;
302 case "cols":
303 $new_style .= "border-style: none solid none solid; border-width: 1px; ";
304 break;
305 case "all":
306 $new_style .= "border-style: solid; border-width: 1px; ";
307 break;
308 default:
309 // Invalid value
310 return null;
311 }
312 $cell_list = self::get_cell_list($node);
313 foreach ($cell_list as $cell) {
314 $style = $cell->getAttribute(self::$_style_attr);
315 $style .= $new_style;
316 $cell->setAttribute(self::$_style_attr, $style);
317 }
318 $style = \rtrim($node->getAttribute(self::$_style_attr), ";");
319 $style .= "; border-collapse: collapse; ";
320 return \ltrim($style, "; ");
321 }
322 /**
323 * @param \DOMElement $node
324 * @param string $value
325 *
326 * @return string
327 */
328 protected static function _set_hr_size(\DOMElement $node, $value)
329 {
330 $style = \rtrim($node->getAttribute(self::$_style_attr), ";");
331 $style .= "; border-width: " . \max(0, $value - 2) . "; ";
332 return \ltrim($style, "; ");
333 }
334 /**
335 * @param \DOMElement $node
336 * @param string $value
337 *
338 * @return null|string
339 */
340 protected static function _set_hr_align(\DOMElement $node, $value)
341 {
342 $style = \rtrim($node->getAttribute(self::$_style_attr), ";");
343 $width = $node->getAttribute("width");
344 if ($width == "") {
345 $width = "100%";
346 }
347 $remainder = 100 - (float) \rtrim($width, "% ");
348 switch ($value) {
349 case "left":
350 $style .= "; margin-right: {$remainder} %;";
351 break;
352 case "right":
353 $style .= "; margin-left: {$remainder} %;";
354 break;
355 case "center":
356 $style .= "; margin-left: auto; margin-right: auto;";
357 break;
358 default:
359 return null;
360 }
361 return \ltrim($style, "; ");
362 }
363 /**
364 * @param \DOMElement $node
365 * @param string $value
366 *
367 * @return null|string
368 */
369 protected static function _set_input_width(\DOMElement $node, $value)
370 {
371 if (empty($value)) {
372 return null;
373 }
374 if ($node->hasAttribute("type") && \in_array(\strtolower($node->getAttribute("type")), ["text", "password"])) {
375 return \sprintf("width: %Fem", (int) $value * 0.65 + 2);
376 } else {
377 return \sprintf("width: %upx;", (int) $value);
378 }
379 }
380 /**
381 * @param \DOMElement $node
382 * @param string $value
383 *
384 * @return null
385 */
386 protected static function _set_table_row_align(\DOMElement $node, $value)
387 {
388 $cell_list = self::get_cell_list($node);
389 foreach ($cell_list as $cell) {
390 self::append_style($cell, "; text-align: {$value};");
391 }
392 return null;
393 }
394 /**
395 * @param \DOMElement $node
396 * @param string $value
397 *
398 * @return null
399 */
400 protected static function _set_table_row_valign(\DOMElement $node, $value)
401 {
402 $cell_list = self::get_cell_list($node);
403 foreach ($cell_list as $cell) {
404 self::append_style($cell, "; vertical-align: {$value};");
405 }
406 return null;
407 }
408 /**
409 * @param \DOMElement $node
410 * @param string $value
411 *
412 * @return null
413 */
414 protected static function _set_table_row_bgcolor(\DOMElement $node, $value)
415 {
416 $cell_list = self::get_cell_list($node);
417 $value = self::_get_valid_color($value);
418 foreach ($cell_list as $cell) {
419 self::append_style($cell, "; background-color: {$value};");
420 }
421 return null;
422 }
423 /**
424 * @param \DOMElement $node
425 * @param string $value
426 *
427 * @return null
428 */
429 protected static function _set_body_link(\DOMElement $node, $value)
430 {
431 $a_list = $node->getElementsByTagName("a");
432 $value = self::_get_valid_color($value);
433 foreach ($a_list as $a) {
434 self::append_style($a, "; color: {$value};");
435 }
436 return null;
437 }
438 /**
439 * @param \DOMElement $node
440 * @param string $value
441 *
442 * @return null
443 */
444 protected static function _set_basefont_size(\DOMElement $node, $value)
445 {
446 // FIXME: ? we don't actually set the font size of anything here, just
447 // the base size for later modification by <font> tags.
448 self::$_last_basefont_size = $value;
449 return null;
450 }
451 /**
452 * @param \DOMElement $node
453 * @param string $value
454 *
455 * @return string
456 */
457 protected static function _set_font_size(\DOMElement $node, $value)
458 {
459 $style = $node->getAttribute(self::$_style_attr);
460 if ($value[0] === "-" || $value[0] === "+") {
461 $value = self::$_last_basefont_size + (int) $value;
462 }
463 if (isset(self::$_font_size_lookup[$value])) {
464 $style .= "; font-size: " . self::$_font_size_lookup[$value] . ";";
465 } else {
466 $style .= "; font-size: {$value};";
467 }
468 return \ltrim($style, "; ");
469 }
470 protected static function _set_list_style_type(\DOMElement $node, string $value) : string
471 {
472 $v = \trim($value);
473 switch ($v) {
474 case "1":
475 $type = "decimal";
476 break;
477 case "a":
478 $type = "lower-alpha";
479 break;
480 case "A":
481 $type = "upper-alpha";
482 break;
483 case "i":
484 $type = "lower-roman";
485 break;
486 case "I":
487 $type = "upper-roman";
488 break;
489 default:
490 $type = $v;
491 break;
492 }
493 return "list-style-type: {$type};";
494 }
495 }
496