Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
MobileFormatter
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applyTransforms
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 canApply
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3use HtmlFormatter\HtmlFormatter;
4use MobileFrontend\Transforms\IMobileTransform;
5use Wikimedia\Parsoid\Utils\DOMCompat;
6
7/**
8 * Converts HTML into a mobile-friendly version
9 */
10class MobileFormatter extends HtmlFormatter {
11
12    /**
13     * @inheritDoc
14     */
15    public function __construct( $html ) {
16        // This is specific to HtmlFormatter, decouple it from callers.
17        parent::__construct( self::wrapHTML( $html ) );
18    }
19
20    /**
21     * Performs various transformations to the content to make it appropriate for mobile devices.
22     * @param array<IMobileTransform> $transforms lit of transforms to be sequentually applied
23     *   to html DOM
24     */
25    public function applyTransforms( array $transforms ) {
26        $doc = $this->getDoc();
27        $body = DOMCompat::querySelector( $doc, 'body' );
28
29        foreach ( $transforms as $transform ) {
30            $transform->apply( $body );
31        }
32    }
33
34    /**
35     * Check whether the MobileFormatter can be applied to the text of a page.
36     * @param string $text
37     * @param array $options with 'maxHeadings' and 'maxImages' keys that limit the MobileFormatter
38     *  to pages with less than or equal to that number of headings and images.
39     * @return bool
40     */
41    public static function canApply( $text, $options ) {
42        $headings = preg_match_all( '/<[hH][1-6]/', $text );
43        $imgs = preg_match_all( '/<img/', $text );
44        return $headings <= $options['maxHeadings'] && $imgs <= $options['maxImages'];
45    }
46}