| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | * @author Florian Schmitz, Brett Zamir, Nikolay Matsievsky, Cedric Morin, Christopher Finke, Mark Scherer, Tobias Bäthge |
| 8 | 8 | * @since 1.0.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +declare(strict_types=1); | |
| 12 | + | |
| 11 | 13 | // Prohibit direct script loading. |
| 12 | 14 | defined( 'ABSPATH' ) || die( 'No direct script access allowed!' ); |
| 13 | 15 | |
| 14 | 16 | /** |
| @@ -24,83 +26,73 @@ | ||
| 24 | 26 | /** |
| 25 | 27 | * TablePress_CSSTidy instance. |
| 26 | 28 | * |
| 27 | 29 | * @since 1.0.0 |
| 28 | - * @var TablePress_CSSTidy | |
| 29 | 30 | */ |
| 30 | - public $parser; | |
| 31 | + public \TablePress_CSSTidy $parser; | |
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * The parsed CSS. |
| 34 | 35 | * |
| 35 | 36 | * @since 1.0.0 |
| 36 | - * @var array | |
| 37 | 37 | */ |
| 38 | - public $css = array(); | |
| 38 | + public array $css = array(); | |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * The output templates. |
| 42 | 42 | * |
| 43 | 43 | * @since 1.0.0 |
| 44 | - * @var array | |
| 45 | 44 | */ |
| 46 | - public $template = array(); | |
| 45 | + public array $template = array(); | |
| 47 | 46 | |
| 48 | 47 | /** |
| 49 | 48 | * The raw parsed CSS. |
| 50 | 49 | * |
| 51 | 50 | * @since 1.0.0 |
| 52 | - * @var array | |
| 53 | 51 | */ |
| 54 | - public $tokens = array(); | |
| 52 | + public array $tokens = array(); | |
| 55 | 53 | |
| 56 | 54 | /** |
| 57 | 55 | * The CSS charset. |
| 58 | 56 | * |
| 59 | 57 | * @since 1.0.0 |
| 60 | - * @var string | |
| 61 | 58 | */ |
| 62 | - public $charset = ''; | |
| 59 | + public string $charset = ''; | |
| 63 | 60 | |
| 64 | 61 | /** |
| 65 | 62 | * All @import URLs. |
| 66 | 63 | * |
| 67 | 64 | * @since 1.0.0 |
| 68 | - * @var array | |
| 69 | 65 | */ |
| 70 | - public $import = array(); | |
| 66 | + public array $import = array(); | |
| 71 | 67 | |
| 72 | 68 | /** |
| 73 | 69 | * The namespace. |
| 74 | 70 | * |
| 75 | 71 | * @since 1.0.0 |
| 76 | - * @var string | |
| 77 | 72 | */ |
| 78 | - public $namespace = ''; | |
| 73 | + public string $namespace = ''; | |
| 79 | 74 | |
| 80 | 75 | /** |
| 81 | 76 | * Saves the input CSS string. |
| 82 | 77 | * |
| 83 | 78 | * @since 1.0.0 |
| 84 | - * @var string | |
| 85 | 79 | */ |
| 86 | - public $input_css = ''; | |
| 80 | + public string $input_css = ''; | |
| 87 | 81 | |
| 88 | 82 | /** |
| 89 | 83 | * Saves the formatted CSS string. |
| 90 | 84 | * |
| 91 | 85 | * @since 1.0.0 |
| 92 | - * @var string | |
| 93 | 86 | */ |
| 94 | - public $output_css = ''; | |
| 87 | + public string $output_css = ''; | |
| 95 | 88 | |
| 96 | 89 | /** |
| 97 | 90 | * Saves the formatted CSS string (plain text). |
| 98 | 91 | * |
| 99 | 92 | * @since 1.0.0 |
| 100 | - * @var string | |
| 101 | 93 | */ |
| 102 | - public $output_css_plain = ''; | |
| 94 | + public string $output_css_plain = ''; | |
| 103 | 95 | |
| 104 | 96 | /** |
| 105 | 97 | * Constructor. |
| 106 | 98 | * |
| @@ -141,66 +133,8 @@ | ||
| 141 | 133 | return $this->output_css_plain; |
| 142 | 134 | } |
| 143 | 135 | |
| 144 | 136 | /** |
| 145 | - * Returns the formatted CSS code. | |
| 146 | - * | |
| 147 | - * @since 1.0 | |
| 148 | - * | |
| 149 | - * @param string $default_media Optional. Default @media to add to selectors without any @media. | |
| 150 | - * @return string Formatted CSS. | |
| 151 | - */ | |
| 152 | - public function formatted( string $default_media = '' ): string { | |
| 153 | - $this->_print( false, $default_media ); | |
| 154 | - return $this->output_css; | |
| 155 | - } | |
| 156 | - | |
| 157 | - /** | |
| 158 | - * Returns the formatted CSS code to make a complete webpage. | |
| 159 | - * | |
| 160 | - * @since 1.0 | |
| 161 | - * | |
| 162 | - * @param string $doctype Optional. Shorthand for the document type. | |
| 163 | - * @param bool $externalcss Optional. Indicates whether styles to be attached internally or as an external stylesheet. | |
| 164 | - * @param string $title Optional. Title to be added in the head of the document. | |
| 165 | - * @param string $lang Optional. Two-letter language code to be added to the output. | |
| 166 | - * @return string Formatted CSS for a full page. | |
| 167 | - */ | |
| 168 | - public function formatted_page( string $doctype = 'html5', bool $externalcss = true, string $title = '', string $lang = 'en' ): string { | |
| 169 | - switch ( $doctype ) { | |
| 170 | - case 'html5': | |
| 171 | - $doctype_output = '<!DOCTYPE html>'; | |
| 172 | - break; | |
| 173 | - case 'xhtml1.0strict': | |
| 174 | - $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; | |
| 175 | - break; | |
| 176 | - case 'xhtml1.1': | |
| 177 | - default: | |
| 178 | - $doctype_output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'; | |
| 179 | - break; | |
| 180 | - } | |
| 181 | - | |
| 182 | - $output = ''; | |
| 183 | - $output .= $doctype_output . "\n" . '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="' . $lang . '"'; | |
| 184 | - $output .= ( 'xhtml1.1' === $doctype ) ? '>' : ' lang="' . $lang . '">'; | |
| 185 | - $output .= "\n<head>\n\t<title>{$title}</title>"; | |
| 186 | - | |
| 187 | - if ( $externalcss ) { | |
| 188 | - $output .= "\n\t<style>\n"; | |
| 189 | - $output .= file_get_contents( 'cssparsed.css' ); // Adds an invisible BOM or something, but not in css_optimised.php. | |
| 190 | - $output .= "\n</style>"; | |
| 191 | - } else { | |
| 192 | - $output .= "\n" . '<link rel="stylesheet" type="text/css" href="cssparsed.css" />'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet | |
| 193 | - } | |
| 194 | - $output .= "\n</head>\n<body><code id=\"copytext\">"; | |
| 195 | - $output .= $this->formatted(); | |
| 196 | - $output .= '</code>' . "\n" . '</body></html>'; | |
| 197 | - | |
| 198 | - $this->output_css_plain = $output; | |
| 199 | - return $this->output_css_plain; | |
| 200 | - } | |
| 201 | - | |
| 202 | - /** | |
| 203 | 137 | * Returns the formatted CSS Code and saves it into $this->output_css and $this->output_css_plain. |
| 204 | 138 | * |
| 205 | 139 | * @since 1.0 |
| 206 | 140 | * |
| @@ -279,9 +213,9 @@ | ||
| 279 | 213 | } |
| 280 | 214 | if ( $this->parser->get_cfg( 'preserve_css' ) ) { |
| 281 | 215 | $token[1] = str_replace( ',', ",\n", $token[1] ); |
| 282 | 216 | } |
| 283 | - $out .= ( '@' !== $token[1][0] ) ? $template[2] . $this->_htmlsp( $token[1], $plain ) : $template[0] . $this->_htmlsp( $token[1], $plain ); | |
| 217 | + $out .= ( '' !== $token[1] && '@' !== $token[1][0] ) ? $template[2] . $this->_htmlsp( $token[1], $plain ) : $template[0] . $this->_htmlsp( $token[1], $plain ); | |
| 284 | 218 | $out .= $template[3]; |
| 285 | 219 | break; |
| 286 | 220 | case TablePress_CSSTidy::PROPERTY: |
| 287 | 221 | if ( 2 === $this->parser->get_cfg( 'case_properties' ) ) { |
| @@ -319,9 +253,12 @@ | ||
| 319 | 253 | $out = &$in_at_out[ $indent_level - 1 ]; |
| 320 | 254 | } else { |
| 321 | 255 | $out = &$output; |
| 322 | 256 | } |
| 323 | - $out .= $template[10] . $in_at_out[ $indent_level ]; | |
| 257 | + $out .= $template[10]; | |
| 258 | + if ( isset( $in_at_out[ $indent_level ] ) ) { | |
| 259 | + $out .= $in_at_out[ $indent_level ]; | |
| 260 | + } | |
| 324 | 261 | if ( TablePress_CSSTidy::AT_END !== $this->_seeknocomment( $key, 1 ) ) { |
| 325 | 262 | $out .= $template[9]; |
| 326 | 263 | } else { |
| 327 | 264 | $out .= rtrim( $template[9] ); |
| @@ -469,64 +406,7 @@ | ||
| 469 | 406 | if ( ! $plain ) { |
| 470 | 407 | return htmlspecialchars( $a_string, ENT_QUOTES, 'utf-8' ); |
| 471 | 408 | } |
| 472 | 409 | return $a_string; |
| 473 | - } | |
| 474 | - | |
| 475 | - /** | |
| 476 | - * Gets compression ratio. | |
| 477 | - * | |
| 478 | - * @since 1.2 | |
| 479 | - * | |
| 480 | - * @return double Compression ratio. | |
| 481 | - */ | |
| 482 | - public function get_ratio(): float { | |
| 483 | - if ( ! $this->output_css_plain ) { | |
| 484 | - $this->formatted(); | |
| 485 | - } | |
| 486 | - return round( ( strlen( $this->input_css ) - strlen( $this->output_css_plain ) ) / strlen( $this->input_css ), 3 ) * 100; | |
| 487 | - } | |
| 488 | - | |
| 489 | - /** | |
| 490 | - * Gets difference between the old and new code in bytes and prints the code if necessary. | |
| 491 | - * | |
| 492 | - * @since 1.1 | |
| 493 | - * | |
| 494 | - * @return string Size difference. | |
| 495 | - */ | |
| 496 | - public function get_diff(): string { | |
| 497 | - if ( ! $this->output_css_plain ) { | |
| 498 | - $this->formatted(); | |
| 499 | - } | |
| 500 | - | |
| 501 | - $diff = strlen( $this->output_css_plain ) - strlen( $this->input_css ); | |
| 502 | - | |
| 503 | - if ( $diff > 0 ) { | |
| 504 | - return '+' . $diff; | |
| 505 | - } elseif ( 0 === $diff ) { | |
| 506 | - return '+-' . $diff; | |
| 507 | - } | |
| 508 | - | |
| 509 | - return (string) $diff; | |
| 510 | - } | |
| 511 | - | |
| 512 | - /** | |
| 513 | - * Gets the size of either input or output CSS in kilobytes (KB). | |
| 514 | - * | |
| 515 | - * @since 1.0 | |
| 516 | - * | |
| 517 | - * @param string $loc Optional. Location of the CSS. | |
| 518 | - * @return double Size of the CSS. | |
| 519 | - */ | |
| 520 | - public function size( string $loc = 'output' ): float { | |
| 521 | - if ( 'output' === $loc && ! $this->output_css ) { | |
| 522 | - $this->formatted(); | |
| 523 | - } | |
| 524 | - | |
| 525 | - if ( 'input' === $loc ) { | |
| 526 | - return strlen( $this->input_css ) / 1000; | |
| 527 | - } else { | |
| 528 | - return strlen( $this->output_css_plain ) / 1000; | |
| 529 | - } | |
| 530 | 410 | } |
| 531 | 411 | |
| 532 | 412 | } // class TablePress_CSSTidy_Print |