|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vikpe; |
|
4
|
|
|
|
|
5
|
|
|
class HtmlHeadingNormalizer |
|
6
|
|
|
{ |
|
7
|
|
|
public static function demote($html, $levels) |
|
8
|
|
|
{ |
|
9
|
|
|
return self::normalize($html, $levels); |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
public static function promote($html, $levels) |
|
13
|
|
|
{ |
|
14
|
|
|
return self::normalize($html, -$levels); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
private static function normalize($html, $levels) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!self::containsHeadings($html)) { |
|
20
|
|
|
return $html; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$domDocument = new \DOMDocument(); |
|
24
|
|
|
$domDocument->loadHTML($html); |
|
25
|
|
|
|
|
26
|
|
|
$originalHeadings = self::getHeadings($domDocument); |
|
27
|
|
|
$normalizedHeadings = self::normalizeHeadings($originalHeadings, $levels); |
|
28
|
|
|
|
|
29
|
|
|
self::replaceHeadings( |
|
30
|
|
|
$originalHeadings, |
|
31
|
|
|
$normalizedHeadings |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
return $domDocument->saveHTML(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private static function getHeadings(\DOMDocument $domDocument) |
|
38
|
|
|
{ |
|
39
|
|
|
$tagNames = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'); |
|
40
|
|
|
|
|
41
|
|
|
$headings = array(); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($tagNames as $tagName) { |
|
44
|
|
|
foreach ($domDocument->getElementsByTagName($tagName) as $heading) { |
|
45
|
|
|
$headings[] = $heading; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $headings; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private static function normalizeHeadings(array $originalHeadings, $levelDelta) |
|
53
|
|
|
{ |
|
54
|
|
|
$normalizedHeadings = array(); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($originalHeadings as $heading) { |
|
57
|
|
|
$currentLevel = self::tagNameToLevel($heading->tagName); |
|
58
|
|
|
$normalizedLevel = $currentLevel + $levelDelta; |
|
59
|
|
|
|
|
60
|
|
|
$normalizedHeadings[] = self::cloneHeading($heading, $normalizedLevel); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $normalizedHeadings; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private static function replaceHeadings(array $needles, array $replacements) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($needles as $i => $needle) { |
|
69
|
|
|
$needle->parentNode->replaceChild($replacements[$i], $needle); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private static function containsHeadings($html) |
|
74
|
|
|
{ |
|
75
|
|
|
$headingNeedle = '<h'; |
|
76
|
|
|
$containsHeadings = (false !== stripos($html, $headingNeedle)); |
|
77
|
|
|
|
|
78
|
|
|
return $containsHeadings; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private static function tagNameToLevel($tagName) |
|
82
|
|
|
{ |
|
83
|
|
|
return substr($tagName, 1); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private static function levelToTagName($level) |
|
87
|
|
|
{ |
|
88
|
|
|
return 'h'.$level; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private static function cloneHeading(\DOMElement $sourceHeading, $level = null) |
|
92
|
|
|
{ |
|
93
|
|
|
if (null !== $level) { |
|
94
|
|
|
$tagName = self::levelToTagName($level); |
|
95
|
|
|
} else { |
|
96
|
|
|
$tagName = $sourceHeading->tagName; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$targetHeading = $sourceHeading->parentNode->ownerDocument->createElement($tagName); |
|
100
|
|
|
self::copyAttributes($sourceHeading, $targetHeading); |
|
101
|
|
|
self::moveChildNodes($sourceHeading, $targetHeading); |
|
102
|
|
|
|
|
103
|
|
|
return $targetHeading; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
private static function copyAttributes(\DOMElement $source, \DOMElement $target) |
|
107
|
|
|
{ |
|
108
|
|
|
foreach ($source->attributes as $attribute) { |
|
109
|
|
|
$target->setAttribute($attribute->name, $attribute->value); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private static function moveChildNodes(\DOMElement $source, \DOMElement $target) |
|
114
|
|
|
{ |
|
115
|
|
|
while ($source->hasChildNodes()) { |
|
116
|
|
|
// appendChild() actually moves the childNode |
|
117
|
|
|
$target->appendChild($source->childNodes->item(0)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|