File modules/util/ui/minifier/CSSMinifier.class.php

Last commit: Sun Oct 13 13:17:32 2024 +0200	Jan Dankert	New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.
1 <?php 2 3 namespace util\ui\minifier; 4 5 class CSSMinifier extends AbstractMinifier 6 { 7 const FLAG_REMOVE_MULTILINE_COMMENT = 2; 8 const FLAG_REMOVE_BLANK_LINES = 4; 9 const FLAG_REMOVE_LINE_COMMENTS = 8; 10 const FLAG_REMOVE_WHITESPACE = 16; 11 const FLAG_REMOVE_LINEBREAK = 32; 12 const FLAG_REMOVE_TABS = 64; 13 const FLAG_REMOVE_SUPERFLOUS_SPACES = 128; 14 15 const REPLACER = [ 16 self::FLAG_REMOVE_MULTILINE_COMMENT => ["/\/\*[\s\S]*?\*\//" , '' ], 17 self::FLAG_REMOVE_LINE_COMMENTS => ["/^(.*)\/\/.*$/m" ,'${1}' ], 18 self::FLAG_REMOVE_BLANK_LINES => ['/^\n+|^[\t\s]*\n+/m' , '' ], 19 self::FLAG_REMOVE_WHITESPACE => ['/^\s*(.*)\s*$/m' ,'${1}' ], 20 self::FLAG_REMOVE_LINEBREAK => ['/\n/' ,'' ], 21 self::FLAG_REMOVE_TABS => ['/\t/' ,'' ], 22 self::FLAG_REMOVE_SUPERFLOUS_SPACES => ['/\s{2,}7' ,' ' ], 23 ]; 24 25 26 public function linkToSourcemap($sourcemap) 27 { 28 return "/*# sourceMappingURL=$sourcemap */"; 29 } 30 31 32 protected function compress($chars) 33 { 34 $sourceLine = 1; 35 $sourcePos = 1; 36 $buffer = ''; 37 while( true ) { 38 if ( empty( $chars ) ) 39 break; 40 41 $char = array_shift( $chars ); 42 43 if ($char == '/') { 44 $nextChar = array_shift( $chars ); 45 if ( $nextChar == '/' && $this->hasFlag( self::FLAG_REMOVE_LINE_COMMENTS )) { 46 // single-line comment 47 while(true) { 48 $nextChar = array_shift( $chars ); 49 if ( $nextChar == "\n" ) { 50 $sourceLine++; 51 break; 52 } 53 } 54 } 55 elseif ( $nextChar == '*' && $this->hasFlag( self::FLAG_REMOVE_MULTILINE_COMMENT)) { 56 // multi-line comment 57 while(true) { 58 $nextChar = array_shift( $chars ); 59 if ( $nextChar == "\n" ) { 60 if ( ! $this->hasFlag( self::FLAG_REMOVE_LINEBREAK) ) 61 $buffer .= $nextChar; 62 } 63 if ( $nextChar == "*" ) { 64 $nextChar = array_shift( $chars ); 65 if ( $nextChar == "/" ) 66 break; 67 else 68 // it's not the end of the multiline comment. 69 array_unshift($chars,$nextChar); 70 } 71 } 72 } 73 else { 74 array_unshift($chars,$nextChar); 75 $buffer .= $char; 76 } 77 } 78 elseif ($char == "\t" && $this->hasFlag(self::FLAG_REMOVE_TABS)) { 79 ; 80 } 81 elseif ($char == "\r" && $this->hasFlag( self::FLAG_REMOVE_LINEBREAK)) { 82 ; 83 } 84 elseif ($char == "\n" && $this->hasFlag( self::FLAG_REMOVE_LINEBREAK)) { 85 ; 86 } 87 elseif ($char == " " && substr($buffer,-1,1)==' ' && $this->hasFlag(self::FLAG_REMOVE_SUPERFLOUS_SPACES)) { 88 ; 89 } else { 90 $buffer .= $char; 91 } 92 } 93 94 if ( $this->hasFlag(self::FLAG_REMOVE_LINE_COMMENTS )) 95 $buffer = preg_replace('/^(@import\s+\S+)\.less/m','${1}.min.css',$buffer); 96 97 $this->addCompressedContent($buffer); 98 } 99 }
Download modules/util/ui/minifier/CSSMinifier.class.php
History Sun, 13 Oct 2024 13:17:32 +0200 Jan Dankert New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.