Skip to content

Commit 55f5c07

Browse files
authored
Fix #88: Add Noscript tag support and shortcuts for Script tag via methods Script::noscript() and Script::noscriptTag()
1 parent 4e2f404 commit 55f5c07

9 files changed

Lines changed: 125 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Yii HTML Change Log
22

3-
## 2.0.1 under development
3+
## 2.1.0 under development
44

5-
- no changes in this release.
5+
- New #88: Add `Noscript` tag support and shortcuts for `Script` tag via methods `Script::noscript()`
6+
and `Script::noscriptTag()` (vjik)
67

78
## 2.0.0 August 24, 2021
89

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
The package provides various tools to help with dynamic server-side generation of HTML:
1919

2020
- Tag classes `A`, `B`, `Br`, `Button`, `Div`, `Em`, `I`, `Img`, `Input` (and specialized `Checkbox`, `Radio`), `Label`, `Li`, `Link`,
21-
`Meta`, `Ol`, `Optgroup`, `Option`, `P`, `Script`, `Select`, `Span`, `Strong`, `Style`, `Textarea`, `Ul`, `Table`,
21+
`Meta`, `Noscript`, `Ol`, `Optgroup`, `Option`, `P`, `Script`, `Select`, `Span`, `Strong`, `Style`, `Textarea`, `Ul`, `Table`,
2222
`Caption`, `Colgroup`, `Col`, `Thead`, `Tbody`, `Tfoot`, `Tr`, `Th`, `Td`.
2323
- `CustomTag` class that helps to generate custom tag with any attributes.
2424
- HTML widgets `CheckboxList` and `RadioList`.
@@ -231,6 +231,7 @@ Overall the helper has the following method groups.
231231
- p
232232
- br
233233
- script
234+
- noscript
234235
- span
235236
- strong
236237
- style

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"phpunit/phpunit": "^9.5",
3535
"roave/infection-static-analysis-plugin": "^1.9",
3636
"spatie/phpunit-watcher": "^1.23",
37-
"vimeo/psalm": "^4.9"
37+
"vimeo/psalm": "^4.10"
3838
},
3939
"autoload": {
4040
"psr-4": {

src/Html.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Yiisoft\Html\Tag\Li;
2828
use Yiisoft\Html\Tag\Link;
2929
use Yiisoft\Html\Tag\Meta;
30+
use Yiisoft\Html\Tag\Noscript;
3031
use Yiisoft\Html\Tag\Ol;
3132
use Yiisoft\Html\Tag\Optgroup;
3233
use Yiisoft\Html\Tag\Option;
@@ -384,6 +385,17 @@ public static function script(string $content = '', array $attributes = []): Scr
384385
return $tag;
385386
}
386387

388+
/**
389+
* Generates a {@see Noscript} tag.
390+
*
391+
* @param string|Stringable $content Tag content.
392+
*/
393+
public static function noscript($content = ''): Noscript
394+
{
395+
$tag = Noscript::tag();
396+
return $content === '' ? $tag : $tag->content($content);
397+
}
398+
387399
/**
388400
* Generates a {@see Meta} tag.
389401
*

src/Tag/Noscript.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tag;
6+
7+
use Yiisoft\Html\Tag\Base\NormalTag;
8+
use Yiisoft\Html\Tag\Base\TagContentTrait;
9+
10+
/**
11+
* @link https://html.spec.whatwg.org/multipage/scripting.html#the-noscript-element
12+
*/
13+
final class Noscript extends NormalTag
14+
{
15+
use TagContentTrait;
16+
17+
protected function getName(): string
18+
{
19+
return 'noscript';
20+
}
21+
}

src/Tag/Script.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Html\Tag;
66

7+
use Stringable;
78
use Yiisoft\Html\Tag\Base\NormalTag;
89

910
/**
@@ -12,6 +13,7 @@
1213
final class Script extends NormalTag
1314
{
1415
private string $content = '';
16+
private ?Noscript $noscript = null;
1517

1618
/**
1719
* @link https://www.w3.org/TR/html52/semantics-scripting.html#script-content-restrictions
@@ -90,6 +92,23 @@ public function defer(bool $defer = true): self
9092
return $new;
9193
}
9294

95+
/**
96+
* @param string|Stringable|null $content
97+
*/
98+
public function noscript($content): self
99+
{
100+
$new = clone $this;
101+
$new->noscript = $content === null ? null : Noscript::tag()->content($content);
102+
return $new;
103+
}
104+
105+
public function noscriptTag(?Noscript $noscript): self
106+
{
107+
$new = clone $this;
108+
$new->noscript = $noscript;
109+
return $new;
110+
}
111+
93112
protected function getName(): string
94113
{
95114
return 'script';
@@ -102,4 +121,9 @@ protected function generateContent(): string
102121
{
103122
return $this->content;
104123
}
124+
125+
protected function after(): string
126+
{
127+
return $this->noscript !== null ? (string)$this->noscript : '';
128+
}
105129
}

tests/common/HtmlTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use InvalidArgumentException;
88
use PHPUnit\Framework\TestCase;
99
use Yiisoft\Html\Html;
10+
use Yiisoft\Html\Tag\Div;
11+
1012
use function array_key_exists;
1113

1214
final class HtmlTest extends TestCase
@@ -115,6 +117,13 @@ public function testScript(): void
115117
);
116118
}
117119

