3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
22 * Module augmented with context-specific LESS variables.
24 * @ingroup ResourceLoader
27 class ResourceLoaderLessVarFileModule
extends ResourceLoaderFileModule
{
28 protected $lessVariables = [];
33 public function __construct(
35 $localBasePath = null,
36 $remoteBasePath = null
38 if ( isset( $options['lessMessages'] ) ) {
39 $this->lessVariables
= $options['lessMessages'];
41 parent
::__construct( $options, $localBasePath, $remoteBasePath );
47 public function getMessages() {
48 // Overload so MessageBlobStore can detect updates to messages and purge as needed.
49 return array_merge( $this->messages
, $this->lessVariables
);
53 * Exclude a set of messages from a JSON string representation
56 * @param array $exclusions
57 * @return object $blob
59 protected function excludeMessagesFromBlob( $blob, $exclusions ) {
60 $data = json_decode( $blob, true );
61 // unset the LESS variables so that they are not forwarded to JavaScript
62 foreach ( $exclusions as $key ) {
71 protected function getMessageBlob( ResourceLoaderContext
$context ) {
72 $blob = parent
::getMessageBlob( $context );
73 return json_encode( $this->excludeMessagesFromBlob( $blob, $this->lessVariables
) );
77 * Takes a message and wraps it in quotes for compatibility with LESS parser
78 * (ModifyVars) method so that the variable can be loaded and made available to stylesheets.
79 * Note this does not take care of CSS escaping. That will be taken care of as part
83 * @return string wrapped LESS variable definition
85 private static function wrapAndEscapeMessage( $msg ) {
86 return str_replace( "'", "\'", CSSMin
::serializeStringValue( $msg ) );
90 * Get language-specific LESS variables for this module.
92 * @param ResourceLoaderContext $context
93 * @return array LESS variables
95 protected function getLessVars( ResourceLoaderContext
$context ) {
96 $blob = parent
::getMessageBlob( $context );
97 $lessMessages = $this->excludeMessagesFromBlob( $blob, $this->messages
);
99 $vars = parent
::getLessVars( $context );
100 foreach ( $lessMessages as $msgKey => $value ) {
101 $vars['msg-' . $msgKey] = self
::wrapAndEscapeMessage( $value );