PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/nooku/libraries/koowa/template/filter/script.php

https://github.com/bhar1red/anahita
PHP | 117 lines | 54 code | 14 blank | 49 comment | 4 complexity | bdb271e1a5ab25c0705893c43062673b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: script.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4. * @package Koowa_Template
  5. * @subpackage Filter
  6. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. * @link http://www.nooku.org
  9. */
  10. /**
  11. * Template filter to parse script tags
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @package Koowa_Template
  15. * @subpackage Filter
  16. */
  17. class KTemplateFilterScript extends KTemplateFilterAbstract implements KTemplateFilterWrite
  18. {
  19. /**
  20. * Initializes the options for the object
  21. *
  22. * Called from {@link __construct()} as a first step of object instantiation.
  23. *
  24. * @param object An optional KConfig object with configuration options
  25. * @return void
  26. */
  27. protected function _initialize(KConfig $config)
  28. {
  29. $config->append(array(
  30. 'priority' => KCommand::PRIORITY_LOW,
  31. ));
  32. parent::_initialize($config);
  33. }
  34. /**
  35. * Find any <script src="" /> or <script></script> elements and render them
  36. *
  37. * <script inline></script> can be used for inline scripts
  38. *
  39. * @param string Block of text to parse
  40. * @return KTemplateFilterLink
  41. */
  42. public function write(&$text)
  43. {
  44. //Parse the script information
  45. $scripts = $this->_parseScripts($text);
  46. //Prepend the script information
  47. $text = $scripts.$text;
  48. return $this;
  49. }
  50. /**
  51. * Parse the text for script tags
  52. *
  53. * @param string Block of text to parse
  54. * @return string
  55. */
  56. protected function _parseScripts(&$text)
  57. {
  58. $scripts = '';
  59. $matches = array();
  60. // <script src="" />
  61. if(preg_match_all('#<script(?!\s+data\-inline\s*)\s+src="([^"]+)"(.*)/>#siU', $text, $matches))
  62. {
  63. foreach(array_unique($matches[1]) as $key => $match)
  64. {
  65. $attribs = $this->_parseAttributes( $matches[2][$key]);
  66. $scripts .= $this->_renderScript($match, true, $attribs);
  67. }
  68. $text = str_replace($matches[0], '', $text);
  69. }
  70. $matches = array();
  71. // <script></script>
  72. if(preg_match_all('#<script(?!\s+data\-inline\s*)(.*)>(.*)</script>#siU', $text, $matches))
  73. {
  74. foreach($matches[2] as $key => $match)
  75. {
  76. $attribs = $this->_parseAttributes( $matches[1][$key]);
  77. $scripts .= $this->_renderScript($match, false, $attribs);
  78. }
  79. $text = str_replace($matches[0], '', $text);
  80. }
  81. return $scripts;
  82. }
  83. /**
  84. * Render script information
  85. *
  86. * @param string The script information
  87. * @param boolean True, if the script information is a URL.
  88. * @param array Associative array of attributes
  89. * @return string
  90. */
  91. protected function _renderScript($script, $link, $attribs = array())
  92. {
  93. $attribs = KHelperArray::toString($attribs);
  94. if(!$link)
  95. {
  96. $html = '<script type="text/javascript" '.$attribs.'>'."\n";
  97. $html .= trim($script);
  98. $html .= '</script>'."\n";
  99. }
  100. else $html = '<script type="text/javascript" src="'.$script.'" '.$attribs.'></script>'."\n";
  101. return $html;
  102. }
  103. }