Skip to content

Commit e2d6780

Browse files
authored
Fix #75: Add methods as() and preload() to the Link tag
1 parent 5d158ed commit e2d6780

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
## 1.2.1 under development
44

5-
- Enh #74: Add classes for tags `Em`, `Strong`, `B` and `I` (vjik)
5+
- New #74: Add classes for tags `Em`, `Strong`, `B` and `I` (vjik)
6+
- New #75: Add methods `as()` and `preload()` to the `Link` tag (vjik)
67

78
## 1.2.0 May 04, 2021
89

9-
- Enh #70: Add support `\Stringable` as content in methods `Html::tag()`, `Html::normalTag()`, `Html::a()`,
10+
- New #70: Add support `\Stringable` as content in methods `Html::tag()`, `Html::normalTag()`, `Html::a()`,
1011
`Html::label()`, `Html::option()`, `Html::div()`, `Html::span()`, `Html::p()`, `Html::li()`, `Html::caption()`,
1112
`Html::td()`, `Html::th()` (vjik)
12-
- Enh #71: Add methods `Script::getContent()` and `Style::getContent()` (vjik)
13+
- New #71: Add methods `Script::getContent()` and `Style::getContent()` (vjik)
1314

1415
## 1.1.0 April 09, 2021
1516

16-
- Enh #65: Add classes for table tags `Table`, `Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td` (vjik)
17-
- Enh #69: Add class for tag `Br` (vjik)
17+
- New #65: Add classes for table tags `Table`, `Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td` (vjik)
18+
- New #69: Add class for tag `Br` (vjik)
1819

1920
## 1.0.1 April 04, 2021
2021

src/Tag/Link.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ public function crossOrigin(?string $value): self
7777
return $new;
7878
}
7979

80+
/**
81+
* @link https://www.w3.org/TR/preload/#as-attribute
82+
*/
83+
public function as(?string $value): self
84+
{
85+
$new = clone $this;
86+
$new->attributes['as'] = $value;
87+
return $new;
88+
}
89+
90+
/**
91+
* @link https://www.w3.org/TR/preload/#link-type-preload
92+
* @link https://www.w3.org/TR/preload/#as-attribute
93+
*/
94+
public function preload(string $url, string $as = null): self
95+
{
96+
$new = clone $this;
97+
$new->attributes['rel'] = 'preload';
98+
$new->attributes['href'] = $url;
99+
if ($as !== null) {
100+
$new->attributes['as'] = $as;
101+
}
102+
return $new;
103+
}
104+
80105
protected function getName(): string
81106
{
82107
return 'link';

tests/common/Tag/LinkTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,43 @@ public function testCrossOrigin(string $expected, ?string $crossOrigin): void
110110
$this->assertSame($expected, (string)Link::tag()->crossOrigin($crossOrigin));
111111
}
112112

113+
public function dataTestAs(): array
114+
{
115+
return [
116+
['<link>', null],
117+
['<link as="">', ''],
118+
['<link as="video">', 'video'],
119+
];
120+
}
121+
122+
/**
123+
* @dataProvider dataTestAs
124+
*/
125+
public function testAs(string $expected, ?string $as): void
126+
{
127+
$this->assertSame($expected, (string)Link::tag()->as($as));
128+
}
129+
130+
public function dataPreload(): array
131+
{
132+
return [
133+
['<link href="/main.css" rel="preload">', '/main.css'],
134+
['<link href="/main.css" rel="preload" as="style">', '/main.css', 'style'],
135+
];
136+
}
137+
138+
/**
139+
* @dataProvider dataPreload
140+
*/
141+
public function testPreload(string $expected, string $url, string $as = null): void
142+
{
143+
$tag = $as === null
144+
? Link::tag()->preload($url)
145+
: Link::tag()->preload($url, $as);
146+
147+
$this->assertSame($expected, $tag->render());
148+
}
149+
113150
public function testImmutability(): void
114151
{
115152
$link = Link::tag();
@@ -119,5 +156,7 @@ public function testImmutability(): void
119156
$this->assertNotSame($link, $link->type(null));
120157
$this->assertNotSame($link, $link->title(null));
121158
$this->assertNotSame($link, $link->crossOrigin(null));
159+
$this->assertNotSame($link, $link->as(null));
160+
$this->assertNotSame($link, $link->preload(''));
122161
}
123162
}

0 commit comments

Comments
 (0)