• 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\UnsupportedFormatException;
9

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

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

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

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

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

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

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

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

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

192✔
63
                return;
64

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

70
                break;
71

8✔
72
            default:
73
                return;
74
        }
192✔
75

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

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

86
    /**
87
     * @return array{0: string, 1?: array<string, scalar>}
192✔
88
     */
89
    private function parseBlockType(\DOMElement $node): array
192✔
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

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

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

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

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

96✔
110
            case 'p':
111
                return ['paragraph'];
176✔
112

32✔
113
            case 'em':
114
                return ['emphasis'];
176✔
115

72✔
116
            case 'strong':
117
                return ['strong'];
144✔
118

96✔
119
            case 'span':
120
                return ['inline_text'];
56✔
121

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

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

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

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

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

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

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

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

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

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

155
            case 'code':
16✔
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