120+
public function testNoscript(): void
121+
{
122+
$this->assertSame('<noscript></noscript>', Html::noscript()->render());
123+
$this->assertSame('<noscript>hello</noscript>', Html::noscript('hello')->render());
124+
$this->assertSame('<noscript><div></div></noscript>', Html::noscript(Div::tag())->render());
125+
}
126+
118127
public function testMeta(): void
119128
{
120129
$this->assertSame('<meta>', Html::meta()->render());

tests/common/Tag/NoscriptTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Html\Tests\Tag;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Html\Tag\Img;
9+
use Yiisoft\Html\Tag\Noscript;
10+
11+
final class NoscriptTest extends TestCase
12+
{
13+
public function testBase(): void
14+
{
15+
$this->assertSame(
16+
'<noscript><img src="pixel.png"></noscript>',
17+
(string)Noscript::tag()->content(Img::tag()->src('pixel.png')),
18+
);
19+
}
20+
}

tests/common/Tag/ScriptTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Yiisoft\Html\Tests\Tag;
66

77
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Html\Tag\Div;
9+
use Yiisoft\Html\Tag\Noscript;
810
use Yiisoft\Html\Tag\Script;
911

1012
final class ScriptTest extends TestCase
@@ -89,6 +91,35 @@ public function testDefer(): void
8991
$this->assertSame('<script></script>', (string)Script::tag()->defer(true)->defer(false));
9092
}
9193

94+
public function testNoscript(): void
95+
{
96+
$this->assertSame(
97+
'<script></script><noscript>hello</noscript>',
98+
(string)Script::tag()->noscript('hello'),
99+
);
100+
$this->assertSame(
101+
'<script></script><noscript><div></div></noscript>',
102+
(string)Script::tag()->noscript(Div::tag()),
103+
);
104+
$this->assertSame(
105+
'<script></script>',
106+
(string)Script::tag()->noscript(null),
107+
);
108+
}
109+
110+
public function testNoscriptTag(): void
111+
{
112+
$noscriptTag = Noscript::tag()->content('hello');
113+
$this->assertSame(
114+
'<script></script><noscript>hello</noscript>',
115+
(string)Script::tag()->noscriptTag($noscriptTag),
116+
);
117+
$this->assertSame(
118+
'<script></script>',
119+
(string)Script::tag()->noscriptTag(null),
120+
);
121+
}
122+
92123
public function testImmutability(): void
93124
{
94125
$script = Script::tag();
@@ -99,5 +130,7 @@ public function testImmutability(): void
99130
$this->assertNotSame($script, $script->charset(null));
100131
$this->assertNotSame($script, $script->async());
101132
$this->assertNotSame($script, $script->defer());
133+
$this->assertNotSame($script, $script->noscript(null));
134+
$this->assertNotSame($script, $script->noscriptTag(null));
102135
}
103136
}

0 commit comments

Comments
 (0)