HeadRenderer.php
11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Document\Renderer\Html;
defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Document\DocumentRenderer;
use Joomla\CMS\Helper\TagsHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Utilities\ArrayHelper;
/**
* HTML document renderer for the document `<head>` element
*
* @since 3.5
*/
class HeadRenderer extends DocumentRenderer
{
/**
* Renders the document head and returns the results as a string
*
* @param string $head (unused)
* @param array $params Associative array of values
* @param string $content The script
*
* @return string The output of the script
*
* @since 3.5
*/
public function render($head, $params = array(), $content = null)
{
return $this->fetchHead($this->_doc);
}
/**
* Generates the head HTML and return the results as a string
*
* @param JDocumentHtml $document The document for which the head will be created
*
* @return string The head hTML
*
* @since 3.5
* @deprecated 4.0 Method code will be moved into the render method
*/
public function fetchHead($document)
{
// Convert the tagids to titles
if (isset($document->_metaTags['name']['tags']))
{
$tagsHelper = new TagsHelper;
$document->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['name']['tags']));
}
if ($document->getScriptOptions())
{
\JHtml::_('behavior.core');
}
// Trigger the onBeforeCompileHead event
$app = \JFactory::getApplication();
$app->triggerEvent('onBeforeCompileHead');
// Get line endings
$lnEnd = $document->_getLineEnd();
$tab = $document->_getTab();
$tagEnd = ' />';
$buffer = '';
$mediaVersion = $document->getMediaVersion();
// Generate charset when using HTML5 (should happen first)
if ($document->isHtml5())
{
$buffer .= $tab . '<meta charset="' . $document->getCharset() . '" />' . $lnEnd;
}
// Generate base tag (need to happen early)
$base = $document->getBase();
#if (!empty($base))
#{
# $buffer .= $tab . '<base href="' . $base . '" />' . $lnEnd;
#}
# $buffer .= $tab . '<base href="https://www.nifs-k.ac.jp/" />' . $lnEnd;
// Generate META tags (needs to happen as early as possible in the head)
foreach ($document->_metaTags as $type => $tag)
{
foreach ($tag as $name => $content)
{
if ($type == 'http-equiv' && !($document->isHtml5() && $name == 'content-type'))
{
$buffer .= $tab . '<meta http-equiv="' . $name . '" content="' . htmlspecialchars($content, ENT_COMPAT, 'UTF-8') . '" />' . $lnEnd;
}
elseif ($type != 'http-equiv' && !empty($content))
{
if (is_array($content))
{
foreach ($content as $value)
{
$buffer .= $tab . '<meta ' . $type . '="' . $name . '" content="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '" />' . $lnEnd;
}
}
else
{
$buffer .= $tab . '<meta ' . $type . '="' . $name . '" content="' . htmlspecialchars($content, ENT_COMPAT, 'UTF-8') . '" />' . $lnEnd;
}
}
}
}
// Don't add empty descriptions
$documentDescription = $document->getDescription();
if ($documentDescription)
{
$buffer .= $tab . '<meta name="description" content="' . htmlspecialchars($documentDescription, ENT_COMPAT, 'UTF-8') . '" />' . $lnEnd;
}
// Don't add empty generators
$generator = $document->getGenerator();
if ($generator)
{
$buffer .= $tab . '<meta name="generator" content="' . htmlspecialchars($generator, ENT_COMPAT, 'UTF-8') . '" />' . $lnEnd;
}
if(htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8') == "ホーム") { // アイハントカスタマイズ
$buffer .= $tab . '<title>国立大学法人 鹿屋体育大学 スポーツで未来を拓く自分を創る</title>' . $lnEnd;
}elseif(htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8') == "HOME") {
$buffer .= $tab . '<title>NIFS : National Institute of Fitness and Sports in KANOYA</title>' . $lnEnd;
}else{
$buffer .= $tab . '<title>' . htmlspecialchars($document->getTitle(), ENT_COMPAT, 'UTF-8') . '|鹿屋体育大学</title>' . $lnEnd;
}
// Generate link declarations
foreach ($document->_links as $link => $linkAtrr)
{
$buffer .= $tab . '<link href="' . $link . '" ' . $linkAtrr['relType'] . '="' . $linkAtrr['relation'] . '"';
if (is_array($linkAtrr['attribs']))
{
if ($temp = ArrayHelper::toString($linkAtrr['attribs']))
{
$buffer .= ' ' . $temp;
}
}
$buffer .= ' />' . $lnEnd;
}
$defaultCssMimes = array('text/css');
// Generate stylesheet links
foreach ($document->_styleSheets as $src => $attribs)
{
// Check if stylesheet uses IE conditional statements.
$conditional = isset($attribs['options']) && isset($attribs['options']['conditional']) ? $attribs['options']['conditional'] : null;
// Check if script uses media version.
if (isset($attribs['options']['version']) && $attribs['options']['version'] && strpos($src, '?') === false
&& ($mediaVersion || $attribs['options']['version'] !== 'auto'))
{
$src .= '?' . ($attribs['options']['version'] === 'auto' ? $mediaVersion : $attribs['options']['version']);
}
$buffer .= $tab;
// This is for IE conditional statements support.
if (!is_null($conditional))
{
$buffer .= '<!--[if ' . $conditional . ']>';
}
$buffer .= '<link href="' . $src . '" rel="stylesheet"';
// Add script tag attributes.
foreach ($attribs as $attrib => $value)
{
// Don't add the 'options' attribute. This attribute is for internal use (version, conditional, etc).
if ($attrib === 'options')
{
continue;
}
// Don't add type attribute if document is HTML5 and it's a default mime type. 'mime' is for B/C.
if (in_array($attrib, array('type', 'mime')) && $document->isHtml5() && in_array($value, $defaultCssMimes))
{
continue;
}
// Don't add type attribute if document is HTML5 and it's a default mime type. 'mime' is for B/C.
if ($attrib === 'mime')
{
$attrib = 'type';
}
// Add attribute to script tag output.
$buffer .= ' ' . htmlspecialchars($attrib, ENT_COMPAT, 'UTF-8');
// Json encode value if it's an array.
$value = !is_scalar($value) ? json_encode($value) : $value;
$buffer .= '="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"';
}
$buffer .= $tagEnd;
// This is for IE conditional statements support.
if (!is_null($conditional))
{
$buffer .= '<![endif]-->';
}
$buffer .= $lnEnd;
}
// Generate stylesheet declarations
foreach ($document->_style as $type => $content)
{
$buffer .= $tab . '<style';
if (!is_null($type) && (!$document->isHtml5() || !in_array($type, $defaultCssMimes)))
{
$buffer .= ' type="' . $type . '"';
}
$buffer .= '>' . $lnEnd;
// This is for full XHTML support.
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '/*<![CDATA[*/' . $lnEnd;
}
$buffer .= $content . $lnEnd;
// See above note
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '/*]]>*/' . $lnEnd;
}
$buffer .= $tab . '</style>' . $lnEnd;
}
// Generate scripts options
$scriptOptions = $document->getScriptOptions();
if (!empty($scriptOptions))
{
$buffer .= $tab . '<script type="application/json" class="joomla-script-options new">';
$prettyPrint = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
$jsonOptions = json_encode($scriptOptions, $prettyPrint);
$jsonOptions = $jsonOptions ? $jsonOptions : '{}';
$buffer .= $jsonOptions;
$buffer .= '</script>' . $lnEnd;
}
$defaultJsMimes = array('text/javascript', 'application/javascript', 'text/x-javascript', 'application/x-javascript');
$html5NoValueAttributes = array('defer', 'async');
// Generate script file links
foreach ($document->_scripts as $src => $attribs)
{
// Check if script uses IE conditional statements.
$conditional = isset($attribs['options']) && isset($attribs['options']['conditional']) ? $attribs['options']['conditional'] : null;
// Check if script uses media version.
if (isset($attribs['options']['version']) && $attribs['options']['version'] && strpos($src, '?') === false
&& ($mediaVersion || $attribs['options']['version'] !== 'auto'))
{
$src .= '?' . ($attribs['options']['version'] === 'auto' ? $mediaVersion : $attribs['options']['version']);
}
$buffer .= $tab;
// This is for IE conditional statements support.
if (!is_null($conditional))
{
$buffer .= '<!--[if ' . $conditional . ']>';
}
$buffer .= '<script src="' . $src . '"';
// Add script tag attributes.
foreach ($attribs as $attrib => $value)
{
// Don't add the 'options' attribute. This attribute is for internal use (version, conditional, etc).
if ($attrib === 'options')
{
continue;
}
// Don't add type attribute if document is HTML5 and it's a default mime type. 'mime' is for B/C.
if (in_array($attrib, array('type', 'mime')) && $document->isHtml5() && in_array($value, $defaultJsMimes))
{
continue;
}
// B/C: If defer and async is false or empty don't render the attribute.
if (in_array($attrib, array('defer', 'async')) && !$value)
{
continue;
}
// Don't add type attribute if document is HTML5 and it's a default mime type. 'mime' is for B/C.
if ($attrib === 'mime')
{
$attrib = 'type';
}
// B/C defer and async can be set to yes when using the old method.
elseif (in_array($attrib, array('defer', 'async')) && $value === true)
{
$value = $attrib;
}
// Add attribute to script tag output.
$buffer .= ' ' . htmlspecialchars($attrib, ENT_COMPAT, 'UTF-8');
if (!($document->isHtml5() && in_array($attrib, $html5NoValueAttributes)))
{
// Json encode value if it's an array.
$value = !is_scalar($value) ? json_encode($value) : $value;
$buffer .= '="' . htmlspecialchars($value, ENT_COMPAT, 'UTF-8') . '"';
}
}
$buffer .= '></script>';
// This is for IE conditional statements support.
if (!is_null($conditional))
{
$buffer .= '<![endif]-->';
}
$buffer .= $lnEnd;
}
// Generate script declarations
foreach ($document->_script as $type => $content)
{
$buffer .= $tab . '<script';
if (!is_null($type) && (!$document->isHtml5() || !in_array($type, $defaultJsMimes)))
{
$buffer .= ' type="' . $type . '"';
}
$buffer .= '>' . $lnEnd;
// This is for full XHTML support.
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//<![CDATA[' . $lnEnd;
}
$buffer .= $content . $lnEnd;
// See above note
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//]]>' . $lnEnd;
}
$buffer .= $tab . '</script>' . $lnEnd;
}
// Output the custom tags - array_unique makes sure that we don't output the same tags twice
foreach (array_unique($document->_custom) as $custom)
{
$buffer .= $tab . $custom . $lnEnd;
}
return ltrim($buffer, $tab);
}
}