Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 189
0.00% covered (danger)
0.00%
0 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestUtils
0.00% covered (danger)
0.00%
0 / 189
0.00% covered (danger)
0.00%
0 / 14
4830
0.00% covered (danger)
0.00%
0 / 1
 encodeXml
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeAbout
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 normalizeOut
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 1
72
 stripParsoidIds
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cleanSpans
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 unwrapSpan
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 newlineAround
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 normalizeIEWVisitor
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
650
 unwrapSpansAndNormalizeIEW
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 normalizePhpOutput
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
90
 normalizeHTML
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
6
 colorString
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
 filterDsr
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 filterNodeDsr
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\ParserTests;
5
6use Error;
7use Exception;
8use Wikimedia\Parsoid\DOM\Comment;
9use Wikimedia\Parsoid\DOM\Element;
10use Wikimedia\Parsoid\DOM\Node;
11use Wikimedia\Parsoid\DOM\Text;
12use Wikimedia\Parsoid\Html2Wt\DOMNormalizer;
13use Wikimedia\Parsoid\Html2Wt\SerializerState;
14use Wikimedia\Parsoid\Html2Wt\WikitextSerializer;
15use Wikimedia\Parsoid\Mocks\MockEnv;
16use Wikimedia\Parsoid\Utils\ContentUtils;
17use Wikimedia\Parsoid\Utils\DOMCompat;
18use Wikimedia\Parsoid\Utils\DOMDataUtils;
19use Wikimedia\Parsoid\Utils\DOMUtils;
20use Wikimedia\Parsoid\Utils\Utils;
21use Wikimedia\Parsoid\Utils\WTUtils;
22
23/**
24 * This class contains helper functions which should not be directly used
25 * outside of Parsoid.
26 *
27 * Per T332457, most of the code in Wikimedia\Parsoid\ParserTests is
28 * "for use in parser test runners only", including the core parser
29 * test runner, but this file is "more internal" than that: core's
30 * parser test runner should not use these helpers directly.
31 *
32 * @internal
33 */
34class TestUtils {
35    /** @var mixed */
36    private static $consoleColor;
37
38    /**
39     * Little helper function for encoding XML entities.
40     *
41     * @param string $str
42     * @return string
43     */
44    public static function encodeXml( string $str ): string {
45        // PORT-FIXME: Find replacement
46        // return entities::encodeXML( $str );
47        return $str;
48    }
49
50    /**
51     * Strip the actual about id from the string
52     * @param string $str
53     * @return string
54     */
55    public static function normalizeAbout( string $str ): string {
56        return preg_replace( "/(about=\\\\?[\"']#mwt)\d+/", '$1', $str );
57    }
58
59    /**
60     * Specialized normalization of the PHP parser & Parsoid output, to ignore
61     * a few known-ok differences in parser test runs.
62     *
63     * This code is also used by the Parsoid round-trip testing code.
64     *
65     * If parsoidOnly is true-ish, we allow more markup through (like property
66     * and typeof attributes), for better checking of parsoid-only test cases.
67     *
68     * @param Element|string $domBody
69     * @param array $options
70     *  - parsoidOnly (bool) Is this test Parsoid Only? Optional. Default: false
71     *  - preserveIEW (bool) Should inter-element WS be preserved? Optional. Default: false
72     *  - hackyNormalize (bool) Apply the normalizer to the html. Optional. Default: false
73     * @return string
74     */
75    public static function normalizeOut( $domBody, array $options = [] ): string {
76        $parsoidOnly = !empty( $options['parsoidOnly'] );
77        $preserveIEW = !empty( $options['preserveIEW'] );
78
79        if ( !empty( $options['hackyNormalize'] ) ) {
80            // Mock env obj
81            //
82            // FIXME: This is ugly.
83            // (a) The normalizer shouldn't need the full env.
84            //     Pass options and a logger instead?
85            // (b) DOM diff code is using page-id for some reason.
86            //     That feels like a carryover of 2013 era code.
87            //     If possible, get rid of it and diff-mark dependency
88            //     on the env object.
89            $mockEnv = new MockEnv( [] );
90            $mockSerializer = new WikitextSerializer( $mockEnv, [] );
91            $mockState = new SerializerState( $mockSerializer, [ 'selserMode' => false ] );
92            if ( is_string( $domBody ) ) {
93                // Careful about the lifetime of this document
94                $doc = ContentUtils::createAndLoadDocument( $domBody );
95                $domBody = DOMCompat::getBody( $doc );
96            } else {
97                DOMDataUtils::visitAndLoadDataAttribs( $domBody, [ 'markNew' => true ] );
98            }
99            ( new DOMNormalizer( $mockState ) )->normalize( $domBody );
100            DOMDataUtils::visitAndStoreDataAttribs( $domBody );
101            DOMDataUtils::getBag( $domBody->ownerDocument )->loaded = false;
102        } elseif ( is_string( $domBody ) ) {
103            $domBody = DOMCompat::getBody( DOMUtils::parseHTML( $domBody ) );
104        }
105
106        $stripTypeof = $parsoidOnly ?
107            '/^mw:Placeholder$/' :
108            '/^mw:(?:DisplaySpace|Placeholder|Nowiki|Transclusion|Entity)$/';
109        $domBody = self::unwrapSpansAndNormalizeIEW( $domBody, $stripTypeof, $parsoidOnly, $preserveIEW );
110        $out = ContentUtils::toXML( $domBody, [ 'innerXML' => true ] );
111        // NOTE that we use a slightly restricted regexp for "attribute"
112        //  which works for the output of DOM serialization.  For example,
113        //  we know that attribute values will be surrounded with double quotes,
114        //  not unquoted or quoted with single quotes.  The serialization
115        //  algorithm is given by:
116        //  http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#serializing-html-fragments
117        if ( !preg_match( '#[^<]*(<\w+(\s+[^\0-\cZ\s"\'>/=]+(="[^"]*")?)*/?>[^<]*)*#u', $out ) ) {
118            throw new Error( 'normalizeOut input is not in standard serialized form' );
119        }
120
121        // Eliminate a source of indeterminacy from leaked strip markers
122        $out = preg_replace( '/UNIQ-.*?-QINU/u', '', $out );
123
124        // Normalize COINS ids -- they aren't stable
125        $out = preg_replace( '/\s?id=[\'"]coins_\d+[\'"]/iu', '', $out );
126
127        // maplink extension
128        $out = preg_replace( '/\s?data-overlays=\'[^\']*\'/u', '', $out );
129
130        // unnecessary attributes, we don't need to check these.
131        $unnecessaryAttribs = 'data-parsoid|prefix|about|rev|datatype|inlist|usemap|vocab';
132        if ( $parsoidOnly ) {
133            $unnecessaryAttribs = "/ ($unnecessaryAttribs)=";
134            $out = preg_replace( $unnecessaryAttribs . '\\\\?"[^\"]*\\\\?"/u', '', $out );
135            $out = preg_replace( $unnecessaryAttribs . "\\\\?'[^\']*\\\\?'/u", '', $out ); // single-quoted variant
136            $out = preg_replace( $unnecessaryAttribs . '&apos;.*?&apos;/u', '', $out ); // apos variant
137            if ( !$options['externallinktarget'] ) {
138                $out = preg_replace( '/ nofollow/', '', $out );
139                $out = str_replace( ' rel="nofollow"', '', $out );
140                $out = preg_replace( '/ noreferrer noopener/', '', $out );
141            }
142
143            // strip self-closed <nowiki /> because we frequently test WTS
144            // <nowiki> insertion by providing an html/parsoid section with the
145            // <meta> tags stripped out, allowing the html2wt test to verify that
146            // the <nowiki> is correctly added during WTS, while still allowing
147            // the html2html and wt2html versions of the test to pass as a
148            // validity check.  If <meta>s were not stripped, these tests would all
149            // have to be modified and split up.  Not worth it at this time.
150            // (see commit 689b22431ad690302420d049b10e689de6b7d426)
151            $out = preg_replace( '#<span typeof="mw:Nowiki"></span>#', '', $out );
152
153            return $out;
154        }
155
156        // strip meta/link elements
157        $out = preg_replace(
158            '#</?(?:meta|link)(?: [^\0-\cZ\s"\'>/=]+(?:=(?:"[^"]*"|\'[^\']*\'))?)*/?>#u',
159            '', $out );
160        // Ignore troublesome attributes.
161        // In addition to attributes listed above, strip other Parsoid-inserted attributes
162        // since these won't be present in legacay parser output.
163        $attribTroubleRE = "/ ($unnecessaryAttribs|data-mw|resource|rel|property|class)=\\\\?";
164        $out = preg_replace( $attribTroubleRE . '"[^"]*\\\\?"/u', '', $out );
165        $out = preg_replace( $attribTroubleRE . "'[^']*\\\\?'/u", '', $out ); // single-quoted variant
166        // strip typeof last
167        $out = preg_replace( '/ typeof="[^\"]*"/u', '', $out );
168        $out = self::stripParsoidIds( $out );
169        $out = preg_replace( '/<span[^>]+about="[^"]*"[^>]*>/u', '', $out );
170        $out = preg_replace( '#(\s)<span>\s*</span>\s*#u', '$1', $out );
171        $out = preg_replace( '#<span>\s*</span>#u', '', $out );
172        $out = preg_replace( '#(href=")(?:\.?\./)+#u', '$1', $out );
173        // replace unnecessary URL escaping
174        $out = preg_replace_callback( '/ href="[^"]*"/u', static function ( $m ) {
175            return Utils::decodeURI( $m[0] );
176        }, $out );
177        // strip thumbnail size prefixes
178        return preg_replace(
179            '#(src="[^"]*?)/thumb(/[0-9a-f]/[0-9a-f]{2}/[^/]+)/[0-9]+px-[^"/]+(?=")#u', '$1$2',
180            $out
181        );
182    }
183
184    /**
185     * Strip Parsoid ID attributes (id="mwXX", used to associate NodeData) from an HTML string
186     * @param string $s
187     * @return string
188     */
189    public static function stripParsoidIds( string $s ): string {
190        return preg_replace( '/ id="mw([-\w]{2,})"/u', '', $s );
191    }
192
193    private static function cleanSpans(
194        Node $node, ?string $stripSpanTypeof
195    ): void {
196        if ( !$stripSpanTypeof ) {
197            return;
198        }
199
200        for ( $child = $node->firstChild; $child; $child = $next ) {
201            $next = $child->nextSibling;
202            if ( $child instanceof Element && DOMCompat::nodeName( $child ) === 'span' &&
203                preg_match( $stripSpanTypeof, DOMCompat::getAttribute( $child, 'typeof' ) ?? '' )
204            ) {
205                self::unwrapSpan( $node, $child, $stripSpanTypeof );
206            }
207        }
208    }
209
210    private static function unwrapSpan(
211        Node $parent, Node $node, ?string $stripSpanTypeof
212    ): void {
213        // first recurse to unwrap any spans in the immediate children.
214        self::cleanSpans( $node, $stripSpanTypeof );
215        // now unwrap this span.
216        DOMUtils::migrateChildren( $node, $parent, $node );
217        $parent->removeChild( $node );
218    }
219
220    private static function newlineAround( ?Node $node ): bool {
221        return $node && preg_match(
222            '/^(body|caption|div|dd|dt|li|p|table|tr|td|th|tbody|dl|ol|ul|h[1-6])$/D',
223            DOMCompat::nodeName( $node )
224        );
225    }
226
227    private static function normalizeIEWVisitor(
228        Node $node, array $opts
229    ): Node {
230        if ( DOMCompat::nodeName( $node ) === 'pre' ) {
231            // Preserve newlines in <pre> tags
232            $opts['inPRE'] = true;
233        }
234        if ( !$opts['preserveIEW'] && $node instanceof Text ) {
235            if ( !$opts['inPRE'] ) {
236                $node->data = preg_replace( '/\s+/u', ' ', $node->data );
237            }
238            if ( $opts['stripLeadingWS'] ) {
239                $node->data = preg_replace( '/^\s+/u', '', $node->data, 1 );
240            }
241            if ( $opts['stripTrailingWS'] ) {
242                $node->data = preg_replace( '/\s+$/uD', '', $node->data, 1 );
243            }
244        }
245        // unwrap certain SPAN nodes
246        self::cleanSpans( $node, $opts['stripSpanTypeof'] );
247        // now remove comment nodes
248        if ( !$opts['parsoidOnly'] ) {
249            for ( $child = $node->firstChild;  $child;  $child = $next ) {
250                $next = $child->nextSibling;
251                if ( $child instanceof Comment ) {
252                    $node->removeChild( $child );
253                }
254            }
255        }
256        // reassemble text nodes split by a comment or span, if necessary
257        if ( $node instanceof Element ) {
258            DOMCompat::normalize( $node );
259        }
260        // now recurse.
261        if ( DOMCompat::nodeName( $node ) === 'pre' ) {
262            // hack, since PHP adds a newline before </pre>
263            $opts['stripLeadingWS'] = false;
264            $opts['stripTrailingWS'] = true;
265        } elseif (
266            DOMCompat::nodeName( $node ) === 'span' &&
267            DOMUtils::matchTypeOf( $node, '/^mw:/' )
268        ) {
269            // SPAN is transparent; pass the strip parameters down to kids
270        } else {
271            $opts['stripLeadingWS'] = $opts['stripTrailingWS'] = self::newlineAround( $node );
272        }
273        $child = $node->firstChild;
274        // Skip over the empty mw:FallbackId <span> and strip leading WS
275        // on the other side of it.
276        if ( $child && DOMUtils::isHeading( $node ) && WTUtils::isFallbackIdSpan( $child ) ) {
277            $child = $child->nextSibling;
278        }
279        for ( ; $child; $child = $next ) {
280            $next = $child->nextSibling;
281            $newOpts = $opts;
282            $newOpts['stripTrailingWS'] = $opts['stripTrailingWS'] && !$child->nextSibling;
283            self::normalizeIEWVisitor( $child, $newOpts );
284            $opts['stripLeadingWS'] = false;
285        }
286
287        if ( $opts['inPRE'] || $opts['preserveIEW'] ) {
288            return $node;
289        }
290
291        // now add newlines around appropriate nodes.
292        for ( $child = $node->firstChild;  $child; $child = $next ) {
293            $prev = $child->previousSibling;
294            $next = $child->nextSibling;
295            if ( self::newlineAround( $child ) ) {
296                if ( $prev instanceof Text ) {
297                    $prev->data = preg_replace( '/\s*$/uD', "\n", $prev->data, 1 );
298                } else {
299                    $prev = $node->ownerDocument->createTextNode( "\n" );
300                    $node->insertBefore( $prev, $child );
301                }
302                if ( $next instanceof Text ) {
303                    $next->data = preg_replace( '/^\s*/u', "\n", $next->data, 1 );
304                } else {
305                    $next = $node->ownerDocument->createTextNode( "\n" );
306                    $node->insertBefore( $next, $child->nextSibling );
307                }
308            }
309        }
310        return $node;
311    }
312
313    /**
314     * Normalize newlines in IEW to spaces instead.
315     *
316     * @param Element $body The document body node to normalize.
317     * @param ?string $stripSpanTypeof Regular expression to strip typeof attributes
318     * @param bool $parsoidOnly
319     * @param bool $preserveIEW
320     * @return Element
321     */
322    public static function unwrapSpansAndNormalizeIEW(
323        Element $body, ?string $stripSpanTypeof = null, bool $parsoidOnly = false, bool $preserveIEW = false
324    ): Element {
325        $opts = [
326            'preserveIEW' => $preserveIEW,
327            'parsoidOnly' => $parsoidOnly,
328            'stripSpanTypeof' => $stripSpanTypeof,
329            'stripLeadingWS' => true,
330            'stripTrailingWS' => true,
331            'inPRE' => false
332        ];
333        // clone body first, since we're going to destructively mutate it.
334        // @phan-suppress-next-line PhanTypeMismatchReturnSuperType
335        return self::normalizeIEWVisitor( $body->cloneNode( true ), $opts );
336    }
337
338    /**
339     * Strip some php output we aren't generating.
340     */
341    public static function normalizePhpOutput( Element $body ): void {
342        // Do not expect section editing for now
343        foreach ( DOMCompat::querySelectorAll( $body, '.mw-editsection' ) as $span ) {
344            DOMCompat::remove( $span );
345        }
346        // Parsoid adds heading wrappers in an OutputTransform stage
347        foreach ( DOMCompat::querySelectorAll( $body, '.mw-heading' ) as $div ) {
348            $nodes = DOMUtils::childNodes( $div );
349            foreach ( $nodes as $i => $child ) {
350                // Remove the nls
351                if ( $i === 0 && $child instanceof Text ) {
352                    $child->data = preg_replace( "/^\n/uD", '', $child->data );
353                }
354                if ( $i === ( count( $nodes ) - 1 ) && $child instanceof Text ) {
355                    $child->data = preg_replace( "/\n$/uD", '', $child->data );
356                }
357                $div->parentNode->insertBefore( $child, $div );
358            }
359            DOMCompat::remove( $div );
360        }
361        // Do not expect a toc for now
362        $toc = DOMCompat::querySelector( $body, '#toc' );
363        if ( $toc ) {
364            DOMCompat::remove( $toc );
365        }
366    }
367
368    /**
369     * Normalize the expected parser output by parsing it using a HTML5 parser and
370     * re-serializing it to HTML. Ideally, the parser would normalize inter-tag
371     * whitespace for us. For now, we fake that by simply stripping all newlines.
372     *
373     * @param string $source
374     * @return string
375     */
376    public static function normalizeHTML( string $source ): string {
377        try {
378            $body = self::unwrapSpansAndNormalizeIEW( DOMCompat::getBody( DOMUtils::parseHTML( $source ) ) );
379            self::normalizePhpOutput( $body );
380            $html = ContentUtils::toXML( $body, [ 'innerXML' => true ] );
381
382            // a few things we ignore for now..
383            //  .replace(/\/wiki\/Main_Page/g, 'Main Page')
384
385            // remove empty span tags
386            $html = preg_replace( '/(\s)<span>\s*<\/span>\s*/u', '$1', $html );
387            $html = preg_replace( '/<span>\s*<\/span>/u', '', $html );
388            // general class and titles, typically on links
389            $html = preg_replace( '/ (class|rel|about|typeof)="[^"]*"/', '', $html );
390            // strip red link markup, we do not check if a page exists yet
391            $html = preg_replace(
392                "#/index.php\\?title=([^']+?)&amp;action=edit&amp;redlink=1#", '/wiki/$1', $html );
393            // strip red link title info
394            $html = preg_replace(
395                "/ \\((?:page does not exist|encara no existeix|bet ele jaratılmaǵan|lonkásá  ezalí tɛ̂)\\)/",
396                '', $html );
397            // the expected html has some extra space in tags, strip it
398            $html = preg_replace( '/<a +href/', '<a href', $html );
399            $html = preg_replace( '#href="/wiki/#', 'href="', $html );
400            $html = preg_replace( '/" +>/', '">', $html );
401            // parsoid always add a page name to lonely fragments
402            $html = preg_replace( '/href="#/', 'href="Main Page#', $html );
403            // replace unnecessary URL escaping
404            $html = preg_replace_callback( '/ href="[^"]*"/',
405                static function ( $m ) {
406                    return Utils::decodeURI( $m[0] );
407                },
408                $html );
409            // strip empty spans
410            $html = preg_replace( '#(\s)<span>\s*</span>\s*#u', '$1', $html );
411            return preg_replace( '#<span>\s*</span>#u', '', $html );
412        } catch ( Exception $e ) {
413            error_log( 'normalizeHTML failed on' . $source . ' with the following error: ' . $e );
414            return $source;
415        }
416    }
417
418    /**
419     * @param string $string
420     * @param string $color
421     * @param bool $inverse
422     * @return string
423     * @suppress PhanUndeclaredClassMethod
424     * @suppress UnusedSuppression
425     */
426    public static function colorString(
427        string $string, string $color, bool $inverse = false
428    ): string {
429        if ( $inverse ) {
430            $color = [ $color, 'reverse' ];
431        }
432
433        if ( !self::$consoleColor ) {
434            // Attempt to instantiate this class to determine if the
435            // (optional) php-console-color library is installed.
436            try {
437                self::$consoleColor = new \PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor();
438            } catch ( Error ) {
439                /* fall back to no-color mode */
440            }
441        }
442
443        if ( self::$consoleColor && self::$consoleColor->isSupported() ) {
444            return self::$consoleColor->apply( $color, $string );
445        } else {
446            return $string;
447        }
448    }
449
450    /**
451     * Removes DSR from data-parsoid for test normalization of a complete document. If
452     * data-parsoid gets subsequently empty, removes it too.
453     * @param string $raw
454     * @return string
455     */
456    public static function filterDsr( string $raw ): string {
457        $doc = ContentUtils::createAndLoadDocument( $raw );
458        foreach ( DOMUtils::childNodes( $doc ) as $child ) {
459            if ( $child instanceof Element ) {
460                self::filterNodeDsr( $child );
461            }
462        }
463        $ret = ContentUtils::ppToXML( DOMCompat::getBody( $doc ), [ 'innerXML' => true ] );
464        $ret = preg_replace( '/\sdata-parsoid="{}"/', '', $ret );
465        return $ret;
466    }
467
468    /**
469     * Removes DSR from data-parsoid for test normalization of an element.
470     */
471    public static function filterNodeDsr( Element $el ): void {
472        $dp = DOMDataUtils::getDataParsoid( $el );
473        unset( $dp->dsr );
474        // XXX: could also set TempData::IS_NEW if !$dp->isModified(),
475        // rather than using the preg_replace above.
476        foreach ( DOMUtils::childNodes( $el ) as $child ) {
477            if ( $child instanceof Element ) {
478                self::filterNodeDsr( $child );
479            }
480        }
481    }
482}