/** * Compress the javascript blocks */ public function onCompress(CompressionEvent $event) { if (!$event->isSafeToContinue()) { return; } foreach ($this->blocks as $k => $content) { // Extract the script code if (preg_match($this->getPattern(), $content, $matches) !== 1) { continue; } // Can't call compressor if not js code block if (!$this->isJavascript($matches[1])) { continue; } // Check if CDATA attribute is present $cdataWrapper = false; $script = $matches[2]; if (preg_match('#\\s*<!\\[CDATA\\[(?:\\s*\\*/)(.*?)(?:/\\*\\s*)\\]\\]>\\s*#usi', $script, $cdataMatches)) { $script = $cdataMatches[1]; $cdataWrapper = true; } // Call the inline compressor if ($script = trim($script)) { $script = $this->compressor->compress($script); } if ($cdataWrapper) { // Rewrap the compressed script into CDATA tag $script = "/*<![CDATA[*/" . $script . "/*]]>*/"; } // Replace the block into the saved array $this->blocks[$k] = $matches[1] . $script . $matches[3]; } }
/** * Compress the css blocks */ public function onCompress(CompressionEvent $event) { if (!$event->isSafeToContinue()) { return; } foreach ($this->blocks as $k => $content) { // Extract the script code if (preg_match($this->getPattern(), $content, $matches) !== 1) { continue; } // Can't call compressor if not css code block if (!$this->isCss($matches[1])) { continue; } // Call the inline compressor $style = $this->compressor->compress($matches[2]); // Replace the block into the saved array $this->blocks[$k] = $matches[1] . $style . $matches[3]; } }