diff --git a/demo/broken.html b/demo/broken.html index a08732c..c2d60e3 100644 --- a/demo/broken.html +++ b/demo/broken.html @@ -1,7 +1,7 @@ - Broken Page +

Welcome to a broken page

diff --git a/src/Rules/PageTitle.php b/src/Rules/PageTitle.php new file mode 100644 index 0000000..b9910ef --- /dev/null +++ b/src/Rules/PageTitle.php @@ -0,0 +1,49 @@ +tagName) !== 'head') { + return null; + } + + $titles = $element->getElementsByTagName('title'); + + if ($titles->length === 0) { + return $this->createViolation( + $element, + $this->getDescription(), + 'Add a element inside the <head>.' + ); + } + + $titleText = ''; + $firstTitle = $titles->item(0); + if ($firstTitle !== null) { + $titleText = trim($firstTitle->textContent); + } + + if ($titleText === '') { + return $this->createViolation( + $element, + $this->getDescription(), + 'Provide descriptive text inside the <title> element.' + ); + } + + return null; + } +} diff --git a/src/Rules/StandardRuleSet.php b/src/Rules/StandardRuleSet.php index 7bb97d1..c4b1638 100644 --- a/src/Rules/StandardRuleSet.php +++ b/src/Rules/StandardRuleSet.php @@ -28,6 +28,7 @@ class StandardRuleSet new PlaceholderAsLabel(), new ImageAltRedundant(), new AriaHiddenFocusable(), + new PageTitle(), ]; return $rules; }