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

un-zero-un / Isocontent / 19910422417

03 Dec 2025 10:09PM UTC coverage: 93.906% (-0.6%) from 94.49%
19910422417

push

gha

339 of 361 relevant lines covered (93.91%)

128.66 hits per line

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

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

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

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

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

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

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

50
    #[\Override]
192✔
51
    public function supportsFormat(string $format): bool
52
    {
192✔
53
        return 'html' === $format;
192✔
54
    }
192✔
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

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

24✔
70
                break;
71

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16✔
137
            case 'a':
×
138
                $nodeAttributes = $node->attributes;
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

152
            case 'sup':
16✔
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