Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| MobileFormatter | |
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| applyTransforms | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | use MobileFrontend\Transforms\IMobileTransform; |
| 4 | use Wikimedia\Parsoid\Core\DOMCompat; |
| 5 | use Wikimedia\Parsoid\DOM\Element; |
| 6 | use Wikimedia\Parsoid\Ext\DOMUtils; |
| 7 | use Wikimedia\Parsoid\Wt2Html\XHtmlSerializer; |
| 8 | |
| 9 | /** |
| 10 | * Converts HTML into a mobile-friendly version |
| 11 | */ |
| 12 | class MobileFormatter { |
| 13 | |
| 14 | /** @var Element */ |
| 15 | private $body; |
| 16 | |
| 17 | public function __construct( string $html ) { |
| 18 | $doc = DOMUtils::parseHTML( $html ); |
| 19 | $this->body = DOMCompat::getBody( $doc ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Performs various transformations to the content to make it appropriate for mobile devices. |
| 24 | * |
| 25 | * @param array<IMobileTransform> $transforms lit of transforms to be sequentially applied |
| 26 | * to html DOM |
| 27 | */ |
| 28 | public function applyTransforms( array $transforms ): void { |
| 29 | foreach ( $transforms as $transform ) { |
| 30 | $transform->apply( $this->body ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the serialized HTML |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public function getHtml(): string { |
| 40 | // Like DOMCompat::getInnerHTML(), but disable 'smartQuote' for compatibility with |
| 41 | // ParserOutput::EDITSECTION_REGEX matching 'mw:editsection' tags (T274709) |
| 42 | return XHtmlSerializer::serialize( $this->body, [ 'innerXML' => true, 'smartQuote' => false ] )['html']; |
| 43 | } |
| 44 | } |