Skip to content

Commit 22118ff

Browse files
Remove trailing slashes only of void elements
1 parent cc188ed commit 22118ff

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

src/Middleware/RemoveQuotes.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace RenatoMarinho\LaravelPageSpeed\Middleware;
44

5+
use RenatoMarinho\LaravelPageSpeed\Entities\HtmlSpecs;
6+
57
class RemoveQuotes extends PageSpeed
68
{
79
public function apply($buffer)
810
{
11+
$buffer = $this->replaceInsideHtmlTags(HtmlSpecs::voidElements(), '/\/>/', '>', $buffer);
12+
913
$replace = [
1014
'/ src="(.\S*?)"/' => ' src=$1',
1115
'/ width="(.\S*?)"/' => ' width=$1',
@@ -16,7 +20,6 @@ public function apply($buffer)
1620
'/ border="(.\S*?)"/' => ' border=$1',
1721
'/ crossorigin="(.\S*?)"/' => ' crossorigin=$1',
1822
'/ type="(.\S*?)"/' => ' type=$1',
19-
'/\/>/' => '>',
2023
];
2124

2225
return $this->replace($replace, $buffer);

tests/Boilerplate/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<div style="background-image: url('img/test-bg.jpg');">
4242
<h1>Test Background Image</h1>
4343
</div>
44+
45+
<svg width="325" height="200" xmlns="http://www.w3.org/2000/svg">
46+
<path d="M 80 80 A 45 45, 0, 0, 0, 125 125 L 125 80 Z" fill="green"/>
47+
<path d="M 230 80 A 45 45, 0, 1, 0, 275 125 L 275 80 Z" fill="red"/>
48+
</svg>
49+
4450
<img src="http://emblemsbf.com/img/18346.jpg" width="250" style="height:300px; padding:10px" />
4551

4652
<img src="tile whitespace.png" width="250" style="height:300px; padding:10px"/>

tests/Middleware/RemoveQuotesTest.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,35 @@
77

88
class RemoveQuotesTest extends TestCase
99
{
10+
protected $response;
11+
12+
public function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->response = $this->middleware->handle($this->request, $this->getNext());
17+
}
18+
1019
protected function getMiddleware()
1120
{
1221
$this->middleware = new RemoveQuotes();
1322
}
1423

1524
public function testRemoveQuotes()
1625
{
17-
$response = $this->middleware->handle($this->request, $this->getNext());
18-
19-
$this->assertStringContainsString('<link rel="apple-touch-icon" href="icon.png">', $response->getContent());
20-
$this->assertStringContainsString('<meta charset=utf-8>', $response->getContent());
21-
$this->assertStringContainsString('<meta name=viewport content="width=device-width, initial-scale=1">', $response->getContent());
22-
$this->assertStringContainsString('<img src=http://emblemsbf.com/img/18346.jpg width=250 style="height:300px; padding:10px" >', $response->getContent());
23-
$this->assertStringContainsString('<img src=/images/1000coin.png>', $response->getContent());
24-
$this->assertStringContainsString('<vue-component :src="\'src\'" :type="\'type\'" :width="200"></vue-component>', $response->getContent());
25-
$this->assertStringContainsString('<img src="tile whitespace.png" width=250 style="height:300px; padding:10px">', $response->getContent());
26-
$this->assertStringContainsString('<input type=text name="name with spaces" value="name with spaces" width=100%>', $response->getContent());
26+
$this->assertStringContainsString('<link rel="apple-touch-icon" href="icon.png">', $this->response->getContent());
27+
$this->assertStringContainsString('<meta charset=utf-8>', $this->response->getContent());
28+
$this->assertStringContainsString('<meta name=viewport content="width=device-width, initial-scale=1">', $this->response->getContent());
29+
$this->assertStringContainsString('<img src=http://emblemsbf.com/img/18346.jpg width=250 style="height:300px; padding:10px" >', $this->response->getContent());
30+
$this->assertStringContainsString('<img src=/images/1000coin.png>', $this->response->getContent());
31+
$this->assertStringContainsString('<vue-component :src="\'src\'" :type="\'type\'" :width="200"></vue-component>', $this->response->getContent());
32+
$this->assertStringContainsString('<img src="tile whitespace.png" width=250 style="height:300px; padding:10px">', $this->response->getContent());
33+
$this->assertStringContainsString('<input type=text name="name with spaces" value="name with spaces" width=100%>', $this->response->getContent());
34+
}
35+
36+
public function testWontRemoveTrailingSlashesOfNonVoidElements()
37+
{
38+
$this->assertStringContainsString('<path d="M 80 80 A 45 45, 0, 0, 0, 125 125 L 125 80 Z" fill="green"/>', $this->response->getContent());
39+
$this->assertStringContainsString('<path d="M 230 80 A 45 45, 0, 1, 0, 275 125 L 275 80 Z" fill="red"/>', $this->response->getContent());
2740
}
2841
}

0 commit comments

Comments
 (0)