• 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\FeatureNotAvailableException;
9
use Isocontent\Exception\UnsupportedFormatException;
10

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

26
        if (!is_string($input)) {
192✔
27
            throw new UnsupportedFormatException();
192✔
28
        }
29

192✔
30
        $document = new \DOMDocument('1.0', 'UTF-8');
192✔
31
        if (!$input) {
32
            return;
192✔
33
        }
192✔
34

192✔
35
        $oldUseInternalErrors = libxml_use_internal_errors();
36
        libxml_use_internal_errors(true);
37

38
        /** @var non-empty-string $html */
39
        $html = '<?xml encoding="UTF-8">'.$input;
120✔
40
        $document->loadHTML($html);
41

42
        libxml_clear_errors();
120✔
43
        libxml_use_internal_errors($oldUseInternalErrors);
44

45
        foreach ($document->getElementsByTagName('body') as $root) {
192✔
46
            assert($root instanceof \DOMElement);
47

192✔
48
            foreach ($root->childNodes as $childNode) {
192✔
49
                assert($childNode instanceof \DOMNode);
192✔
50
                $this->parseNode($builder, $childNode);
51
            }
192✔
52
        }
53
    }
192✔
54

192✔
55
    #[\Override]
192✔
56
    public function supportsFormat(string $format): bool
57
    {
192✔
58
        return 'html' === $format && class_exists(\DOMDocument::class);
59
    }
60

8✔
61
    private function parseNode(Builder $builder, \DOMNode $node): void
62
    {
63
        switch ($node->nodeType) {
192✔
64
            case XML_TEXT_NODE:
24✔
65
                assert($node instanceof \DOMText);
66
                $builder->addTextNode(preg_replace('#\s{2,}#', ' ', $node->textContent) ?? '');
67

192✔
68
                return;
192✔
69

70
            case XML_ELEMENT_NODE:
71
                assert($node instanceof \DOMElement);
72
                $blockType = $this->parseBlockType($node);
73
                $childBuilder = $builder->addBlockNode($blockType[0], $blockType[1] ?? []);
74

75
                break;
192✔
76

77
            default:
192✔
78
                return;
192✔
79
        }
16✔
80

81
        if (0 === $node->childNodes->length) {
192✔
82
            return;
16✔
83
        }
84

192✔
85
        foreach ($node->childNodes as $subNode) {
16✔
86
            assert($subNode instanceof \DOMNode);
87
            $this->parseNode($childBuilder, $subNode);
192✔
88
        }
24✔
89
    }
90

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

176✔
100
            case 'h2':
32✔
101
                return ['title', ['level' => 2]];
102

176✔
103
            case 'h3':
72✔
104
                return ['title', ['level' => 3]];
105

144✔
106
            case 'h4':
96✔
107
                return ['title', ['level' => 4]];
108

56✔
109
            case 'h5':
24✔
110
                return ['title', ['level' => 5]];
111

56✔
112
            case 'h6':
24✔
113
                return ['title', ['level' => 6]];
114

56✔
115
            case 'p':
24✔
116
                return ['paragraph'];
117

48✔
118
            case 'em':
16✔
119
                return ['emphasis'];
120

48✔
121
            case 'strong':
16✔
122
                return ['strong'];
123

32✔
124
            case 'span':
16✔
125
                return ['inline_text'];
16✔
126

16✔
127
            case 'ul':
16✔
128
                return ['list', ['ordered' => false]];
16✔
129

16✔
130
            case 'ol':
131
                return ['list', ['ordered' => true]];
16✔
132

×
133
            case 'li':
134
                return ['list_item'];
16✔
135

×
136
            case 'blockquote':
137
                return ['quote'];
16✔
138

×
139
            case 'br':
140
                return ['new_line'];
16✔
141

×
142
            case 'a':
143
                $nodeAttributes = $node->attributes;
16✔
144
                assert($nodeAttributes instanceof \DOMNamedNodeMap);
×
145
                $attributes = array_filter([
146
                    'href' => $nodeAttributes->getNamedItem('href')?->nodeValue,
147
                    'download' => (bool) $nodeAttributes->getNamedItem('download'),
16✔
148
                    'rel' => $nodeAttributes->getNamedItem('target')?->nodeValue,
149
                    'target' => $nodeAttributes->getNamedItem('target')?->nodeValue,
150
                ]);
151

152
                return ['link', $attributes];
153
            case 'del':
154
                return ['stripped'];
155

156
            case 'hr':
157
                return ['separator'];
158

159
            case 'sub':
160
                return ['subscript'];
161

162
            case 'sup':
163
                return ['superscript'];
164

165
            case 'code':
166
                return ['code'];
167

168
            default:
169
                return ['generic'];
170
        }
171
    }
172
}
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