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

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

https://github.com/bhar1red/anahita
PHP | 113 lines | 54 code | 14 blank | 45 comment | 4 complexity | c91aaf7332cf7563e92e91b1a5b86c70 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: style.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 style tags
  12. *
  13. * @author Johan Janssens <johan@nooku.org>
  14. * @package Koowa_Template
  15. * @subpackage Filter
  16. */
  17. class KTemplateFilterStyle 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 <style src"" /> or <style></style> elements and render them
  36. *
  37. * @param string Block of text to parse
  38. * @return KTemplateFilterStyle
  39. */
  40. public function write(&$text)
  41. {
  42. //Parse the script information
  43. $styles = $this->_parseStyles($text);
  44. //Prepend the script information
  45. $text = $styles.$text;
  46. return $this;
  47. }
  48. /**
  49. * Parse the text for style tags
  50. *
  51. * @param string Block of text to parse
  52. * @return string
  53. */
  54. protected function _parseStyles(&$text)
  55. {
  56. $styles = '';
  57. $matches = array();
  58. if(preg_match_all('#<style\s*src="([^"]+)"(.*)\/>#iU', $text, $matches))
  59. {
  60. foreach(array_unique($matches[1]) as $key => $match)
  61. {
  62. $attribs = $this->_parseAttributes( $matches[2][$key]);
  63. $styles .= $this->_renderStyle($match, true, $attribs);
  64. }
  65. $text = str_replace($matches[0], '', $text);
  66. }
  67. $matches = array();
  68. if(preg_match_all('#<style(.*)>(.*)<\/style>#siU', $text, $matches))
  69. {
  70. foreach($matches[2] as $key => $match)
  71. {
  72. $attribs = $this->_parseAttributes( $matches[1][$key]);
  73. $styles .= $this->_renderStyle($match, false, $attribs);
  74. }
  75. $text = str_replace($matches[0], '', $text);
  76. }
  77. return $styles;
  78. }
  79. /**
  80. * Render style information
  81. *
  82. * @param string The style information
  83. * @param boolean True, if the style information is a URL
  84. * @param array Associative array of attributes
  85. * @return string
  86. */
  87. protected function _renderStyle($style, $link, $attribs = array())
  88. {
  89. $attribs = KHelperArray::toString($attribs);
  90. if(!$link)
  91. {
  92. $html = '<style type="text/css" '.$attribs.'>'."\n";
  93. $html .= trim($style['data']);
  94. $html .= '</style>'."\n";
  95. }
  96. else $html = '<link type="text/css" rel="stylesheet" href="'.$style.'" '.$attribs.' />'."\n";
  97. return $html;
  98. }
  99. }