Issues (994)

src/shim/minifier.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Minify HTML
5
 *
6
 * @param string $ori
7
 * @return string
8
 */
9
function htmlmin(string $ori)
10
{
11
  //return preg_replace('/<!--.*?-->|\s+/s', ' ', $ori);
12
  $no_tab = preg_replace('/\n|\t|\n+|\t+/m', '', $ori);
13
  $no_comments = preg_replace('/<!--.*?-->/m', '', $no_tab);
14
  $between_tags = preg_replace('/\>\s+\</', '><', $no_comments);
15
16
  return $between_tags;
17
}
18
/**
19
 * Minify inline css from buffer
20
 *
21
 * @param string $css
22
 * @return string
23
 */
24
function mincss($css)
25
{
26
  $css = preg_replace('/\/\*[^*]*\*+([^\/][^*]*\*+)*\//m', '', $css); // remove comments in beautify css
27
  $css = preg_replace('/\/\*((?!\*\/).)*\*\//', '', $css); // negative look ahead
28
  $css = preg_replace('/\s{2,}/', ' ', $css);
29
  $css = preg_replace('/\s*([:;{}])\s*/', '$1', $css);
30
  $css = preg_replace('/;}/', '}', $css);
31
  // clean whitespaces
32
  /*$css = preg_replace(
33
  "/(\t|\n|\v|\f|\r| |\xC2\x85|\xc2\xa0|\xe1\xa0\x8e|\xe2\x80[\x80-\x8D]|\xe2\x80\xa8|\xe2\x80\xa9|\xe2\x80\xaF|\xe2\x81\x9f|\xe2\x81\xa0|\xe3\x80\x80|\xef\xbb\xbf)+/",
34
  "",
35
  $css
36
  );*/
37
  $css = preg_replace('/\s+/m', ' ', $css);
38
39
  return $css;
40
}
41
42
// pretext filter and syntax highlighter
43
$syntaxHighlighter = null;
44
/**
45
 * Fix <pre/> syntax highlight
46
 *
47
 * @param string $c
48
 * @return string
49
 */
50
function prefilter(string $c)
51
{
52
  global $syntaxHighlighter;
53
  $dom = new SmartDOMDocument('1.0', 'ISO-8859-15');
54
  $dom->preserveWhiteSpace = true;
55
  $dom->formatOutput = false;
56
57
  if (!$dom->loadHTML($c)) {
58
    \HTML\js::var('HTML_ERROR', json_encode(['error' => true, 'message' => 'HTML Optimizer failed']));
59
    echo $c;
60
61
    return;
62
  }
63
64
  $xpath = new SmartDOMXpath($dom);
65
66
  $title = getTitle($dom);
67
  $pres = $xpath->query('//pre');
0 ignored issues
show
The call to SmartDOMXpath::query() has too few arguments starting with contextnode. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
  /** @scrutinizer ignore-call */ 
68
  $pres = $xpath->query('//pre');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
68
  if (!empty($pres)) {
69
    foreach ($pres as $pre) {
70
      if (is_json($pre->innerHTML)) {
71
        $pre->innerHTML = \JSON\json::beautify(json_decode($pre->innerHTML));
72
        if (!$syntaxHighlighter) {
73
          $syntaxHighlighter = true;
74
          insertBodyFirst($xpath, createScript($dom, ['src' => '/assets/highlight.js/loader.js', 'cache' => 'true']));
75
        }
76
        if (empty(trim($pre->getAttribute('class')))) {
77
          $pre->setAttribute('class', 'json');
78
        }
79
      }
80
      if (empty(trim($pre->getAttribute('title')))) {
81
        $pre->setAttribute('title', $title);
82
      }
83
    }
84
  }
85
86
  $textareas = $xpath->query('//textarea');
87
  if (!empty($textareas)) {
88
    foreach ($textareas as $area) {
89
      if ($area->hasAttribute('innerhtml')) {
90
        $area->innerHTML = base64_decode($area->getAttribute('innerhtml'));
91
        $area->removeAttribute('innerhtml');
92
      }
93
    }
94
  }
95
96
  return $dom->saveHTML();
97
}
98