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
37 private static function cssSerializeString( $value ) {
38 if ( strstr( $value, "\0" ) ) {
39 throw new Exception( "Invalid character in CSS string" );
41 $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
42 $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
43 return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
45 return '"' . $value . '"';
49 * Get language-specific LESS variables for this module.
53 private function getLessVars( ResourceLoaderContext
$context ) {
54 $language = Language
::factory( $context->getLanguage() );
56 // This is very conveniently formatted and we can pass it right through
57 $vars = $language->getImageFiles();
59 // lessc tries to be helpful and parse our variables as LESS source code
60 foreach ( $vars as $key => &$value ) {
61 $value = self
::cssSerializeString( $value );
68 * @param ResourceLoaderContext $context
69 * @return int UNIX timestamp
71 public function getModifiedTime( ResourceLoaderContext
$context ) {
73 parent
::getModifiedTime( $context ),
74 $this->getHashMtime( $context )
79 * @param ResourceLoaderContext $context
82 public function getModifiedHash( ResourceLoaderContext
$context ) {
84 parent
::getModifiedHash( $context ) .
85 serialize( $this->getLessVars( $context ) )
90 * Get a LESS compiler instance for this module.
92 * Set our variables in it.
95 * @param ResourceLoaderContext $context
98 protected function getLessCompiler( ResourceLoaderContext
$context = null ) {
99 $compiler = parent
::getLessCompiler();
100 $compiler->setVariables( $this->getLessVars( $context ) );