3 * Resource loader module for the edit toolbar.
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.
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.
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
24 * ResourceLoader module for the edit toolbar.
28 class ResourceLoaderEditToolbarModule
extends ResourceLoaderFileModule
{
30 * Serialize a string (escape and quote) for use as a CSS string value.
31 * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
33 * @param string $value
36 private static function cssSerializeString( $value ) {
37 if ( strstr( $value, "\0" ) ) {
38 throw new Exception( "Invalid character in CSS string" );
40 $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
41 $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
42 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
44 return '"' . $value . '"';
48 * Get language-specific LESS variables for this module.
52 private function getLessVars( ResourceLoaderContext
$context ) {
53 $language = Language
::factory( $context->getLanguage() );
55 // This is very conveniently formatted and we can pass it right through
56 $vars = $language->getImageFiles();
58 // lessc tries to be helpful and parse our variables as LESS source code
59 foreach ( $vars as $key => &$value ) {
60 $value = self
::cssSerializeString( $value );
67 * @param ResourceLoaderContext $context
68 * @return int UNIX timestamp
70 public function getModifiedTime( ResourceLoaderContext
$context ) {
72 parent
::getModifiedTime( $context ),
73 $this->getHashMtime( $context )
78 * @param ResourceLoaderContext $context
81 public function getModifiedHash( ResourceLoaderContext
$context ) {
83 parent
::getModifiedHash( $context ) .
84 serialize( $this->getLessVars( $context ) )
89 * Get a LESS compiler instance for this module.
91 * Set our variables in it.
94 * @param ResourceLoaderContext $context
97 protected function getLessCompiler( ResourceLoaderContext
$context = null ) {
98 $compiler = parent
::getLessCompiler();
99 $compiler->setVariables( $this->getLessVars( $context ) );