Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestFormatter
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 10
1980
0.00% covered (danger)
0.00%
0 / 1
 startDocument
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doctype
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 characters
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatCharacters
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 element
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatElement
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
210
 comment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatComment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 formatDOMNode
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
156
 formatDOMElement
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\Serializer;
5
6use Wikimedia\RemexHtml\DOM\DOMFormatter;
7use Wikimedia\RemexHtml\DOM\DOMUtils;
8use Wikimedia\RemexHtml\HTMLData;
9use Wikimedia\RemexHtml\Tokenizer\Attribute;
10
11/**
12 * A Formatter which is used to format documents in (almost) the way they
13 * appear in the html5lib tests. A little bit of post-processing is required
14 * in the PHPUnit tests.
15 */
16class TestFormatter implements Formatter, DOMFormatter {
17    private const ATTR_NAMESPACES = [
18        HTMLData::NS_XML => 'xml',
19        HTMLData::NS_XLINK => 'xlink',
20        HTMLData::NS_XMLNS => 'xmlns',
21    ];
22
23    /** @inheritDoc */
24    public function startDocument( $fragmentNamespace, $fragmentName ) {
25        return '';
26    }
27
28    /** @inheritDoc */
29    public function doctype( $name, $public, $system ) {
30        $ret = "<!DOCTYPE $name";
31        if ( $public !== '' || $system !== '' ) {
32            $ret .= " \"$public\" \"$system\"";
33        }
34        $ret .= ">\n";
35        return $ret;
36    }
37
38    /** @inheritDoc */
39    public function characters( SerializerNode $parent, $text, $start, $length ) {
40        return $this->formatCharacters( substr( $text, $start, $length ) );
41    }
42
43    private function formatCharacters( string $text ): string {
44        return '"' .
45            str_replace( "\n", "<EOL>", $text ) .
46            "\"\n";
47    }
48
49    /** @inheritDoc */
50    public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
51        return $this->formatElement( $node->namespace, $node->name,
52            $node->attrs->getObjects(), $contents );
53    }
54
55    private function formatElement( ?string $namespace, string $name, array $attrs, ?string $contents ): string {
56        $name = DOMUtils::uncoerceName( $name );
57        if ( $namespace === HTMLData::NS_HTML ) {
58            $tagName = $name;
59        } elseif ( $namespace === HTMLData::NS_SVG ) {
60            $tagName = "svg $name";
61        } elseif ( $namespace === HTMLData::NS_MATHML ) {
62            $tagName = "math $name";
63        } else {
64            $tagName = $name;
65        }
66        $ret = "<$tagName>\n";
67        $sortedAttrs = $attrs;
68        ksort( $sortedAttrs, SORT_STRING );
69        foreach ( $sortedAttrs as $attrName => $attr ) {
70            $localName = DOMUtils::uncoerceName( $attr->localName );
71            if ( $attr->namespaceURI === null
72                || isset( $attr->reallyNoNamespace )
73            ) {
74                $prefix = '';
75            } elseif ( isset( self::ATTR_NAMESPACES[$attr->namespaceURI] ) ) {
76                $prefix = self::ATTR_NAMESPACES[$attr->namespaceURI] . ' ';
77            } else {
78                $prefix = '';
79            }
80            $ret .= "  $prefix$localName=\"{$attr->value}\"\n";
81        }
82        if ( $contents !== null && $contents !== '' ) {
83            $contents = preg_replace( '/^/m', '  ', $contents );
84        } else {
85            $contents = '';
86        }
87        if (
88            // PHP < 8.4 defaults to "suppressHtmlNamespace", in which case
89            // NS_HTML will be represented as `null`
90            ( $namespace === HTMLData::NS_HTML || $namespace === null ) &&
91            $name === 'template'
92        ) {
93            if ( $contents === '' ) {
94                $contents = "  content\n";
95            } else {
96                $contents = "  content\n" . preg_replace( '/^/m', '  ', $contents );
97            }
98        }
99        $ret .= $contents;
100        return $ret;
101    }
102
103    /** @inheritDoc */
104    public function comment( SerializerNode $parent, $text ) {
105        return $this->formatComment( $text );
106    }
107
108    private function formatComment( string $text ): string {
109        return "<!-- $text -->\n";
110    }
111
112    /** @inheritDoc */
113    public function formatDOMNode( $node ) {
114        $contents = '';
115        if ( $node->firstChild ) {
116            foreach ( $node->childNodes as $child ) {
117                $contents .= $this->formatDOMNode( $child );
118            }
119        }
120
121        switch ( $node->nodeType ) {
122            case XML_ELEMENT_NODE:
123                '@phan-var \DOMElement $node'; /** @var \DOMElement $node */
124                return $this->formatDOMElement( $node, $contents );
125
126            case XML_DOCUMENT_NODE:
127            case XML_DOCUMENT_FRAG_NODE:
128                return $contents;
129
130            case XML_TEXT_NODE:
131            case XML_CDATA_SECTION_NODE:
132                '@phan-var \DOMCharacterData $node'; /** @var \DOMCharacterData $node */
133                return $this->formatCharacters( $node->data );
134
135            case XML_COMMENT_NODE:
136                '@phan-var \DOMComment $node'; /** @var \DOMComment $node */
137                return $this->formatComment( $node->data );
138
139            case XML_DOCUMENT_TYPE_NODE:
140                '@phan-var \DOMDocumentType $node'; /** @var \DOMDocumentType $node */
141                return $this->doctype( $node->name, $node->publicId, $node->systemId );
142
143            case XML_PI_NODE:
144            default:
145                return '';
146        }
147    }
148
149    /** @inheritDoc */
150    public function formatDOMElement( $node, $content ) {
151        $attrs = [];
152        foreach ( $node->attributes as $attr ) {
153            $prefix = null;
154            switch ( $attr->namespaceURI ) {
155                case HTMLData::NS_XML:
156                    $prefix = 'xml';
157                    $qName = 'xml:' . $attr->localName;
158                    break;
159                case HTMLData::NS_XMLNS:
160                    if ( $attr->localName === 'xmlns' ) {
161                        $qName = 'xmlns';
162                    } else {
163                        $prefix = 'xmlns';
164                        $qName = 'xmlns:' . $attr->localName;
165                    }
166                    break;
167                case HTMLData::NS_XLINK:
168                    $prefix = 'xlink';
169                    $qName = 'xlink:' . $attr->localName;
170                    break;
171                default:
172                    if ( strlen( $attr->prefix ?? '' ) ) {
173                        $qName = $attr->prefix . ':' . $attr->localName;
174                    } else {
175                        $prefix = $attr->prefix;
176                        $qName = $attr->localName;
177                    }
178            }
179
180            $attrs[$qName] = new Attribute( $qName, $attr->namespaceURI, $prefix,
181                $attr->localName, $attr->value );
182        }
183
184        $qName = $node->prefix ? ( $node->prefix . ':' . $node->localName ) :
185               $node->localName;
186        return $this->formatElement( $node->namespaceURI, $qName, $attrs, $content );
187    }
188}