Skip to content

Commit

Permalink
Header::normalize(): Add basic normalization rules for title and desc…
Browse files Browse the repository at this point in the history
…ription.
  • Loading branch information
janbarasek authored Mar 24, 2021
1 parent 89361c3 commit 5647916
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/HtmlHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function link(string $key, $value): void

public function metaDescription(string $content): void
{
if (($content = trim($content)) === '') {
if (($content = $this->normalize($content)) === '') {
return;
}
$this->description = $content;
Expand Down Expand Up @@ -169,7 +169,7 @@ public function title(?string $value): void
if ($value === null) {
return;
}
if (($value = trim($value)) !== '') {
if (($value = $this->normalize($value)) !== '') {
$this->title = $value;
$this->tags['title'][] = '<title>' . htmlspecialchars($value, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</title>';
}
Expand Down Expand Up @@ -301,4 +301,14 @@ private function truncate(string $s, int $maxLen, string $append = "\u{2026}"):

return $s;
}


private function normalize(string $haystack): string
{
$haystack = strip_tags($haystack);
$haystack = (string) preg_replace('/[*\-=]{2,}/', ' ', $haystack);
$haystack = (string) preg_replace('/\s+/', ' ', $haystack);

return trim($haystack);
}
}

0 comments on commit 5647916

Please sign in to comment.