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