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

un-zero-un / Isocontent / 19911539666

03 Dec 2025 10:57PM UTC coverage: 93.767% (-0.1%) from 93.906%
19911539666

push

gha

346 of 369 relevant lines covered (93.77%)

129.28 hits per line

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

92.5
/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
15
{
16
    /**
17
     * @psalm-suppress MixedAssignment
18
     */
200✔
19
    #[\Override]
20
    public function parse(Builder $builder, mixed $input): void
21
    {
200✔
22
        if (!class_exists(\DOMDocument::class)) {
×
23
            throw new FeatureNotAvailableException(\DOMDocument::class, __CLASS__); // @codeCoverageIgnore
24
        }
25

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

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

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

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

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

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

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

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

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

68
                return;
192✔
69

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

192✔
75
                break;
24✔
76

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

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

85
        foreach ($node->childNodes as $subNode) {
86
            assert($subNode instanceof \DOMNode);
87
            $this->parseNode($childBuilder, $subNode);
192✔
88
        }
89
    }
192✔
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':
16✔
98
                return ['title', ['level' => 1]];
99

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

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

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

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

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

176✔
115
            case 'p':
72✔
116
                return ['paragraph'];
117

144✔
118
            case 'em':
96✔
119
                return ['emphasis'];
120

56✔
121
            case 'strong':
24✔
122
                return ['strong'];
123

56✔
124
            case 'span':
24✔
125
                return ['inline_text'];
126

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

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

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

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

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

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

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

16✔
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