library/smarty/libs/plugins/outputfilter.trimwhitespace.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsFilter
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty trimwhitespace outputfilter plugin
       
    11  * Trim unnecessary whitespace from HTML markup.
       
    12  *
       
    13  * @author   Rodney Rehm
       
    14  *
       
    15  * @param string $source input string
       
    16  *
       
    17  * @return string filtered output
       
    18  * @todo     substr_replace() is not overloaded by mbstring.func_overload - so this function might fail!
       
    19  */
       
    20 function smarty_outputfilter_trimwhitespace($source) {
       
    21     $store = array();
       
    22     $_store = 0;
       
    23     $_offset = 0;
       
    24 
       
    25     // Unify Line-Breaks to \n
       
    26     $source = preg_replace("/\015\012|\015|\012/", "\n", $source);
       
    27 
       
    28     // capture Internet Explorer Conditional Comments
       
    29     if (preg_match_all('#<!--\[[^\]]+\]>.*?<!\[[^\]]+\]-->#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
       
    30         foreach ($matches as $match) {
       
    31             $store[] = $match[0][0];
       
    32             $_length = strlen($match[0][0]);
       
    33             $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
       
    34             $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
       
    35 
       
    36             $_offset += $_length - strlen($replace);
       
    37             $_store++;
       
    38         }
       
    39     }
       
    40 
       
    41     // Strip all HTML-Comments
       
    42     // yes, even the ones in <script> - see http://stackoverflow.com/a/808850/515124
       
    43     $source = preg_replace('#<!--.*?-->#ms', '', $source);
       
    44 
       
    45     // capture html elements not to be messed with
       
    46     $_offset = 0;
       
    47     if (preg_match_all('#(<script[^>]*>.*?</script[^>]*>)|(<textarea[^>]*>.*?</textarea[^>]*>)|(<pre[^>]*>.*?</pre[^>]*>)#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
       
    48         foreach ($matches as $match) {
       
    49             $store[] = $match[0][0];
       
    50             $_length = strlen($match[0][0]);
       
    51             $replace = '@!@SMARTY:' . $_store . ':SMARTY@!@';
       
    52             $source = substr_replace($source, $replace, $match[0][1] - $_offset, $_length);
       
    53 
       
    54             $_offset += $_length - strlen($replace);
       
    55             $_store++;
       
    56         }
       
    57     }
       
    58 
       
    59     $expressions = array(
       
    60         // replace multiple spaces between tags by a single space
       
    61         // can't remove them entirely, becaue that might break poorly implemented CSS display:inline-block elements
       
    62         '#(:SMARTY@!@|>)\s+(?=@!@SMARTY:|<)#s' => '\1 \2',
       
    63         // remove spaces between attributes (but not in attribute values!)
       
    64         '#(([a-z0-9]\s*=\s*("[^"]*?")|(\'[^\']*?\'))|<[a-z0-9_]+)\s+([a-z/>])#is' => '\1 \5',
       
    65         // note: for some very weird reason trim() seems to remove spaces inside attributes.
       
    66         // maybe a \0 byte or something is interfering?
       
    67         '#^\s+<#Ss' => '<',
       
    68         '#>\s+$#Ss' => '>',
       
    69     );
       
    70 
       
    71     $source = preg_replace(array_keys($expressions), array_values($expressions), $source);
       
    72     // note: for some very weird reason trim() seems to remove spaces inside attributes.
       
    73     // maybe a \0 byte or something is interfering?
       
    74     // $source = trim( $source );
       
    75 
       
    76     $_offset = 0;
       
    77     if (preg_match_all('#@!@SMARTY:([0-9]+):SMARTY@!@#is', $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER)) {
       
    78         foreach ($matches as $match) {
       
    79             $_length = strlen($match[0][0]);
       
    80             $replace = $store[$match[1][0]];
       
    81             $source = substr_replace($source, $replace, $match[0][1] + $_offset, $_length);
       
    82 
       
    83             $_offset += strlen($replace) - $_length;
       
    84             $_store++;
       
    85         }
       
    86     }
       
    87 
       
    88     return $source;
       
    89 }