| @@ -372,10 +372,12 @@ | ||
| 372 | 372 | . '<body class="easy-invoice-pdf easy-invoice-pdf--design' . ( $has_canvas ? ' easy-invoice-pdf--canvas' : '' ) . '"><div class="' . esc_attr( $type ) . '-content">' |
| 373 | 373 | . $body . $extra . $after . '</div></body></html>'; |
| 374 | 374 | |
| 375 | 375 | // Resolved over the whole document so the compat sheet can refer to the |
| 376 | - // design's own palette (its :root block lives in the body). | |
| 377 | - $html = self::resolveCssVariables( $html ); | |
| 376 | + // design's own palette (its :root block lives in the body). The compat | |
| 377 | + // sheet's own :root wins: it sits in the head, so document order would | |
| 378 | + // otherwise hand every variable back to the design. | |
| 379 | + $html = self::resolveCssVariables( $html, self::rootVariables( $compat ) ); | |
| 378 | 380 | |
| 379 | 381 | /** |
| 380 | 382 | * Filter the complete HTML of a design-based PDF before dompdf sees it. |
| 381 | 383 | * |
| @@ -393,14 +395,18 @@ | ||
| 393 | 395 | * |
| 394 | 396 | * @param string $html Markup with inline <style> blocks. |
| 395 | 397 | * @return string |
| 396 | 398 | */ |
| 397 | - public static function resolveCssVariables( string $html ): string { | |
| 398 | - if ( false === strpos( $html, 'var(--' ) ) { | |
| 399 | - return $html; | |
| 400 | - } | |
| 399 | + /** | |
| 400 | + * The custom properties declared in every `:root` block of a stylesheet or | |
| 401 | + * document, last declaration winning. | |
| 402 | + * | |
| 403 | + * @param string $css CSS or HTML. | |
| 404 | + * @return array<string, string> | |
| 405 | + */ | |
| 406 | + private static function rootVariables( string $css ): array { | |
| 401 | 407 | $vars = []; |
| 402 | - if ( preg_match_all( '/:root\s*\{([^}]*)\}/', $html, $blocks ) ) { | |
| 408 | + if ( preg_match_all( '/:root\s*\{([^}]*)\}/', $css, $blocks ) ) { | |
| 403 | 409 | foreach ( $blocks[1] as $block ) { |
| 404 | 410 | foreach ( explode( ';', $block ) as $declaration ) { |
| 405 | 411 | if ( preg_match( '/--([a-zA-Z0-9\-_]+)\s*:\s*(.+)$/', trim( $declaration ), $m ) ) { |
| 406 | 412 | $vars[ $m[1] ] = trim( $m[2] ); |
| @@ -406,8 +412,19 @@ | ||
| 406 | 412 | $vars[ $m[1] ] = trim( $m[2] ); |
| 407 | 413 | } |
| 408 | 414 | } |
| 409 | 415 | } |
| 416 | + } | |
| 417 | + return $vars; | |
| 418 | + } | |
| 419 | + | |
| 420 | + public static function resolveCssVariables( string $html, array $overrides = [] ): string { | |
| 421 | + if ( false === strpos( $html, 'var(--' ) ) { | |
| 422 | + return $html; | |
| 423 | + } | |
| 424 | + $vars = self::rootVariables( $html ); | |
| 425 | + foreach ( $overrides as $name => $value ) { | |
| 426 | + $vars[ $name ] = $value; | |
| 410 | 427 | } |
| 411 | 428 | // Values may reference other variables; three passes cover any sane chain. |
| 412 | 429 | for ( $i = 0; $i < 3 && false !== strpos( $html, 'var(--' ); $i++ ) { |
| 413 | 430 | $html = (string) preg_replace_callback( |