HTML compression #737
manojLondhe
started this conversation in
Snippets
Replies: 1 comment
-
I am using a little event subscriber that can minify html. It makes use of:
Because of these dependencies I haven't put it into the core yet. I prefer to keep the Pages core dependency free. Here is the code. class ExtPagesEventSubscriberMinify extends ComPagesEventSubscriberAbstract
{
protected function _initialize(KObjectConfig $config)
{
$config->append(array(
'priority' => KEvent::PRIORITY_LOWEST,
'enabled' => JDEBUG ? false : true,
));
parent::_initialize($config);
}
public function onBeforeDispatcherSend(KEventInterface $event)
{
$content = $event->getTarget()->getResponse()->getContent();
if($event->getTarget()->getRequest()->getFormat() == 'html')
{
//Filter our multiple spaces and html commments
$htmlMin = new voku\helper\HtmlMin();
$htmlMin->doOptimizeViaHtmlDomParser();
$htmlMin->doRemoveComments();
$htmlMin->doSumUpWhitespace();
$htmlMin->doRemoveWhitespaceAroundTags();
$htmlMin->doSortHtmlAttributes(false);
$content = $htmlMin->minify($content);
//Minify inline styles
if(preg_match_all('#<style(.*)>(.*)</style>#siU', $content, $matches))
{
foreach($matches[2] as $key => $match)
{
$minifier = new MatthiasMullie\Minify\CSS();
$minifier->add($match);
$content = str_replace($matches[2][$key], $minifier->minify(), $content);
}
}
//Minify inline scripts
if(preg_match_all('#<script(.*)>(.*)</script>#siU', $content, $matches))
{
$minifier = new MatthiasMullie\Minify\JS();
foreach($matches[2] as $key => $match)
{
$minifier = new MatthiasMullie\Minify\JS();
$minifier->add($match);
$content = str_replace($matches[2][$key], $minifier->minify(), $content);
}
}
$event->getTarget()->getResponse()->setContent($content);
}
}
public function isEnabled()
{
$result = false;
if(class_exists('voku\helper\HtmlMin') && class_exists('MatthiasMullie\Minify\CSS') ) {
$result = parent::isEnabled();
}
return $result;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Is there a way to compress HTML o/p using some pages config?
Beta Was this translation helpful? Give feedback.
All reactions