PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/code/web/private_php/ams/smarty/libs/plugins/outputfilter.trimwhitespace.php

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