Skip to content

Commit cc188ed

Browse files
Fix html tags matching and replacing
- Void tags are parsed differently of common tags - Improves replacing attributes of tags
1 parent 5c807f0 commit cc188ed

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Entities/HtmlSpecs.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace RenatoMarinho\LaravelPageSpeed\Entities;
4+
5+
class HtmlSpecs
6+
{
7+
public static function voidElements(): array
8+
{
9+
return [
10+
'area',
11+
'base',
12+
'br',
13+
'col',
14+
'embed',
15+
'hr',
16+
'img',
17+
'input',
18+
'link',
19+
'meta',
20+
'param',
21+
'source',
22+
'track',
23+
'wbr',
24+
];
25+
}
26+
}

src/Middleware/PageSpeed.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RenatoMarinho\LaravelPageSpeed\Middleware;
44

55
use Closure;
6+
use RenatoMarinho\LaravelPageSpeed\Entities\HtmlSpecs;
67
use Symfony\Component\HttpFoundation\StreamedResponse;
78
use Symfony\Component\HttpFoundation\BinaryFileResponse;
89

@@ -109,10 +110,26 @@ protected function shouldProcessPageSpeed($request, $response)
109110
*/
110111
protected function matchAllHtmlTag(array $tags, string $buffer): array
111112
{
112-
$tags = '('.implode('|', $tags).')';
113+
$voidTags = array_intersect($tags, HtmlSpecs::voidElements());
114+
$normalTags = array_diff($tags, $voidTags);
113115

114-
preg_match_all("/\<\s*{$tags}[^>]*\>((.|\n)*?)\<\s*\/\s*{$tags}\>/", $buffer, $matches);
115-
return $matches;
116+
return array_merge(
117+
$this->matchTags($voidTags, '/\<\s*(%tags)[^>]*\>/', $buffer),
118+
$this->matchTags($normalTags, '/\<\s*(%tags)[^>]*\>((.|\n)*?)\<\s*\/\s*(%tags)\>/', $buffer)
119+
);
120+
}
121+
122+
protected function matchTags(array $tags, string $pattern, string $buffer): array
123+
{
124+
if (empty($tags)) {
125+
return [];
126+
}
127+
128+
$normalizedPattern = str_replace('%tags', implode('|', $tags), $pattern);
129+
130+
preg_match_all($normalizedPattern, $buffer, $matches);
131+
132+
return $matches[0];
116133
}
117134

118135
/**
@@ -127,12 +144,11 @@ protected function matchAllHtmlTag(array $tags, string $buffer): array
127144
*/
128145
protected function replaceInsideHtmlTags(array $tags, string $regex, string $replace, string $buffer): string
129146
{
130-
foreach ($this->matchAllHtmlTag($tags, $buffer)[0] as $tagMatched) {
131-
preg_match_all($regex, $tagMatched, $tagContentsMatchedToReplace);
147+
foreach ($this->matchAllHtmlTag($tags, $buffer) as $tagMatched) {
148+
preg_match_all($regex, $tagMatched, $contentsMatched);
132149

133-
foreach ($tagContentsMatchedToReplace[0] as $tagContentReplace) {
134-
$buffer = str_replace($tagContentReplace, $replace, $buffer);
135-
}
150+
$tagAfterReplace = str_replace($contentsMatched[0], $replace, $tagMatched);
151+
$buffer = str_replace($tagMatched, $tagAfterReplace, $buffer);
136152
}
137153

138154
return $buffer;

0 commit comments

Comments
 (0)