/libraries/molajo/document/html/renderer/head.php

https://github.com/ot2sen/Molajo · PHP · 191 lines · 119 code · 21 blank · 51 comment · 23 complexity · d0676fb45374b67d66c244ddbc24f8f0 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Molajo
  4. * @subpackage Document
  5. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  6. * @copyright Copyright (C) 2011 Amy Stephen. All rights reserved.
  7. * @license GNU General Public License Version 2, or later http://www.gnu.org/licenses/gpl.html
  8. */
  9. defined('MOLAJO') or die;
  10. /**
  11. * MolajoDocument head renderer
  12. *
  13. * @package Molajo
  14. * @subpackage Document
  15. * @since 1.0
  16. */
  17. class MolajoDocumentRendererHead extends MolajoDocumentRenderer
  18. {
  19. /**
  20. * Renders the document head and returns the results as a string
  21. *
  22. * @param string $head (unused)
  23. * @param array $params Associative array of values
  24. * @param string $content The script
  25. *
  26. * @return string The output of the script
  27. *
  28. * @since 11.1
  29. *
  30. * @note Unused arguments are retained to preserve backward compatibility.
  31. */
  32. public function render($head, $params = array(), $content = null)
  33. {
  34. ob_start();
  35. echo $this->fetchHead($this->_doc);
  36. $buffer = ob_get_contents();
  37. ob_end_clean();
  38. return $buffer;
  39. }
  40. /**
  41. * Generates the head HTML and return the results as a string
  42. *
  43. * @param $document The document for which the head will be created
  44. *
  45. * @return string The head hTML
  46. *
  47. * @since 11.1
  48. */
  49. public function fetchHead(&$document)
  50. {
  51. // Trigger the onBeforeCompileHead event (skip for installation, since it causes an error)
  52. $app = MolajoFactory::getApplication();
  53. $app->triggerEvent('onBeforeCompileHead');
  54. // Get line endings
  55. $lnEnd = $document->_getLineEnd();
  56. $tab = $document->_getTab();
  57. $tagEnd = ' />';
  58. $buffer = '';
  59. // Generate base tag (need to happen first)
  60. $base = $document->getBase();
  61. if (!empty($base)) {
  62. $buffer .= $tab.'<base href="'.$document->getBase().'" />'.$lnEnd;
  63. }
  64. // Generate META tags (needs to happen as early as possible in the head)
  65. foreach ($document->_metaTags as $type => $tag)
  66. {
  67. foreach ($tag as $name => $content)
  68. {
  69. if ($type == 'http-equiv') {
  70. $content.= '; charset='.$document->getCharset();
  71. $buffer .= $tab.'<meta http-equiv="'.$name.'" content="'.htmlspecialchars($content).'"'.$tagEnd.$lnEnd;
  72. }
  73. else if ($type == 'standard' && !empty($content)) {
  74. $buffer .= $tab.'<meta name="'.$name.'" content="'.htmlspecialchars($content).'"'.$tagEnd.$lnEnd;
  75. }
  76. }
  77. }
  78. // Don't add empty descriptions
  79. $documentDescription = $document->getDescription();
  80. if ($documentDescription) {
  81. $buffer .= $tab.'<meta name="description" content="'.htmlspecialchars($documentDescription).'" />'.$lnEnd;
  82. }
  83. $buffer .= $tab.'<meta name="generator" content="'.htmlspecialchars($document->getGenerator()).'" />'.$lnEnd;
  84. $buffer .= $tab.'<title>'.htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8').'</title>'.$lnEnd;
  85. // Generate link declarations
  86. foreach ($document->_links as $link => $linkAtrr)
  87. {
  88. $buffer .= $tab.'<link href="'.$link.'" '.$linkAtrr['relType'].'="'.$linkAtrr['relation'].'"';
  89. if ($temp = JArrayHelper::toString($linkAtrr['attribs'])) {
  90. $buffer .= ' '.$temp;
  91. }
  92. $buffer .= ' />'.$lnEnd;
  93. }
  94. // Generate stylesheet links
  95. foreach ($document->_styleSheets as $strSrc => $strAttr)
  96. {
  97. $buffer .= $tab.'<link rel="stylesheet" href="'.$strSrc.'" type="'.$strAttr['mime'].'"';
  98. if (!is_null($strAttr['media'])) {
  99. $buffer .= ' media="'.$strAttr['media'].'" ';
  100. }
  101. if ($temp = JArrayHelper::toString($strAttr['attribs'])) {
  102. $buffer .= ' '.$temp;
  103. }
  104. $buffer .= $tagEnd.$lnEnd;
  105. }
  106. // Generate stylesheet declarations
  107. foreach ($document->_style as $type => $content)
  108. {
  109. $buffer .= $tab.'<style type="'.$type.'">'.$lnEnd;
  110. // This is for full XHTML support.
  111. if ($document->_mime != 'text/html') {
  112. $buffer .= $tab.$tab.'<![CDATA['.$lnEnd;
  113. }
  114. $buffer .= $content.$lnEnd;
  115. // See above note
  116. if ($document->_mime != 'text/html') {
  117. $buffer .= $tab.$tab.']]>'.$lnEnd;
  118. }
  119. $buffer .= $tab.'</style>'.$lnEnd;
  120. }
  121. // Generate script file links
  122. foreach ($document->_scripts as $strSrc => $strAttr) {
  123. $buffer .= $tab.'<script src="'.$strSrc.'"';
  124. if (!is_null($strAttr['mime'])) {
  125. $buffer .= ' type="'.$strAttr['mime'].'"';
  126. }
  127. if ($strAttr['defer']) {
  128. $buffer .= ' defer="defer"';
  129. }
  130. if ($strAttr['async']) {
  131. $buffer .= ' async="async"';
  132. }
  133. $buffer .= '></script>'.$lnEnd;
  134. }
  135. // Generate script declarations
  136. foreach ($document->_script as $type => $content)
  137. {
  138. $buffer .= $tab.'<script type="'.$type.'">'.$lnEnd;
  139. // This is for full XHTML support.
  140. if ($document->_mime != 'text/html') {
  141. $buffer .= $tab.$tab.'<![CDATA['.$lnEnd;
  142. }
  143. $buffer .= $content.$lnEnd;
  144. // See above note
  145. if ($document->_mime != 'text/html') {
  146. $buffer .= $tab.$tab.']]>'.$lnEnd;
  147. }
  148. $buffer .= $tab.'</script>'.$lnEnd;
  149. }
  150. // Generate script language declarations.
  151. if (count(MolajoText::script())) {
  152. $buffer .= $tab.'<script type="text/javascript">'.$lnEnd;
  153. $buffer .= $tab.$tab.'(function() {'.$lnEnd;
  154. $buffer .= $tab.$tab.$tab.'var strings = '.json_encode(MolajoText::script()).';'.$lnEnd;
  155. $buffer .= $tab.$tab.$tab.'if (typeof Joomla == \'undefined\') {'.$lnEnd;
  156. $buffer .= $tab.$tab.$tab.$tab.'Joomla = {};'.$lnEnd;
  157. $buffer .= $tab.$tab.$tab.$tab.'Joomla.MolajoText = strings;'.$lnEnd;
  158. $buffer .= $tab.$tab.$tab.'}'.$lnEnd;
  159. $buffer .= $tab.$tab.$tab.'else {'.$lnEnd;
  160. $buffer .= $tab.$tab.$tab.$tab.'Joomla.MolajoText.load(strings);'.$lnEnd;
  161. $buffer .= $tab.$tab.$tab.'}'.$lnEnd;
  162. $buffer .= $tab.$tab.'})();'.$lnEnd;
  163. $buffer .= $tab.'</script>'.$lnEnd;
  164. }
  165. foreach($document->_custom as $custom) {
  166. $buffer .= $tab.$custom.$lnEnd;
  167. }
  168. return $buffer;
  169. }
  170. }