Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| MediaStructure | |
0.00% |
0 / 29 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| isRedLink | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAlt | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMediaUrl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| parse | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Core; |
| 5 | |
| 6 | use Wikimedia\Parsoid\DOM\Element; |
| 7 | use Wikimedia\Parsoid\DOM\Node; |
| 8 | use Wikimedia\Parsoid\Utils\DiffDOMUtils; |
| 9 | use Wikimedia\Parsoid\Utils\DOMUtils; |
| 10 | use Wikimedia\Parsoid\Utils\WTUtils; |
| 11 | use Wikimedia\Parsoid\Wikitext\Consts; |
| 12 | |
| 13 | /** |
| 14 | * All media should have a fixed structure: |
| 15 | * |
| 16 | * ``` |
| 17 | * <containerElt> |
| 18 | * <linkElt><mediaElt /></linkElt> |
| 19 | * <captionElt>...</captionElt> |
| 20 | * </containerElt> |
| 21 | * ``` |
| 22 | * |
| 23 | * Pull out this fixed structure, being as generous as possible with |
| 24 | * possibly-broken HTML. |
| 25 | */ |
| 26 | class MediaStructure { |
| 27 | |
| 28 | /** |
| 29 | * Node names: figure, span |
| 30 | * |
| 31 | * @var ?Element |
| 32 | */ |
| 33 | public $containerElt; |
| 34 | |
| 35 | /** |
| 36 | * Node names: a, span |
| 37 | * |
| 38 | * @var ?Element |
| 39 | */ |
| 40 | public $linkElt; |
| 41 | |
| 42 | /** |
| 43 | * Node names: img, audio, video, span |
| 44 | * |
| 45 | * @var ?Element |
| 46 | */ |
| 47 | public $mediaElt; |
| 48 | |
| 49 | /** |
| 50 | * Node names: figcaption |
| 51 | * |
| 52 | * @var ?Element |
| 53 | */ |
| 54 | public $captionElt; |
| 55 | |
| 56 | /** |
| 57 | * @param Element $mediaElt |
| 58 | * @param ?Element $linkElt |
| 59 | * @param ?Element $containerElt |
| 60 | */ |
| 61 | public function __construct( |
| 62 | Element $mediaElt, ?Element $linkElt = null, |
| 63 | ?Element $containerElt = null |
| 64 | ) { |
| 65 | $this->mediaElt = $mediaElt; |
| 66 | $this->linkElt = $linkElt; |
| 67 | $this->containerElt = $containerElt; |
| 68 | if ( $containerElt && DOMUtils::nodeName( $containerElt ) === 'figure' ) { |
| 69 | // FIXME: Support last child, which is not the linkElt, as the caption? |
| 70 | $this->captionElt = DOMCompat::querySelector( $containerElt, 'figcaption' ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * We were not able to fetch info for the title, so the media was |
| 76 | * considered missing and rendered as a span. |
| 77 | * |
| 78 | * @return bool |
| 79 | */ |
| 80 | public function isRedLink(): bool { |
| 81 | return ( DOMUtils::nodeName( $this->mediaElt ) === 'span' ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return ?string the resource name if it exists, otherwise null |
| 86 | */ |
| 87 | public function getResource(): ?string { |
| 88 | return DOMCompat::getAttribute( $this->mediaElt, 'resource' ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @return ?string The alt text if it exists, otherwise null |
| 93 | */ |
| 94 | public function getAlt(): ?string { |
| 95 | return DOMCompat::getAttribute( $this->mediaElt, 'alt' ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return ?string The media href if it exists, otherwise null. |
| 100 | */ |
| 101 | public function getMediaUrl(): ?string { |
| 102 | return $this->linkElt ? |
| 103 | DOMCompat::getAttribute( $this->linkElt, 'href' ) : |
| 104 | null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param Node $node |
| 109 | * @return ?MediaStructure |
| 110 | */ |
| 111 | public static function parse( Node $node ): ?MediaStructure { |
| 112 | if ( |
| 113 | // Be a bit more liberal than WTUtils::isGeneratedFigure to support |
| 114 | // serializing arbitrary HTML and old Flow boards |
| 115 | DOMUtils::nodeName( $node ) !== 'figure' && |
| 116 | !WTUtils::isInlineMedia( $node ) |
| 117 | ) { |
| 118 | return null; |
| 119 | } |
| 120 | '@phan-var Element $node'; // @var Element $node |
| 121 | $linkElt = $node; |
| 122 | do { |
| 123 | // Try being lenient, maybe there was a content model violation when |
| 124 | // parsing and an active formatting element was reopened in the wrapper |
| 125 | $linkElt = DiffDOMUtils::firstNonSepChild( $linkElt ); |
| 126 | } while ( |
| 127 | $linkElt instanceof Element && DOMUtils::nodeName( $linkElt ) !== 'a' && |
| 128 | isset( Consts::$HTML['FormattingTags'][DOMUtils::nodeName( $linkElt )] ) |
| 129 | ); |
| 130 | if ( |
| 131 | !( $linkElt instanceof Element && |
| 132 | in_array( DOMUtils::nodeName( $linkElt ), [ 'a', 'span' ], true ) ) |
| 133 | ) { |
| 134 | if ( $linkElt instanceof Element ) { |
| 135 | // Try being lenient, maybe this is the media element and we don't |
| 136 | // have a link elt. See the test, "Image: from basic HTML (1)" |
| 137 | $mediaElt = $linkElt; |
| 138 | $linkElt = null; |
| 139 | } else { |
| 140 | return null; |
| 141 | } |
| 142 | } else { |
| 143 | $mediaElt = DiffDOMUtils::firstNonSepChild( $linkElt ); |
| 144 | } |
| 145 | if ( |
| 146 | !( $mediaElt instanceof Element && |
| 147 | in_array( DOMUtils::nodeName( $mediaElt ), [ 'audio', 'img', 'span', 'video' ], true ) ) |
| 148 | ) { |
| 149 | return null; |
| 150 | } |
| 151 | return new MediaStructure( $mediaElt, $linkElt, $node ); |
| 152 | } |
| 153 | |
| 154 | } |