Created
May 22, 2015 13:47
-
-
Save ryaan-anthony/ba828d5a222953796c58 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0"?> | |
| <config> | |
| <modules> | |
| <Jemoon_Htmlminify> | |
| <version>0.1.0</version> | |
| </Jemoon_Htmlminify> | |
| </modules> | |
| <frontend> | |
| <events> | |
| <controller_action_postdispatch> | |
| <observers> | |
| <Jemoon_Htmlminify_Model_Observer> | |
| <type>singleton</type> | |
| <class>Jemoon_Htmlminify_Model_Observer</class> | |
| <method>HtmlMinify</method> | |
| </Jemoon_Htmlminify_Model_Observer> | |
| </observers> | |
| </controller_action_postdispatch> | |
| </events> | |
| </frontend> | |
| </config> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Jemoon_Htmlminify_Model_Observer { | |
| public function Htmlminify($observer) { | |
| // Fetches the current event | |
| $event = $observer->getEvent(); | |
| $controller = $event->getControllerAction(); | |
| $allHtml = $controller->getResponse()->getBody(); | |
| if(false){ | |
| // Trim each line | |
| $allHtml = preg_replace('/^\\s+|\\s+$/m', '', $allHtml); | |
| // Remove HTML comments | |
| /*$allHtml = preg_replace_callback( | |
| '/<!--([\\s\\S]*?)-->/', | |
| array($this, '_commentCB'), | |
| $allHtml); */ | |
| // Remove ws around block/undisplayed elements | |
| $allHtml = preg_replace('/\\s+(<\\/?(?:area|base(?:font)?|blockquote|body' | |
| .'|caption|center|cite|col(?:group)?|dd|dir|div|dl|dt|fieldset|form' | |
| .'|frame(?:set)?|h[1-6]|head|hr|html|legend|li|link|map|menu|meta' | |
| .'|ol|opt(?:group|ion)|p|param|t(?:able|body|head|d|h||r|foot|itle)' | |
| .'|ul)\\b[^>]*>)/i', '$1', $allHtml); | |
| // Remove ws outside of all elements | |
| $allHtml = preg_replace_callback( | |
| '/>([^<]+)</', | |
| array($this, '_outsideTagCB'), | |
| $allHtml); | |
| } | |
| $controller->getResponse()->setBody($allHtml); | |
| } | |
| protected function _outsideTagCB($m) | |
| { | |
| return '>' . preg_replace('/^\\s+|\\s+$/', ' ', $m[1]) . '<'; | |
| } | |
| protected function _commentCB($m) | |
| { | |
| return (0 === strpos($m[1], '[') || false !== strpos($m[1], '<![')) | |
| ? $m[0] | |
| : ''; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment