Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ErrorUtils | |
0.00% |
0 / 18 |
|
0.00% |
0 / 3 |
12 | |
0.00% |
0 / 1 |
isDiffingError | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
renderParsoidError | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
renderParsoidErrorSpan | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Cite\Parsoid; |
5 | |
6 | use Wikimedia\Message\MessageValue; |
7 | use Wikimedia\Parsoid\DOM\DocumentFragment; |
8 | use Wikimedia\Parsoid\Ext\DOMUtils; |
9 | use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI; |
10 | use Wikimedia\Parsoid\Utils\DOMCompat; |
11 | |
12 | /** |
13 | * @license GPL-2.0-or-later |
14 | */ |
15 | class ErrorUtils { |
16 | /** |
17 | * This method compares the provided $catName against the Cite error message keys where there are known, |
18 | * expected differences in the rendering (typically: Parsoid renders errors at the bottom of the page rather |
19 | * than inline). It does not take into account categories where there are known, but unexpected/undesired |
20 | * differences (typically: one parser renders an error and the other does not). |
21 | */ |
22 | public static function isDiffingError( string $catName ): bool { |
23 | static $diffingErrors = [ |
24 | 'cite_error_ref_numeric_key' => true, |
25 | 'cite_error_ref_no_key' => true, |
26 | 'cite_error_ref_too_many_keys' => true, |
27 | 'cite_error_references_invalid_parameters' => true, |
28 | 'cite_error_ref_invalid_dir' => true, |
29 | 'cite_error_ref_follow_conflicts' => true, |
30 | 'cite_error_ref_no_input' => true, |
31 | 'cite_error_group_refs_without_references' => true, |
32 | ]; |
33 | return $diffingErrors[$catName] ?? false; |
34 | } |
35 | |
36 | /** |
37 | * Creates a document fragment containing the Parsoid rendering of an error message |
38 | */ |
39 | public static function renderParsoidError( |
40 | ParsoidExtensionAPI $extApi, |
41 | string $errorKey, |
42 | ?array $errorParams |
43 | ): DocumentFragment { |
44 | $error = new MessageValue( $errorKey, $errorParams ?? [] ); |
45 | return self::renderParsoidErrorSpan( $extApi, $error ); |
46 | } |
47 | |
48 | /** |
49 | * Adds classes and lead on an existing Parsoid rendering of an error message, sets the tracking category and |
50 | * returns the completed fragment |
51 | */ |
52 | public static function renderParsoidErrorSpan( |
53 | ParsoidExtensionAPI $extApi, MessageValue $error |
54 | ): DocumentFragment { |
55 | $extApi->addTrackingCategory( 'cite-tracking-category-cite-error' ); |
56 | $fragment = $extApi->createInterfaceI18nFragment( 'cite_error', [ $error ] ); |
57 | $fragSpan = DOMCompat::getFirstElementChild( $fragment ); |
58 | DOMUtils::addAttributes( $fragSpan, [ 'class' => 'error mw-ext-cite-error' ] ); |
59 | return $fragment; |
60 | } |
61 | } |