1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens\Elements; |
4
|
|
|
|
5
|
|
|
use Groundskeeper\Tokens\ElementTypes\MetadataContent; |
6
|
|
|
use Groundskeeper\Tokens\ElementTypes\OpenElement; |
7
|
|
|
use Groundskeeper\Tokens\NonParticipating; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* "head" element |
12
|
|
|
* |
13
|
|
|
* https://html.spec.whatwg.org/multipage/semantics.html#the-head-element |
14
|
|
|
*/ |
15
|
|
|
class Head extends OpenElement |
16
|
|
|
{ |
17
|
33 |
|
protected function fixSelf(LoggerInterface $logger) |
18
|
|
|
{ |
19
|
|
|
// Look for "title" element |
20
|
33 |
|
foreach ($this->children as $child) { |
21
|
33 |
|
if ($child instanceof Title) { |
22
|
33 |
|
return; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Missing title. |
27
|
4 |
|
$logger->debug('Adding "title" element. One "title" element required.'); |
28
|
4 |
|
$title = new Title($this->configuration, 0, 0, 'title'); |
29
|
4 |
|
$this->prependChild($title); |
30
|
4 |
|
} |
31
|
|
|
|
32
|
31 |
|
protected function removeInvalidChildren(LoggerInterface $logger) |
33
|
|
|
{ |
34
|
|
|
// "head" element must contain only metadata content elements. |
35
|
|
|
// "head" element must contain exactly one "title" element. |
36
|
|
|
// "head" element must contain either 0 or 1 "base" element. |
37
|
31 |
|
$titleCount = 0; |
38
|
31 |
|
$baseCount = 0; |
39
|
31 |
|
foreach ($this->children as $child) { |
40
|
31 |
|
if ($child instanceof NonParticipating) { |
41
|
1 |
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
31 |
|
if (!$child instanceof MetadataContent) { |
45
|
1 |
|
$logger->debug('Removing ' . $child . '. Only children of metadata content allowed.'); |
46
|
1 |
|
$this->removeChild($child); |
47
|
|
|
|
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
31 |
|
if ($child instanceof Title) { |
52
|
31 |
|
++$titleCount; |
53
|
31 |
|
if ($titleCount > 1) { |
54
|
1 |
|
$logger->debug('Removing ' . $child . '. Only one "title" element allowed.'); |
55
|
1 |
|
$this->removeChild($child); |
56
|
|
|
|
57
|
31 |
|
continue; |
58
|
|
|
} |
59
|
11 |
|
} elseif ($child instanceof Base) { |
60
|
3 |
|
++$baseCount; |
61
|
3 |
|
if ($baseCount > 1) { |
62
|
1 |
|
$logger->debug('Removing ' . $child . '. Maximum one "base" element allowed.'); |
63
|
|
|
|
64
|
1 |
|
$this->removeChild($child); |
65
|
|
|
|
66
|
31 |
|
continue; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
31 |
|
} |
71
|
|
|
|
72
|
32 |
|
protected function removeInvalidSelf(LoggerInterface $logger) : bool |
73
|
|
|
{ |
74
|
|
|
// "head" element must be a child of "html" element. |
75
|
32 |
|
$parent = $this->getParent(); |
76
|
32 |
|
if ($parent !== null && !$parent instanceof Html) { |
77
|
1 |
|
$logger->debug('Removing ' . $this . '. Must be a child of "html" element.'); |
78
|
|
|
|
79
|
1 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
31 |
|
return false; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|