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

https://github.com/dextercowley/joomla-cms · PHP · 243 lines · 155 code · 34 blank · 54 comment · 33 complexity · 28227ef153cb46f74ef8caa047a88c72 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Document
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * JDocument head renderer
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Document
  15. * @since 11.1
  16. */
  17. class JDocumentRendererHead extends JDocumentRenderer
  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 JDocument $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. // Convert the tagids to titles
  52. if (isset($document->_metaTags['standard']['tags']))
  53. {
  54. $tagsHelper = new JHelperTags;
  55. $document->_metaTags['standard']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['standard']['tags']));
  56. }
  57. // Trigger the onBeforeCompileHead event
  58. $app = JFactory::getApplication();
  59. $app->triggerEvent('onBeforeCompileHead');
  60. // Get line endings
  61. $lnEnd = $document->_getLineEnd();
  62. $tab = $document->_getTab();
  63. $tagEnd = ' />';
  64. $buffer = '';
  65. // Generate charset when using HTML5 (should happen first)
  66. if ($document->isHtml5())
  67. {
  68. $buffer .= $tab . '<meta charset="' . $document->getCharset() . '" />' . $lnEnd;
  69. }
  70. // Generate base tag (need to happen early)
  71. $base = $document->getBase();
  72. if (!empty($base))
  73. {
  74. $buffer .= $tab . '<base href="' . $document->getBase() . '" />' . $lnEnd;
  75. }
  76. // Generate META tags (needs to happen as early as possible in the head)
  77. foreach ($document->_metaTags as $type => $tag)
  78. {
  79. foreach ($tag as $name => $content)
  80. {
  81. if ($type == 'http-equiv' && !($document->isHtml5() && $name == 'content-type'))
  82. {
  83. $buffer .= $tab . '<meta http-equiv="' . $name . '" content="' . htmlspecialchars($content) . '" />' . $lnEnd;
  84. }
  85. elseif ($type == 'standard' && !empty($content))
  86. {
  87. $buffer .= $tab . '<meta name="' . $name . '" content="' . htmlspecialchars($content) . '" />' . $lnEnd;
  88. }
  89. }
  90. }
  91. // Don't add empty descriptions
  92. $documentDescription = $document->getDescription();
  93. if ($documentDescription)
  94. {
  95. $buffer .= $tab . '<meta name="description" content="' . htmlspecialchars($documentDescription) . '" />' . $lnEnd;
  96. }
  97. // Don't add empty generators
  98. $generator = $document->getGenerator();
  99. if ($generator)
  100. {
  101. $buffer .= $tab . '<meta name="generator" content="' . htmlspecialchars($generator) . '" />' . $lnEnd;
  102. }
  103. $buffer .= $tab . '<title>' . htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8') . '</title>' . $lnEnd;
  104. // Generate link declarations
  105. foreach ($document->_links as $link => $linkAtrr)
  106. {
  107. $buffer .= $tab . '<link href="' . $link . '" ' . $linkAtrr['relType'] . '="' . $linkAtrr['relation'] . '"';
  108. if ($temp = JArrayHelper::toString($linkAtrr['attribs']))
  109. {
  110. $buffer .= ' ' . $temp;
  111. }
  112. $buffer .= ' />' . $lnEnd;
  113. }
  114. // Generate stylesheet links
  115. foreach ($document->_styleSheets as $strSrc => $strAttr)
  116. {
  117. $buffer .= $tab . '<link rel="stylesheet" href="' . $strSrc . '"';
  118. if (!is_null($strAttr['mime']) && (!$document->isHtml5() || $strAttr['mime'] != 'text/css'))
  119. {
  120. $buffer .= ' type="' . $strAttr['mime'] . '"';
  121. }
  122. if (!is_null($strAttr['media']))
  123. {
  124. $buffer .= ' media="' . $strAttr['media'] . '"';
  125. }
  126. if ($temp = JArrayHelper::toString($strAttr['attribs']))
  127. {
  128. $buffer .= ' ' . $temp;
  129. }
  130. $buffer .= $tagEnd . $lnEnd;
  131. }
  132. // Generate stylesheet declarations
  133. foreach ($document->_style as $type => $content)
  134. {
  135. $buffer .= $tab . '<style type="' . $type . '">' . $lnEnd;
  136. // This is for full XHTML support.
  137. if ($document->_mime != 'text/html')
  138. {
  139. $buffer .= $tab . $tab . '<![CDATA[' . $lnEnd;
  140. }
  141. $buffer .= $content . $lnEnd;
  142. // See above note
  143. if ($document->_mime != 'text/html')
  144. {
  145. $buffer .= $tab . $tab . ']]>' . $lnEnd;
  146. }
  147. $buffer .= $tab . '</style>' . $lnEnd;
  148. }
  149. // Generate script file links
  150. foreach ($document->_scripts as $strSrc => $strAttr)
  151. {
  152. $buffer .= $tab . '<script src="' . $strSrc . '"';
  153. $defaultMimes = array(
  154. 'text/javascript', 'application/javascript', 'text/x-javascript', 'application/x-javascript'
  155. );
  156. if (!is_null($strAttr['mime']) && (!$document->isHtml5() || !in_array($strAttr['mime'], $defaultMimes)))
  157. {
  158. $buffer .= ' type="' . $strAttr['mime'] . '"';
  159. }
  160. if ($strAttr['defer'])
  161. {
  162. $buffer .= ' defer="defer"';
  163. }
  164. if ($strAttr['async'])
  165. {
  166. $buffer .= ' async="async"';
  167. }
  168. $buffer .= '></script>' . $lnEnd;
  169. }
  170. // Generate script declarations
  171. foreach ($document->_script as $type => $content)
  172. {
  173. $buffer .= $tab . '<script type="' . $type . '">' . $lnEnd;
  174. // This is for full XHTML support.
  175. if ($document->_mime != 'text/html')
  176. {
  177. $buffer .= $tab . $tab . '<![CDATA[' . $lnEnd;
  178. }
  179. $buffer .= $content . $lnEnd;
  180. // See above note
  181. if ($document->_mime != 'text/html')
  182. {
  183. $buffer .= $tab . $tab . ']]>' . $lnEnd;
  184. }
  185. $buffer .= $tab . '</script>' . $lnEnd;
  186. }
  187. // Generate script language declarations.
  188. if (count(JText::script()))
  189. {
  190. $buffer .= $tab . '<script type="text/javascript">' . $lnEnd;
  191. $buffer .= $tab . $tab . '(function() {' . $lnEnd;
  192. $buffer .= $tab . $tab . $tab . 'var strings = ' . json_encode(JText::script()) . ';' . $lnEnd;
  193. $buffer .= $tab . $tab . $tab . 'if (typeof Joomla == \'undefined\') {' . $lnEnd;
  194. $buffer .= $tab . $tab . $tab . $tab . 'Joomla = {};' . $lnEnd;
  195. $buffer .= $tab . $tab . $tab . $tab . 'Joomla.JText = strings;' . $lnEnd;
  196. $buffer .= $tab . $tab . $tab . '}' . $lnEnd;
  197. $buffer .= $tab . $tab . $tab . 'else {' . $lnEnd;
  198. $buffer .= $tab . $tab . $tab . $tab . 'Joomla.JText.load(strings);' . $lnEnd;
  199. $buffer .= $tab . $tab . $tab . '}' . $lnEnd;
  200. $buffer .= $tab . $tab . '})();' . $lnEnd;
  201. $buffer .= $tab . '</script>' . $lnEnd;
  202. }
  203. foreach ($document->_custom as $custom)
  204. {
  205. $buffer .= $tab . $custom . $lnEnd;
  206. }
  207. return $buffer;
  208. }
  209. }