This patch adds support for the LESS stylesheet language to ResourceLoader. LESS is a stylesheet language that compiles into CSS. The patch includes lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is explained in a MediaWiki RFC which accompanies this patch, available at <https://www.mediawiki.org/wiki/Requests_for_comment/LESS>. LESS support is provided for ResourceLoader file modules. It is triggered by the presence of the '.less' extension in stylesheet filenames. LESS files are compiled by lessc, and the resultant CSS is subjected to the standard set of transformations (CSSJanus & CSSMin). The immediate result of LESS compilation is encoded as an array, which includes the list of LESS files that were compiled and their mtimes. This array is cached. Cache invalidation is performed by comparing the cached mtimes with the mtimes of the files on disk. If the compiler itself throws an exception, ResourceLoader constructs a compilation result which consists of the error message encoded as a CSS comment. Failed compilation results are cached too, but with an expiration time of five minutes. The expiration time is required because the full list of referenced files is not known. Three configuration variables configure the global environment for LESS modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and $wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names to CSS values, specified as strings. Variables declared in this array are available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except it maps custom function names to PHP callables. These functions can be called from within LESS to transform values. Read more about custom functions at <http://leafo.net/lessphp/docs/#custom_functions>. Finally, $wgResourceLoaderLESSImportPaths specifies file system paths in addition to the current module's path where the LESS compiler should look up files referenced in @import statements. The issue of handling of /* @embed */ and /* @noflip */ annotations is left unresolved. Earlier versions of this patch included an @embed analog implemented as a LESS custom function, but there was enough ambiguity about whether the strategy it took was optimal to merit discussing it in a separate, follow-up patch. Bug: 40964 Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
147 lines
3.9 KiB
PHP
147 lines
3.9 KiB
PHP
<?php
|
|
|
|
class ResourceLoaderTest extends MediaWikiTestCase {
|
|
|
|
protected static $resourceLoaderRegisterModulesHook;
|
|
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
|
|
// $wgResourceLoaderLESSFunctions, $wgResourceLoaderLESSImportPaths; $wgResourceLoaderLESSVars;
|
|
|
|
$this->setMwGlobals( array(
|
|
'wgResourceLoaderLESSFunctions' => array(
|
|
'test-sum' => function ( $frame, $less ) {
|
|
$sum = 0;
|
|
foreach ( $frame[2] as $arg ) {
|
|
$sum += (int)$arg[1];
|
|
}
|
|
return $sum;
|
|
},
|
|
),
|
|
'wgResourceLoaderLESSImportPaths' => array(
|
|
dirname( __DIR__ ) . '/data/less/common',
|
|
),
|
|
'wgResourceLoaderLESSVars' => array(
|
|
'foo' => '2px',
|
|
'Foo' => '#eeeeee',
|
|
'bar' => 5,
|
|
),
|
|
) );
|
|
}
|
|
|
|
/* Hook Methods */
|
|
|
|
/**
|
|
* ResourceLoaderRegisterModules hook
|
|
*/
|
|
public static function resourceLoaderRegisterModules( &$resourceLoader ) {
|
|
self::$resourceLoaderRegisterModulesHook = true;
|
|
|
|
return true;
|
|
}
|
|
|
|
/* Provider Methods */
|
|
public static function provideValidModules() {
|
|
return array(
|
|
array( 'TEST.validModule1', new ResourceLoaderTestModule() ),
|
|
);
|
|
}
|
|
|
|
public static function provideResourceLoaderContext() {
|
|
$resourceLoader = new ResourceLoader();
|
|
$request = new FauxRequest();
|
|
return array(
|
|
array( new ResourceLoaderContext( $resourceLoader, $request ) ),
|
|
);
|
|
}
|
|
|
|
/* Test Methods */
|
|
|
|
/**
|
|
* Ensures that the ResourceLoaderRegisterModules hook is called when a new ResourceLoader object is constructed
|
|
* @covers ResourceLoader::__construct
|
|
*/
|
|
public function testCreatingNewResourceLoaderCallsRegistrationHook() {
|
|
self::$resourceLoaderRegisterModulesHook = false;
|
|
$resourceLoader = new ResourceLoader();
|
|
$this->assertTrue( self::$resourceLoaderRegisterModulesHook );
|
|
|
|
return $resourceLoader;
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideValidModules
|
|
* @depends testCreatingNewResourceLoaderCallsRegistrationHook
|
|
* @covers ResourceLoader::register
|
|
* @covers ResourceLoader::getModule
|
|
*/
|
|
public function testRegisteredValidModulesAreAccessible(
|
|
$name, ResourceLoaderModule $module, ResourceLoader $resourceLoader
|
|
) {
|
|
$resourceLoader->register( $name, $module );
|
|
$this->assertEquals( $module, $resourceLoader->getModule( $name ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideResourceLoaderContext
|
|
* @covers ResourceLoaderFileModule::compileLessFile
|
|
*/
|
|
public function testLessFileCompilation( $context ) {
|
|
$basePath = __DIR__ . '/../data/less/module';
|
|
$module = new ResourceLoaderFileModule( array(
|
|
'localBasePath' => $basePath,
|
|
'styles' => array( 'styles.less' ),
|
|
) );
|
|
$styles = $module->getStyles( $context );
|
|
$this->assertStringEqualsFile( $basePath . '/styles.css', $styles['all'] );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providePackedModules
|
|
*/
|
|
public function testMakePackedModulesString( $desc, $modules, $packed ) {
|
|
$this->assertEquals( $packed, ResourceLoader::makePackedModulesString( $modules ), $desc );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providePackedModules
|
|
*/
|
|
public function testexpandModuleNames( $desc, $modules, $packed ) {
|
|
$this->assertEquals( $modules, ResourceLoaderContext::expandModuleNames( $packed ), $desc );
|
|
}
|
|
|
|
public static function providePackedModules() {
|
|
return array(
|
|
array(
|
|
'Example from makePackedModulesString doc comment',
|
|
array( 'foo.bar', 'foo.baz', 'bar.baz', 'bar.quux' ),
|
|
'foo.bar,baz|bar.baz,quux',
|
|
),
|
|
array(
|
|
'Example from expandModuleNames doc comment',
|
|
array( 'jquery.foo', 'jquery.bar', 'jquery.ui.baz', 'jquery.ui.quux' ),
|
|
'jquery.foo,bar|jquery.ui.baz,quux',
|
|
),
|
|
array(
|
|
'Regression fixed in r88706 with dotless names',
|
|
array( 'foo', 'bar', 'baz' ),
|
|
'foo,bar,baz',
|
|
),
|
|
array(
|
|
'Prefixless modules after a prefixed module',
|
|
array( 'single.module', 'foobar', 'foobaz' ),
|
|
'single.module|foobar,foobaz',
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/* Stubs */
|
|
|
|
class ResourceLoaderTestModule extends ResourceLoaderModule {
|
|
}
|
|
|
|
/* Hooks */
|
|
global $wgHooks;
|
|
$wgHooks['ResourceLoaderRegisterModules'][] = 'ResourceLoaderTest::resourceLoaderRegisterModules';
|