You are here

protected function MinifyHTMLExit::minify in Minify Source HTML 8

Helper function to minify HTML.

1 call to MinifyHTMLExit::minify()
MinifyHTMLExit::response in src/EventSubscriber/MinifyHTMLExit.php
Minifies the HTML.

File

src/EventSubscriber/MinifyHTMLExit.php, line 143

Class

MinifyHTMLExit
Minifies the HTML of the response.

Namespace

Drupal\minifyhtml\EventSubscriber

Code

protected function minify() {
  $callbacks = [
    'minifyhtmlPlaceholderCallbackTextarea' => '/\\s*<textarea(\\b[^>]*?>[\\s\\S]*?<\\/textarea>)\\s*/i',
    'minifyhtmlPlaceholderCallbackPre' => '/\\s*<pre(\\b[^>]*?>[\\s\\S]*?<\\/pre>)\\s*/i',
    'minifyhtmlPlaceholderCallbackIframe' => '/\\s*<iframe(\\b[^>]*?>[\\s\\S]*?<\\/iframe>)\\s*/i',
    'minifyhtmlPlaceholderCallbackScript' => '/\\s*<script(\\b[^>]*?>[\\s\\S]*?<\\/script>)\\s*/i',
    'minifyhtmlPlaceholderCallbackStyle' => '/\\s*<style(\\b[^>]*?>[\\s\\S]*?<\\/style>)\\s*/i',
  ];

  // Only strip HTML comments if required.
  if ($this->config
    ->get('minifyhtml.config')
    ->get('strip_comments')) {
    $callbacks['minifyhtmlRemoveHtmlComment'] = '/<!--([\\s\\S]*?)-->/';
  }
  foreach ($callbacks as $callback => $pattern) {
    $content = $this
      ->minifyhtmlCallback($pattern, $callback);
    if (!is_null($content)) {
      $this->content = $content;
    }
  }

  // Minify the page.
  $this
    ->minifyHtml();

  // Restore all values that are currently represented by a placeholder.
  if (!empty($this->placeholders)) {
    $this->content = str_replace(array_keys($this->placeholders), array_values($this->placeholders), $this->content);
  }
}