• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

un-zero-un / Isocontent / 19909526193

03 Dec 2025 09:33PM UTC coverage: 94.49% (-0.02%) from 94.505%
19909526193

push

gha

343 of 363 relevant lines covered (94.49%)

131.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.67
/src/Parser/DOMParser.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Isocontent\Parser;
6

7
use Isocontent\AST\Builder;
8
use Isocontent\Exception\UnsupportedFormatException;
9

10
/**
11
 * A simple HTML parser using DOMDocument / LibXML.
12
 */
13
final class DOMParser implements Parser
14
{
200✔
15
    /**
16
     * @psalm-suppress MixedAssignment
17
     */
200✔
18
    #[\Override]
200✔
19
    public function parse(Builder $builder, mixed $input): void
8✔
20
    {
21
        if (!is_string($input)) {
22
            throw new UnsupportedFormatException();
192✔
23
        }
192✔
24

25
        $document = new \DOMDocument('1.0', 'UTF-8');
26
        if (!$input) {
192✔
27
            return;
192✔
28
        }
29

192✔
30
        $oldUseInternalErrors = libxml_use_internal_errors();
192✔
31
        libxml_use_internal_errors(true);
32

192✔
33
        /** @var non-empty-string $html */
192✔
34
        $html = '<?xml encoding="UTF-8">'.$input;
192✔
35
        $document->loadHTML($html);
36

37
        libxml_clear_errors();
38
        libxml_use_internal_errors($oldUseInternalErrors);
39

120✔
40
        foreach ($document->getElementsByTagName('body') as $root) {
41
            assert($root instanceof \DOMElement);
42

120✔
43
            foreach ($root->childNodes as $childNode) {
44
                assert($childNode instanceof \DOMNode);
45
                $this->parseNode($builder, $childNode);
192✔
46
            }
47
        }
192✔
48
    }
192✔
49

192✔
50
    #[\Override]
51
    public function supportsFormat(string $format): bool
192✔
52
    {
53
        return 'html' === $format;
192✔
54
    }
192✔
55

192✔
56
    private function parseNode(Builder $builder, \DOMNode $node): void
57
    {
192✔
58
        switch ($node->nodeType) {
59
            case XML_TEXT_NODE:
60
                assert($node instanceof \DOMText);
8✔
61
                $builder->addTextNode(preg_replace('#\s{2,}#', ' ', $node->textContent) ?? '');
62

63
                return;
192✔
64

24✔
65
            case XML_ELEMENT_NODE:
66
                assert($node instanceof \DOMElement);
67
                $blockType = $this->parseBlockType($node);
192✔
68
                $childBuilder = $builder->addBlockNode($blockType[0], $blockType[1] ?? []);
192✔
69

70
                break;
71

72
            default:
73
                return;
74
        }
75

192✔
76
        if (0 === $node->childNodes->length) {
77
            return;
192✔
78
        }
192✔
79

16✔
80
        foreach ($node->childNodes as $subNode) {
81
            assert($subNode instanceof \DOMNode);
192✔
82
            $this->parseNode($childBuilder, $subNode);
16✔
83
        }
84
    }
192✔
85

16✔
86
    /**
87
     * @return array{0: string, 1?: array<string, scalar>}
192✔
88
     */
24✔
89
    private function parseBlockType(\DOMElement $node): array
90
    {
192✔
91
        switch ($node->nodeName) {
16✔
92
            case 'h1':
93
                return ['title', ['level' => 1]];
192✔
94

16✔
95
            case 'h2':
96
                return ['title', ['level' => 2]];
192✔
97

96✔
98
            case 'h3':
99
                return ['title', ['level' => 3]];
176✔
100

32✔
101
            case 'h4':
102
                return ['title', ['level' => 4]];
176✔
103

72✔
104
            case 'h5':
105
                return ['title', ['level' => 5]];
144✔
106

96✔
107
            case 'h6':
108
                return ['title', ['level' => 6]];
56✔
109

24✔
110
            case 'p':
111
                return ['paragraph'];
56✔
112

24✔
113
            case 'em':
114
                return ['emphasis'];
56✔
115

24✔
116
            case 'strong':
117
                return ['strong'];
48✔
118

16✔
119
            case 'span':
120
                return ['inline_text'];
48✔
121

16✔
122
            case 'ul':
123
                return ['list', ['ordered' => false]];
32✔
124

16✔
125
            case 'ol':
16✔
126
                return ['list', ['ordered' => true]];
16✔
127

16✔
128
            case 'li':
16✔
129
                return ['list_item'];
16✔
130

131
            case 'blockquote':
16✔
132
                return ['quote'];
×
133

134
            case 'br':
16✔
135
                return ['new_line'];
×
136

137
            case 'a':
16✔
138
                $nodeAttributes = $node->attributes;
×
139
                assert($nodeAttributes instanceof \DOMNamedNodeMap);
140
                $attributes = array_filter(['href' => $nodeAttributes->getNamedItem('href')?->nodeValue]);
16✔
141

×
142
                return ['link', $attributes];
143
            case 'del':
16✔
144
                return ['stripped'];
×
145

146
            case 'hr':
147
                return ['separator'];
16✔
148

149
            case 'sub':
150
                return ['subscript'];
151

152
            case 'sup':
153
                return ['superscript'];
154

155
            case 'code':
156
                return ['code'];
157

158
            default:
159
                return ['generic'];
160
        }
161
    }
162
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc