Resolve complex arguments to LESS helper functions
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderLESSFunctions.php
1 <?php
2 /**
3 * PHP-provided functions for LESS; see docs for $wgResourceLoaderLESSFunctions
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 class ResourceLoaderLESSFunctions {
24 /**
25 * Check if an image file reference is suitable for embedding.
26 * An image is embeddable if it (a) exists, (b) has a suitable MIME-type,
27 * (c) does not exceed IE<9 size limit of 32kb. This is a LESS predicate
28 * function; it returns a LESS boolean value and can thus be used as a
29 * mixin guard.
30 *
31 * @par Example:
32 * @code
33 * .background-image(@url) when(embeddable(@url)) {
34 * background-image: url(@url) !ie;
35 * }
36 * @endcode
37 */
38 public static function embeddable( $frame, $less ) {
39 $base = pathinfo( $less->parser->sourceName, PATHINFO_DIRNAME );
40 $url = trim( $less->compileValue( $frame ), '"\'' );
41 $file = realpath( $base . '/' . $url );
42 return $less->toBool( $file
43 && strpos( $url, '//' ) === false
44 && filesize( $file ) < CSSMin::EMBED_SIZE_LIMIT
45 && CSSMin::getMimeType( $file ) !== false );
46 }
47
48 /**
49 * Convert an image URI to a base64-encoded data URI.
50 *
51 * @par Example:
52 * @code
53 * .fancy-button {
54 * background-image: embed('../images/button-bg.png');
55 * }
56 * @endcode
57 */
58 public static function embed( $frame, $less ) {
59 $base = pathinfo( $less->parser->sourceName, PATHINFO_DIRNAME );
60 $url = trim( $less->compileValue( $frame ), '"\'' );
61 $file = realpath( $base . '/' . $url );
62
63 $data = CSSMin::encodeImageAsDataURI( $file );
64 $less->addParsedFile( $file );
65 return 'url(' . $data . ')';
66 }
67 }