Skip to content

Commit f732fa3

Browse files
authored
Merge pull request #7022 from kenjis/fix-html-helper-link_tag
fix: `link_tag()` missing `type="application/rss+xml"`
2 parents 80436ff + fb76493 commit f732fa3

2 files changed

Lines changed: 89 additions & 10 deletions

File tree

system/Helpers/html_helper.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,20 @@ function script_tag($src = '', bool $indexPage = false): string
222222
/**
223223
* Link
224224
*
225-
* Generates link to a CSS file
225+
* Generates link tag
226226
*
227-
* @param array|string $href Stylesheet href or an array
228-
* @param bool $indexPage should indexPage be added to the CSS path.
227+
* @param array<string, bool|string>|string $href Stylesheet href or an array
228+
* @param bool $indexPage should indexPage be added to the CSS path.
229229
*/
230-
function link_tag($href = '', string $rel = 'stylesheet', string $type = 'text/css', string $title = '', string $media = '', bool $indexPage = false, string $hreflang = ''): string
231-
{
230+
function link_tag(
231+
$href = '',
232+
string $rel = 'stylesheet',
233+
string $type = 'text/css',
234+
string $title = '',
235+
string $media = '',
236+
bool $indexPage = false,
237+
string $hreflang = ''
238+
): string {
232239
$link = '<link ';
233240

234241
// extract fields if needed
@@ -258,7 +265,7 @@ function link_tag($href = '', string $rel = 'stylesheet', string $type = 'text/c
258265

259266
$link .= 'rel="' . $rel . '" ';
260267

261-
if (! in_array($rel, ['alternate', 'canonical'], true)) {
268+
if ($type !== '' && $rel !== 'canonical' && $hreflang === '' && ! ($rel === 'alternate' && $media !== '')) {
262269
$link .= 'type="' . $type . '" ';
263270
}
264271

tests/system/Helpers/HTMLHelperTest.php

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,71 @@ public function testLinkTag()
312312
$this->assertSame($expected, link_tag($target));
313313
}
314314

315-
public function testLinkTagComplete()
315+
public function testLinkTagMedia()
316316
{
317-
$target = 'https://styles.com/css/mystyles.css';
318-
$expected = '<link href="https://styles.com/css/mystyles.css" rel="banana" type="fruit" media="VHS" title="Go away" />';
319-
$this->assertSame($expected, link_tag($target, 'banana', 'fruit', 'Go away', 'VHS'));
317+
$target = 'https://styles.com/css/mystyles.css';
318+
$tag = link_tag($target, 'stylesheet', 'text/css', '', 'print');
319+
320+
$expected = '<link href="https://styles.com/css/mystyles.css" rel="stylesheet" type="text/css" media="print" />';
321+
$this->assertSame($expected, $tag);
322+
}
323+
324+
public function testLinkTagTitle()
325+
{
326+
$tag = link_tag('default.css', 'stylesheet', 'text/css', 'Default Style');
327+
328+
$expected = '<link href="http://example.com/default.css" rel="stylesheet" type="text/css" title="Default Style" />';
329+
$this->assertSame($expected, $tag);
330+
}
331+
332+
public function testLinkTagFavicon()
333+
{
334+
$tag = link_tag('favicon.ico', 'shortcut icon', 'image/ico');
335+
336+
$expected = '<link href="http://example.com/favicon.ico" rel="shortcut icon" type="image/ico" />';
337+
$this->assertSame($expected, $tag);
338+
}
339+
340+
public function testLinkTagRss()
341+
{
342+
$tag = link_tag('feed', 'alternate', 'application/rss+xml', 'My RSS Feed');
343+
344+
$expected = '<link href="http://example.com/feed" rel="alternate" type="application/rss+xml" title="My RSS Feed" />';
345+
$this->assertSame($expected, $tag);
346+
}
347+
348+
public function testLinkTagAlternate()
349+
{
350+
$tag = link_tag(
351+
'http://sp.example.com/',
352+
'alternate',
353+
'',
354+
'',
355+
'only screen and (max-width: 640px)'
356+
);
357+
358+
$expected = '<link href="http://sp.example.com/" rel="alternate" media="only screen and (max-width: 640px)" />';
359+
$this->assertSame($expected, $tag);
360+
}
361+
362+
public function testLinkTagArrayAlternate()
363+
{
364+
$tag = link_tag([
365+
'href' => 'http://sp.example.com/',
366+
'rel' => 'alternate',
367+
'media' => 'only screen and (max-width: 640px)',
368+
]);
369+
370+
$expected = '<link href="http://sp.example.com/" rel="alternate" media="only screen and (max-width: 640px)" />';
371+
$this->assertSame($expected, $tag);
372+
}
373+
374+
public function testLinkTagCanonical()
375+
{
376+
$tag = link_tag('http://www.example.com/', 'canonical');
377+
378+
$expected = '<link href="http://www.example.com/" rel="canonical" />';
379+
$this->assertSame($expected, $tag);
320380
}
321381

322382
public function testLinkTagArray()
@@ -329,6 +389,18 @@ public function testLinkTagArray()
329389
$this->assertSame($expected, link_tag($parms));
330390
}
331391

392+
public function testLinkTagArrayHreflang()
393+
{
394+
$tag = link_tag([
395+
'href' => 'https://example.com/en',
396+
'rel' => 'alternate',
397+
'hreflang' => 'x-default',
398+
]);
399+
400+
$expected = '<link href="https://example.com/en" hreflang="x-default" rel="alternate" />';
401+
$this->assertSame($expected, $tag);
402+
}
403+
332404
public function testDocType()
333405
{
334406
$target = 'html4-strict';

0 commit comments

Comments
 (0)