third_party
4 years ago
IQuery.php
4 years ago
LICENSE
4 years ago
gan_formatter.php
4 years ago
gan_node_html.php
4 years ago
gan_parser_html.php
4 years ago
gan_selector_html.php
4 years ago
gan_tokenizer.php
4 years ago
gan_xml2array.php
4 years ago
ganon.php
4 years ago
index.php
4 years ago
pQuery.php
4 years ago
gan_formatter.php
383 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @author Niels A.D. |
| 4 | * @author Todd Burry <todd@vanillaforums.com> |
| 5 | * @copyright 2010 Niels A.D., 2014 Todd Burry |
| 6 | * @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1 |
| 7 | * @package pQuery |
| 8 | */ |
| 9 | |
| 10 | namespace MailPoetVendor\pQuery; |
| 11 | |
| 12 | if (!defined('ABSPATH')) exit; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Indents text |
| 17 | * @param string $text |
| 18 | * @param int $indent |
| 19 | * @param string $indent_string |
| 20 | * @return string |
| 21 | */ |
| 22 | function indent_text($text, $indent, $indent_string = ' ') { |
| 23 | if ($indent && $indent_string) { |
| 24 | return str_replace("\n", "\n".str_repeat($indent_string, $indent), $text); |
| 25 | } else { |
| 26 | return $text; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Class used to format/minify HTML nodes |
| 32 | * |
| 33 | * Used like: |
| 34 | * <code> |
| 35 | * <?php |
| 36 | * $formatter = new HtmlFormatter(); |
| 37 | * $formatter->format($root); |
| 38 | * ?> |
| 39 | * </code> |
| 40 | */ |
| 41 | class HtmlFormatter { |
| 42 | |
| 43 | /** |
| 44 | * Determines which elements start on a new line and which function as block |
| 45 | * @var array('element' => array('new_line' => true, 'as_block' => true, 'format_inside' => true)) |
| 46 | */ |
| 47 | var $block_elements = array( |
| 48 | 'p' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 49 | 'h1' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 50 | 'h2' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 51 | 'h3' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 52 | 'h4' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 53 | 'h5' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 54 | 'h6' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 55 | |
| 56 | 'form' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 57 | 'fieldset' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 58 | 'legend' => array('new_line' => true, 'as_block' => false, 'format_inside' => true), |
| 59 | 'dl' => array('new_line' => true, 'as_block' => false, 'format_inside' => true), |
| 60 | 'dt' => array('new_line' => true, 'as_block' => false, 'format_inside' => true), |
| 61 | 'dd' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 62 | 'ol' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 63 | 'ul' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 64 | 'li' => array('new_line' => true, 'as_block' => false, 'format_inside' => true), |
| 65 | |
| 66 | 'table' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 67 | 'tr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 68 | |
| 69 | 'dir' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 70 | 'menu' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 71 | 'address' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 72 | 'blockquote' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 73 | 'center' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 74 | 'del' => array('new_line' => true, 'as_block' => false, 'format_inside' => true), |
| 75 | //'div' => array('new_line' => false, 'as_block' => true, 'format_inside' => true), |
| 76 | 'hr' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 77 | 'ins' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 78 | 'noscript' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 79 | 'pre' => array('new_line' => true, 'as_block' => true, 'format_inside' => false), |
| 80 | 'script' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 81 | 'style' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 82 | |
| 83 | 'html' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 84 | 'head' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 85 | 'body' => array('new_line' => true, 'as_block' => true, 'format_inside' => true), |
| 86 | 'title' => array('new_line' => true, 'as_block' => false, 'format_inside' => false) |
| 87 | ); |
| 88 | |
| 89 | /** |
| 90 | * Determines which characters are considered whitespace |
| 91 | * @var array("\t" => true) True to recognize as new line |
| 92 | */ |
| 93 | var $whitespace = array( |
| 94 | ' ' => false, |
| 95 | "\t" => false, |
| 96 | "\x0B" => false, |
| 97 | "\0" => false, |
| 98 | "\n" => true, |
| 99 | "\r" => true |
| 100 | ); |
| 101 | |
| 102 | /** |
| 103 | * String that is used to generate correct indenting |
| 104 | * @var string |
| 105 | */ |
| 106 | var $indent_string = ' '; |
| 107 | |
| 108 | /** |
| 109 | * String that is used to break lines |
| 110 | * @var string |
| 111 | */ |
| 112 | var $linebreak_string = "\n"; |
| 113 | |
| 114 | /** |
| 115 | * Other formatting options |
| 116 | * @var array |
| 117 | */ |
| 118 | public $options = array( |
| 119 | 'img_alt' => '', |
| 120 | 'self_close_str' => null, |
| 121 | 'attribute_shorttag' => false, |
| 122 | 'sort_attributes' => false, |
| 123 | 'attributes_case' => CASE_LOWER, |
| 124 | 'minify_script' => true |
| 125 | ); |
| 126 | |
| 127 | /** |
| 128 | * Errors found during formatting |
| 129 | * @var array |
| 130 | */ |
| 131 | var $errors = array(); |
| 132 | |
| 133 | |
| 134 | /** |
| 135 | * Class constructor |
| 136 | * @param array $options {@link $options} |
| 137 | */ |
| 138 | function __construct($options = array()) { |
| 139 | $this->options = array_merge($this->options, $options); |
| 140 | |
| 141 | if (isset($options['indent_str'])) |
| 142 | $this->indent_string = $options['indent_str']; |
| 143 | |
| 144 | if (isset($options['linebreak_str'])) |
| 145 | $this->linebreak_string = $options['linebreak_str']; |
| 146 | } |
| 147 | |
| 148 | #php4 PHP4 class constructor compatibility |
| 149 | #function HtmlFormatter($options = array()) {return $this->__construct($options);} |
| 150 | #php4e |
| 151 | |
| 152 | /** |
| 153 | * Class magic invoke method, performs {@link format()} |
| 154 | * @access private |
| 155 | */ |
| 156 | function __invoke(&$node) { |
| 157 | return $this->format($node); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Minifies HTML / removes unneeded whitespace |
| 162 | * @param DomNode $root |
| 163 | * @param bool $strip_comments |
| 164 | * @param bool $recursive |
| 165 | */ |
| 166 | static function minify_html(&$root, $strip_comments = true, $recursive = true) { |
| 167 | if ($strip_comments) { |
| 168 | foreach($root->select(':comment', false, $recursive, true) as $c) { |
| 169 | $prev = $c->getSibling(-1); |
| 170 | $next = $c->getSibling(1); |
| 171 | $c->delete(); |
| 172 | if ($prev && $next && ($prev->isText()) && ($next->isText())) { |
| 173 | $prev->text .= $next->text; |
| 174 | $next->delete(); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | foreach($root->select('(!pre + !xmp + !style + !script + !"?php" + !"~text~" + !"~comment~"):not-empty > "~text~"', false, $recursive, true) as $c) { |
| 179 | $c->text = preg_replace('`\s+`', ' ', $c->text); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Minifies javascript using JSMin+ |
| 185 | * @param DomNode $root |
| 186 | * @param string $indent_string |
| 187 | * @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->) |
| 188 | * @param bool $recursive |
| 189 | * @return bool|array Array of errors on failure, true on succes |
| 190 | */ |
| 191 | static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true) { |
| 192 | #php4 JSMin+ doesn't support PHP4 |
| 193 | #return true; |
| 194 | #php4e |
| 195 | #php5 |
| 196 | include_once('third_party/jsminplus.php'); |
| 197 | |
| 198 | $errors = array(); |
| 199 | foreach($root->select('script:not-empty > "~text~"', false, $recursive, true) as $c) { |
| 200 | try { |
| 201 | $text = $c->text; |
| 202 | while ($text) { |
| 203 | $text = trim($text); |
| 204 | //Remove comment/CDATA tags at begin and end |
| 205 | if (substr($text, 0, 4) === '<!--') { |
| 206 | $text = substr($text, 5); |
| 207 | continue; |
| 208 | } elseif (strtolower(substr($text, 0, 9)) === '<![cdata[') { |
| 209 | $text = substr($text, 10); |
| 210 | continue; |
| 211 | } |
| 212 | |
| 213 | if (($end = substr($text, -3)) && (($end === '-->') || ($end === ']]>'))) { |
| 214 | $text = substr($text, 0, -3); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | if (trim($text)) { |
| 222 | $text = JSMinPlus::minify($text); |
| 223 | if ($wrap_comment) { |
| 224 | $text = "<!--\n".$text."\n//-->"; |
| 225 | } |
| 226 | if ($indent_string && ($wrap_comment || (strpos($text, "\n") !== false))) { |
| 227 | $text = indent_text("\n".$text, $c->indent(), $indent_string); |
| 228 | } |
| 229 | } |
| 230 | $c->text = $text; |
| 231 | } catch (\Exception $e) { |
| 232 | $errors[] = array($e, $c->parent->dumpLocation()); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return (($errors) ? $errors : true); |
| 237 | #php5e |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Formats HTML |
| 242 | * @param DomNode $root |
| 243 | * @param bool $recursive |
| 244 | * @access private |
| 245 | */ |
| 246 | function format_html(&$root, $recursive = null) { |
| 247 | if ($recursive === null) { |
| 248 | $recursive = true; |
| 249 | self::minify_html($root); |
| 250 | } elseif (is_int($recursive)) { |
| 251 | $recursive = (($recursive > 1) ? $recursive - 1 : false); |
| 252 | } |
| 253 | |
| 254 | $root_tag = strtolower($root->tag); |
| 255 | $in_block = isset($this->block_elements[$root_tag]) && $this->block_elements[$root_tag]['as_block']; |
| 256 | $child_count = count($root->children); |
| 257 | |
| 258 | if (isset($this->options['attributes_case']) && $this->options['attributes_case']) { |
| 259 | $root->attributes = array_change_key_case($root->attributes, $this->options['attributes_case']); |
| 260 | $root->attributes_ns = null; |
| 261 | } |
| 262 | |
| 263 | if (isset($this->options['sort_attributes']) && $this->options['sort_attributes']) { |
| 264 | if ($this->options['sort_attributes'] === 'reverse') { |
| 265 | krsort($root->attributes); |
| 266 | } else { |
| 267 | ksort($root->attributes); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if ($root->select(':element', true, false, true)) { |
| 272 | $root->setTag(strtolower($root->tag), true); |
| 273 | if (($this->options['img_alt'] !== null) && ($root_tag === 'img') && (!isset($root->alt))) { |
| 274 | $root->setAttribute('alt', $this->options['img_alt']); |
| 275 | } |
| 276 | } |
| 277 | if ($this->options['self_close_str'] !== null) { |
| 278 | $root->self_close_str = $this->options['self_close_str']; |
| 279 | } |
| 280 | if ($this->options['attribute_shorttag'] !== null) { |
| 281 | $root->attribute_shorttag = $this->options['attribute_shorttag']; |
| 282 | } |
| 283 | |
| 284 | $prev = null; |
| 285 | $n_tag = ''; |
| 286 | // $prev_tag = ''; |
| 287 | $as_block = false; |
| 288 | $prev_asblock = false; |
| 289 | for($i = 0; $i < $child_count; $i++) { |
| 290 | $n =& $root->children[$i]; |
| 291 | $indent = $n->indent(); |
| 292 | |
| 293 | if (!$n->isText()) { |
| 294 | $n_tag = strtolower($n->tag); |
| 295 | $new_line = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['new_line']; |
| 296 | $as_block = isset($this->block_elements[$n_tag]) && $this->block_elements[$n_tag]['as_block']; |
| 297 | $format_inside = ((!isset($this->block_elements[$n_tag])) || $this->block_elements[$n_tag]['format_inside']); |
| 298 | |
| 299 | if ($prev && ($prev->isText()) && $prev->text && ($char = $prev->text[strlen($prev->text) - 1]) && isset($this->whitespace[$char])) { |
| 300 | if ($this->whitespace[$char]) { |
| 301 | $prev->text .= str_repeat($this->indent_string, $indent); |
| 302 | } else { |
| 303 | $prev->text = substr_replace($prev->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1); |
| 304 | } |
| 305 | } elseif (($new_line || $prev_asblock || ($in_block && ($i === 0)))){ |
| 306 | if ($prev && ($prev->isText())) { |
| 307 | $prev->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent); |
| 308 | } else { |
| 309 | $root->addText($this->linebreak_string.str_repeat($this->indent_string, $indent), $i); |
| 310 | ++$child_count; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if ($format_inside && count($n->children)) { |
| 315 | //$last = end($n->children); |
| 316 | $last = $n->children[count($n->children) - 1]; |
| 317 | $last_tag = ($last) ? strtolower($last->tag) : ''; |
| 318 | $last_asblock = ($last_tag && isset($this->block_elements[$last_tag]) && $this->block_elements[$last_tag]['as_block']); |
| 319 | |
| 320 | if (($n->childCount(true) > 0) || (trim($n->getPlainText()))) { |
| 321 | if ($last && ($last->isText()) && $last->text && ($char = $last->text[strlen($last->text) - 1]) && isset($this->whitespace[$char])) { |
| 322 | if ($as_block || ($last->index() > 0) || isset($this->whitespace[$last->text[0]])) { |
| 323 | if ($this->whitespace[$char]) { |
| 324 | $last->text .= str_repeat($this->indent_string, $indent); |
| 325 | } else { |
| 326 | $last->text = substr_replace($last->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), -1, 1); |
| 327 | } |
| 328 | } |
| 329 | } elseif (($as_block || $last_asblock || ($in_block && ($i === 0))) && $last) { |
| 330 | if ($last && ($last->isText())) { |
| 331 | $last->text .= $this->linebreak_string.str_repeat($this->indent_string, $indent); |
| 332 | } else { |
| 333 | $n->addText($this->linebreak_string.str_repeat($this->indent_string, $indent)); |
| 334 | } |
| 335 | } |
| 336 | } elseif (!trim($n->getInnerText())) { |
| 337 | $n->clear(); |
| 338 | } |
| 339 | |
| 340 | if ($recursive) { |
| 341 | $this->format_html($n, $recursive); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | } elseif (trim($n->text) && ((($i - 1 < $child_count) && ($char = $n->text[0]) && isset($this->whitespace[$char])) || ($in_block && ($i === 0)))) { |
| 346 | if (isset($this->whitespace[$char])) { |
| 347 | if ($this->whitespace[$char]) { |
| 348 | $n->text = str_repeat($this->indent_string, $indent).$n->text; |
| 349 | } else { |
| 350 | $n->text = substr_replace($n->text, $this->linebreak_string.str_repeat($this->indent_string, $indent), 0, 1); |
| 351 | } |
| 352 | } else { |
| 353 | $n->text = $this->linebreak_string.str_repeat($this->indent_string, $indent).$n->text; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | $prev = $n; |
| 358 | // $prev_tag = $n_tag; |
| 359 | $prev_asblock = $as_block; |
| 360 | } |
| 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Formats HTML/Javascript |
| 367 | * @param DomNode $root |
| 368 | * @see format_html() |
| 369 | */ |
| 370 | function format(&$node) { |
| 371 | $this->errors = array(); |
| 372 | if ($this->options['minify_script']) { |
| 373 | $a = self::minify_javascript($node, $this->indent_string, true, true); |
| 374 | if (is_array($a)) { |
| 375 | foreach($a as $error) { |
| 376 | $this->errors[] = $error[0]->getMessage().' >>> '.$error[1]; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | return $this->format_html($node); |
| 381 | } |
| 382 | } |
| 383 |