Less update 3.21

Update because of deprecations with php 8.2.
4.1 as reserve
This commit is contained in:
gtbu 2023-04-24 17:13:37 +02:00
parent 4d2d486e14
commit 242963b878
141 changed files with 2819 additions and 1656 deletions

View file

@ -2,28 +2,14 @@
/**
* Autoloader
*
* @package Less
* @subpackage autoload
*/
class Less_Autoloader {
/**
* Registered flag
*
* @var boolean
*/
/** @var bool */
protected static $registered = false;
/**
* Library directory
*
* @var string
*/
protected static $libDir;
/**
* Register the autoloader in the spl autoloader
* Register the autoloader in the SPL autoloader
*
* @return void
* @throws Exception If there was an error in registration
@ -33,9 +19,7 @@ class Less_Autoloader {
return;
}
self::$libDir = dirname( __FILE__ );
if ( false === spl_autoload_register( array( 'Less_Autoloader', 'loadClass' ) ) ) {
if ( !spl_autoload_register( [ 'Less_Autoloader', 'loadClass' ] ) ) {
throw new Exception( 'Unable to register Less_Autoloader::loadClass as an autoloading method.' );
}
@ -43,17 +27,17 @@ class Less_Autoloader {
}
/**
* Unregisters the autoloader
* Unregister the autoloader
*
* @return void
*/
public static function unregister() {
spl_autoload_unregister( array( 'Less_Autoloader', 'loadClass' ) );
spl_autoload_unregister( [ 'Less_Autoloader', 'loadClass' ] );
self::$registered = false;
}
/**
* Loads the class
* Load the class
*
* @param string $className The class to load
*/
@ -64,14 +48,10 @@ class Less_Autoloader {
}
$className = substr( $className, 5 );
$fileName = self::$libDir . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php';
$fileName = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php';
if ( file_exists( $fileName ) ) {
require $fileName;
return true;
} else {
throw new Exception( 'file not loadable '.$fileName );
}
require $fileName;
return true;
}
}

View file

@ -1,14 +1,5 @@
# Changelog
## 4.0.0
* [All changes](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+log/v4.0.0)
* Remove support for PHP 7.2 and 7.3. Raise requirement to PHP 7.4+.
* Remove support for `cache_method=php` and `cache_method=var_export`, only the faster and more secure `cache_method=serialize` is now available. The built-in cache remains disabled by default.
* Fix `url(#myid)` to be treated as absolute URL. [T331649](https://phabricator.wikimedia.org/T331688)
* Fix "Undefined property" PHP 8.1 warning when `calc()` is used with CSS `var()`. [T331688](https://phabricator.wikimedia.org/T331688)
* Less_Parser: Improve performance by removing MatchFuncs and NewObj overhead. (Timo Tijhof)
## 3.2.1
* [All changes](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+log/v3.2.1)

View file

@ -1,27 +1,21 @@
<?php
require_once dirname( __FILE__ ).'/Version.php';
/**
* Utility for handling the generation and caching of css files
*
* @package Less
* @subpackage cache
*
*/
class Less_Cache {
// directory less.php can use for storing data
public static $cache_dir = false;
public static $cache_dir = false;
// prefix for the storing data
public static $prefix = 'lessphp_';
public static $prefix = 'lessphp_';
// prefix for the storing vars
public static $prefix_vars = 'lessphpvars_';
public static $prefix_vars = 'lessphpvars_';
// specifies the number of seconds after which data created by less.php will be seen as 'garbage' and potentially cleaned up
public static $gc_lifetime = 604800;
public static $gc_lifetime = 604800;
/**
* Save and reuse the results of compiled less files.
@ -31,31 +25,31 @@ class Less_Cache {
* @param array $less_files Array of .less files to compile
* @param array $parser_options Array of compiler options
* @param array $modify_vars Array of variables
* @return string Name of the css file
* @return string|false Name of the css file
*/
public static function Get( $less_files, $parser_options = array(), $modify_vars = array() ) {
public static function Get( $less_files, $parser_options = [], $modify_vars = [] ) {
// check $cache_dir
if ( isset( $parser_options['cache_dir'] ) ) {
Less_Cache::$cache_dir = $parser_options['cache_dir'];
self::$cache_dir = $parser_options['cache_dir'];
}
if ( empty( Less_Cache::$cache_dir ) ) {
if ( empty( self::$cache_dir ) ) {
throw new Exception( 'cache_dir not set' );
}
if ( isset( $parser_options['prefix'] ) ) {
Less_Cache::$prefix = $parser_options['prefix'];
self::$prefix = $parser_options['prefix'];
}
if ( empty( Less_Cache::$prefix ) ) {
if ( empty( self::$prefix ) ) {
throw new Exception( 'prefix not set' );
}
if ( isset( $parser_options['prefix_vars'] ) ) {
Less_Cache::$prefix_vars = $parser_options['prefix_vars'];
self::$prefix_vars = $parser_options['prefix_vars'];
}
if ( empty( Less_Cache::$prefix_vars ) ) {
if ( empty( self::$prefix_vars ) ) {
throw new Exception( 'prefix_vars not set' );
}
@ -65,18 +59,18 @@ class Less_Cache {
// create a file for variables
if ( !empty( $modify_vars ) ) {
$lessvars = Less_Parser::serializeVars( $modify_vars );
$vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1( $lessvars ) . '.less';
$vars_file = self::$cache_dir . self::$prefix_vars . sha1( $lessvars ) . '.less';
if ( !file_exists( $vars_file ) ) {
file_put_contents( $vars_file, $lessvars );
}
$less_files += array( $vars_file => '/' );
$less_files += [ $vars_file => '/' ];
}
// generate name for compiled css file
$hash = md5( json_encode( $less_files ) );
$list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list';
$list_file = self::$cache_dir . self::$prefix . $hash . '.list';
// check cached content
if ( !isset( $parser_options['use_cache'] ) || $parser_options['use_cache'] === true ) {
@ -129,19 +123,13 @@ class Less_Cache {
* @param array $modify_vars Array of variables
* @return string Name of the css file
*/
public static function Regen( $less_files, $parser_options = array(), $modify_vars = array() ) {
public static function Regen( $less_files, $parser_options = [], $modify_vars = [] ) {
$parser_options['use_cache'] = false;
return self::Get( $less_files, $parser_options, $modify_vars );
}
public static function Cache( &$less_files, $parser_options = array() ) {
// get less.php if it exists
$file = dirname( __FILE__ ) . '/Less.php';
if ( file_exists( $file ) && !class_exists( 'Less_Parser' ) ) {
require_once $file;
}
$parser_options['cache_dir'] = Less_Cache::$cache_dir;
public static function Cache( &$less_files, $parser_options = [] ) {
$parser_options['cache_dir'] = self::$cache_dir;
$parser = new Less_Parser( $parser_options );
// combine files
@ -172,54 +160,52 @@ class Less_Cache {
return $parser_options['output'];
}
return Less_Cache::$cache_dir.$parser_options['output'];
return self::$cache_dir . $parser_options['output'];
}
return Less_Cache::$cache_dir.$compiled_name;
return self::$cache_dir . $compiled_name;
}
private static function CompiledName( $files, $extrahash ) {
// save the file list
$temp = array( Less_Version::cache_version );
$temp = [ Less_Version::cache_version ];
foreach ( $files as $file ) {
$temp[] = filemtime( $file )."\t".filesize( $file )."\t".$file;
$temp[] = filemtime( $file ) . "\t" . filesize( $file ) . "\t" . $file;
}
return Less_Cache::$prefix.sha1( json_encode( $temp ).$extrahash ).'.css';
return self::$prefix . sha1( json_encode( $temp ) . $extrahash ) . '.css';
}
public static function SetCacheDir( $dir ) {
Less_Cache::$cache_dir = $dir;
self::$cache_dir = $dir;
self::CheckCacheDir();
}
public static function CheckCacheDir() {
Less_Cache::$cache_dir = str_replace( '\\', '/', Less_Cache::$cache_dir );
Less_Cache::$cache_dir = rtrim( Less_Cache::$cache_dir, '/' ).'/';
self::$cache_dir = str_replace( '\\', '/', self::$cache_dir );
self::$cache_dir = rtrim( self::$cache_dir, '/' ) . '/';
if ( !file_exists( Less_Cache::$cache_dir ) ) {
if ( !mkdir( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: '.Less_Cache::$cache_dir );
if ( !file_exists( self::$cache_dir ) ) {
if ( !mkdir( self::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: ' . self::$cache_dir );
}
} elseif ( !is_dir( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: '.Less_Cache::$cache_dir );
} elseif ( !is_dir( self::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: ' . self::$cache_dir );
} elseif ( !is_writable( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: '.Less_Cache::$cache_dir );
} elseif ( !is_writable( self::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: ' . self::$cache_dir );
}
}
/**
* Delete unused less.php files
*
*/
public static function CleanCache() {
static $clean = false;
if ( $clean || empty( Less_Cache::$cache_dir ) ) {
if ( $clean || empty( self::$cache_dir ) ) {
return;
}
@ -227,9 +213,9 @@ class Less_Cache {
// only remove files with extensions created by less.php
// css files removed based on the list files
$remove_types = array( 'lesscache' => 1,'list' => 1,'less' => 1,'map' => 1 );
$remove_types = [ 'lesscache' => 1,'list' => 1,'less' => 1,'map' => 1 ];
$files = scandir( Less_Cache::$cache_dir );
$files = scandir( self::$cache_dir );
if ( !$files ) {
return;
}
@ -238,7 +224,7 @@ class Less_Cache {
foreach ( $files as $file ) {
// don't delete if the file wasn't created with less.php
if ( strpos( $file, Less_Cache::$prefix ) !== 0 ) {
if ( strpos( $file, self::$prefix ) !== 0 ) {
continue;
}
@ -249,7 +235,7 @@ class Less_Cache {
continue;
}
$full_path = Less_Cache::$cache_dir . $file;
$full_path = self::$cache_dir . $file;
$mtime = filemtime( $full_path );
// don't delete if it's a relatively new file
@ -261,7 +247,7 @@ class Less_Cache {
if ( $type === 'list' ) {
self::ListFiles( $full_path, $list, $css_file_name );
if ( $css_file_name ) {
$css_file = Less_Cache::$cache_dir . $css_file_name;
$css_file = self::$cache_dir . $css_file_name;
if ( file_exists( $css_file ) ) {
unlink( $css_file );
}
@ -270,12 +256,10 @@ class Less_Cache {
unlink( $full_path );
}
}
/**
* Get the list of less files and generated css file from a list file
*
*/
static function ListFiles( $list_file, &$list, &$css_file_name ) {
$list = explode( "\n", file_get_contents( $list_file ) );
@ -283,11 +267,10 @@ class Less_Cache {
// pop the cached name that should match $compiled_name
$css_file_name = array_pop( $list );
if ( !preg_match( '/^' . Less_Cache::$prefix . '[a-f0-9]+\.css$/', $css_file_name ) ) {
if ( !preg_match( '/^' . self::$prefix . '[a-f0-9]+\.css$/', $css_file_name ) ) {
$list[] = $css_file_name;
$css_file_name = false;
}
}
}

View file

@ -1,169 +1,176 @@
<?php
/**
* Utility for css colors
*
* @package Less
* @subpackage color
* @private
*/
class Less_Colors {
public static $colors = array(
'aliceblue' => '#f0f8ff',
'antiquewhite' => '#faebd7',
'aqua' => '#00ffff',
'aquamarine' => '#7fffd4',
'azure' => '#f0ffff',
'beige' => '#f5f5dc',
'bisque' => '#ffe4c4',
'black' => '#000000',
'blanchedalmond' => '#ffebcd',
'blue' => '#0000ff',
'blueviolet' => '#8a2be2',
'brown' => '#a52a2a',
'burlywood' => '#deb887',
'cadetblue' => '#5f9ea0',
'chartreuse' => '#7fff00',
'chocolate' => '#d2691e',
'coral' => '#ff7f50',
'cornflowerblue' => '#6495ed',
'cornsilk' => '#fff8dc',
'crimson' => '#dc143c',
'cyan' => '#00ffff',
'darkblue' => '#00008b',
'darkcyan' => '#008b8b',
'darkgoldenrod' => '#b8860b',
'darkgray' => '#a9a9a9',
'darkgrey' => '#a9a9a9',
'darkgreen' => '#006400',
'darkkhaki' => '#bdb76b',
'darkmagenta' => '#8b008b',
'darkolivegreen' => '#556b2f',
'darkorange' => '#ff8c00',
'darkorchid' => '#9932cc',
'darkred' => '#8b0000',
'darksalmon' => '#e9967a',
'darkseagreen' => '#8fbc8f',
'darkslateblue' => '#483d8b',
'darkslategray' => '#2f4f4f',
'darkslategrey' => '#2f4f4f',
'darkturquoise' => '#00ced1',
'darkviolet' => '#9400d3',
'deeppink' => '#ff1493',
'deepskyblue' => '#00bfff',
'dimgray' => '#696969',
'dimgrey' => '#696969',
'dodgerblue' => '#1e90ff',
'firebrick' => '#b22222',
'floralwhite' => '#fffaf0',
'forestgreen' => '#228b22',
'fuchsia' => '#ff00ff',
'gainsboro' => '#dcdcdc',
'ghostwhite' => '#f8f8ff',
'gold' => '#ffd700',
'goldenrod' => '#daa520',
'gray' => '#808080',
'grey' => '#808080',
'green' => '#008000',
'greenyellow' => '#adff2f',
'honeydew' => '#f0fff0',
'hotpink' => '#ff69b4',
'indianred' => '#cd5c5c',
'indigo' => '#4b0082',
'ivory' => '#fffff0',
'khaki' => '#f0e68c',
'lavender' => '#e6e6fa',
'lavenderblush' => '#fff0f5',
'lawngreen' => '#7cfc00',
'lemonchiffon' => '#fffacd',
'lightblue' => '#add8e6',
'lightcoral' => '#f08080',
'lightcyan' => '#e0ffff',
'lightgoldenrodyellow' => '#fafad2',
'lightgray' => '#d3d3d3',
'lightgrey' => '#d3d3d3',
'lightgreen' => '#90ee90',
'lightpink' => '#ffb6c1',
'lightsalmon' => '#ffa07a',
'lightseagreen' => '#20b2aa',
'lightskyblue' => '#87cefa',
'lightslategray' => '#778899',
'lightslategrey' => '#778899',
'lightsteelblue' => '#b0c4de',
'lightyellow' => '#ffffe0',
'lime' => '#00ff00',
'limegreen' => '#32cd32',
'linen' => '#faf0e6',
'magenta' => '#ff00ff',
'maroon' => '#800000',
'mediumaquamarine' => '#66cdaa',
'mediumblue' => '#0000cd',
'mediumorchid' => '#ba55d3',
'mediumpurple' => '#9370d8',
'mediumseagreen' => '#3cb371',
'mediumslateblue' => '#7b68ee',
'mediumspringgreen' => '#00fa9a',
'mediumturquoise' => '#48d1cc',
'mediumvioletred' => '#c71585',
'midnightblue' => '#191970',
'mintcream' => '#f5fffa',
'mistyrose' => '#ffe4e1',
'moccasin' => '#ffe4b5',
'navajowhite' => '#ffdead',
'navy' => '#000080',
'oldlace' => '#fdf5e6',
'olive' => '#808000',
'olivedrab' => '#6b8e23',
'orange' => '#ffa500',
'orangered' => '#ff4500',
'orchid' => '#da70d6',
'palegoldenrod' => '#eee8aa',
'palegreen' => '#98fb98',
'paleturquoise' => '#afeeee',
'palevioletred' => '#d87093',
'papayawhip' => '#ffefd5',
'peachpuff' => '#ffdab9',
'peru' => '#cd853f',
'pink' => '#ffc0cb',
'plum' => '#dda0dd',
'powderblue' => '#b0e0e6',
'purple' => '#800080',
'red' => '#ff0000',
'rosybrown' => '#bc8f8f',
'royalblue' => '#4169e1',
'saddlebrown' => '#8b4513',
'salmon' => '#fa8072',
'sandybrown' => '#f4a460',
'seagreen' => '#2e8b57',
'seashell' => '#fff5ee',
'sienna' => '#a0522d',
'silver' => '#c0c0c0',
'skyblue' => '#87ceeb',
'slateblue' => '#6a5acd',
'slategray' => '#708090',
'slategrey' => '#708090',
'snow' => '#fffafa',
'springgreen' => '#00ff7f',
'steelblue' => '#4682b4',
'tan' => '#d2b48c',
'teal' => '#008080',
'thistle' => '#d8bfd8',
'tomato' => '#ff6347',
'turquoise' => '#40e0d0',
'violet' => '#ee82ee',
'wheat' => '#f5deb3',
'white' => '#ffffff',
'whitesmoke' => '#f5f5f5',
'yellow' => '#ffff00',
'yellowgreen' => '#9acd32'
);
private const COLORS = [
'aliceblue' => '#f0f8ff',
'antiquewhite' => '#faebd7',
'aqua' => '#00ffff',
'aquamarine' => '#7fffd4',
'azure' => '#f0ffff',
'beige' => '#f5f5dc',
'bisque' => '#ffe4c4',
'black' => '#000000',
'blanchedalmond' => '#ffebcd',
'blue' => '#0000ff',
'blueviolet' => '#8a2be2',
'brown' => '#a52a2a',
'burlywood' => '#deb887',
'cadetblue' => '#5f9ea0',
'chartreuse' => '#7fff00',
'chocolate' => '#d2691e',
'coral' => '#ff7f50',
'cornflowerblue' => '#6495ed',
'cornsilk' => '#fff8dc',
'crimson' => '#dc143c',
'cyan' => '#00ffff',
'darkblue' => '#00008b',
'darkcyan' => '#008b8b',
'darkgoldenrod' => '#b8860b',
'darkgray' => '#a9a9a9',
'darkgrey' => '#a9a9a9',
'darkgreen' => '#006400',
'darkkhaki' => '#bdb76b',
'darkmagenta' => '#8b008b',
'darkolivegreen' => '#556b2f',
'darkorange' => '#ff8c00',
'darkorchid' => '#9932cc',
'darkred' => '#8b0000',
'darksalmon' => '#e9967a',
'darkseagreen' => '#8fbc8f',
'darkslateblue' => '#483d8b',
'darkslategray' => '#2f4f4f',
'darkslategrey' => '#2f4f4f',
'darkturquoise' => '#00ced1',
'darkviolet' => '#9400d3',
'deeppink' => '#ff1493',
'deepskyblue' => '#00bfff',
'dimgray' => '#696969',
'dimgrey' => '#696969',
'dodgerblue' => '#1e90ff',
'firebrick' => '#b22222',
'floralwhite' => '#fffaf0',
'forestgreen' => '#228b22',
'fuchsia' => '#ff00ff',
'gainsboro' => '#dcdcdc',
'ghostwhite' => '#f8f8ff',
'gold' => '#ffd700',
'goldenrod' => '#daa520',
'gray' => '#808080',
'grey' => '#808080',
'green' => '#008000',
'greenyellow' => '#adff2f',
'honeydew' => '#f0fff0',
'hotpink' => '#ff69b4',
'indianred' => '#cd5c5c',
'indigo' => '#4b0082',
'ivory' => '#fffff0',
'khaki' => '#f0e68c',
'lavender' => '#e6e6fa',
'lavenderblush' => '#fff0f5',
'lawngreen' => '#7cfc00',
'lemonchiffon' => '#fffacd',
'lightblue' => '#add8e6',
'lightcoral' => '#f08080',
'lightcyan' => '#e0ffff',
'lightgoldenrodyellow' => '#fafad2',
'lightgray' => '#d3d3d3',
'lightgrey' => '#d3d3d3',
'lightgreen' => '#90ee90',
'lightpink' => '#ffb6c1',
'lightsalmon' => '#ffa07a',
'lightseagreen' => '#20b2aa',
'lightskyblue' => '#87cefa',
'lightslategray' => '#778899',
'lightslategrey' => '#778899',
'lightsteelblue' => '#b0c4de',
'lightyellow' => '#ffffe0',
'lime' => '#00ff00',
'limegreen' => '#32cd32',
'linen' => '#faf0e6',
'magenta' => '#ff00ff',
'maroon' => '#800000',
'mediumaquamarine' => '#66cdaa',
'mediumblue' => '#0000cd',
'mediumorchid' => '#ba55d3',
'mediumpurple' => '#9370d8',
'mediumseagreen' => '#3cb371',
'mediumslateblue' => '#7b68ee',
'mediumspringgreen' => '#00fa9a',
'mediumturquoise' => '#48d1cc',
'mediumvioletred' => '#c71585',
'midnightblue' => '#191970',
'mintcream' => '#f5fffa',
'mistyrose' => '#ffe4e1',
'moccasin' => '#ffe4b5',
'navajowhite' => '#ffdead',
'navy' => '#000080',
'oldlace' => '#fdf5e6',
'olive' => '#808000',
'olivedrab' => '#6b8e23',
'orange' => '#ffa500',
'orangered' => '#ff4500',
'orchid' => '#da70d6',
'palegoldenrod' => '#eee8aa',
'palegreen' => '#98fb98',
'paleturquoise' => '#afeeee',
'palevioletred' => '#d87093',
'papayawhip' => '#ffefd5',
'peachpuff' => '#ffdab9',
'peru' => '#cd853f',
'pink' => '#ffc0cb',
'plum' => '#dda0dd',
'powderblue' => '#b0e0e6',
'purple' => '#800080',
'red' => '#ff0000',
'rosybrown' => '#bc8f8f',
'royalblue' => '#4169e1',
'saddlebrown' => '#8b4513',
'salmon' => '#fa8072',
'sandybrown' => '#f4a460',
'seagreen' => '#2e8b57',
'seashell' => '#fff5ee',
'sienna' => '#a0522d',
'silver' => '#c0c0c0',
'skyblue' => '#87ceeb',
'slateblue' => '#6a5acd',
'slategray' => '#708090',
'slategrey' => '#708090',
'snow' => '#fffafa',
'springgreen' => '#00ff7f',
'steelblue' => '#4682b4',
'tan' => '#d2b48c',
'teal' => '#008080',
'thistle' => '#d8bfd8',
'tomato' => '#ff6347',
'turquoise' => '#40e0d0',
'violet' => '#ee82ee',
'wheat' => '#f5deb3',
'white' => '#ffffff',
'whitesmoke' => '#f5f5f5',
'yellow' => '#ffff00',
'yellowgreen' => '#9acd32',
];
public static function hasOwnProperty( $color ) {
return isset( self::$colors[$color] );
/**
* @param string $color
* @return bool
*/
public static function hasOwnProperty( string $color ): bool {
return isset( self::COLORS[$color] );
}
public static function color( $color ) {
return self::$colors[$color];
/**
* @param string $color Should be an existing color name,
* checked via hasOwnProperty()
* @return string the corresponding hexadecimal representation
*/
public static function color( string $color ): string {
return self::COLORS[$color];
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Configurable
*
* @package Less
* @subpackage Core
* @private
*/
abstract class Less_Configurable {
@ -13,14 +9,14 @@ abstract class Less_Configurable {
*
* @var array
*/
protected $options = array();
protected $options = [];
/**
* Array of default options
*
* @var array
*/
protected $defaultOptions = array();
protected $defaultOptions = [];
/**
* Set options
@ -28,9 +24,7 @@ abstract class Less_Configurable {
* If $options is an object it will be converted into an array by called
* it's toArray method.
*
* @throws Exception
* @param array|object $options
*
*/
public function setOptions( $options ) {
$options = array_intersect_key( $options, $this->defaultOptions );

View file

@ -1,39 +1,35 @@
<?php
/**
* Environment
*
* @package Less
* @subpackage environment
* @private
*/
class Less_Environment {
// public $paths = array(); // option - unmodified - paths to search for imports on
//public static $files = array(); // list of files that have been imported, used for import-once
//public $rootpath; // option - rootpath to append to URL's
//public static $strictImports = null; // option -
//public $insecure; // option - whether to allow imports from insecure ssl hosts
//public $processImports; // option - whether to process imports. if false then imports will not be imported
//public $javascriptEnabled; // option - whether JavaScript is enabled. if undefined, defaults to true
//public $useFileCache; // browser only - whether to use the per file session cache
public $currentFileInfo; // information about the current file - for error reporting and importing and making urls relative etc.
/**
* Information about the current file - for error reporting and importing and making urls relative etc.
*
* - rootpath: rootpath to append to URLs
*
* @var array|null $currentFileInfo;
*/
public $currentFileInfo;
public $importMultiple = false; // whether we are currently importing multiple copies
/* Whether we are currently importing multiple copies */
public $importMultiple = false;
/**
* @var array
*/
public $frames = array();
public $frames = [];
/**
* @var array
*/
public $mediaBlocks = array();
public $mediaBlocks = [];
/**
* @var array
*/
public $mediaPath = array();
public $mediaPath = [];
public static $parensStack = 0;
@ -48,7 +44,7 @@ class Less_Environment {
/**
* @var array
*/
public $functions = array();
public $functions = [];
public function Init() {
self::$parensStack = 0;
@ -58,7 +54,7 @@ class Less_Environment {
if ( Less_Parser::$options['compress'] ) {
Less_Environment::$_outputMap = array(
self::$_outputMap = [
',' => ',',
': ' => ':',
'' => '',
@ -70,11 +66,11 @@ class Less_Environment {
'|' => '|',
'^' => '^',
'^^' => '^^'
);
];
} else {
Less_Environment::$_outputMap = array(
self::$_outputMap = [
',' => ', ',
': ' => ': ',
'' => '',
@ -86,19 +82,19 @@ class Less_Environment {
'|' => '|',
'^' => ' ^ ',
'^^' => ' ^^ '
);
];
}
}
public function copyEvalEnv( $frames = array() ) {
public function copyEvalEnv( $frames = [] ) {
$new_env = new Less_Environment();
$new_env->frames = $frames;
return $new_env;
}
public static function isMathOn() {
return !Less_Parser::$options['strictMath'] || Less_Environment::$parensStack;
return !Less_Parser::$options['strictMath'] || self::$parensStack;
}
public static function isPathRelative( $path ) {
@ -108,15 +104,14 @@ class Less_Environment {
/**
* Canonicalize a path by resolving references to '/./', '/../'
* Does not remove leading "../"
* @param string path or url
* @param string $path or url
* @return string Canonicalized path
*
*/
public static function normalizePath( $path ) {
$segments = explode( '/', $path );
$segments = array_reverse( $segments );
$path = array();
$path = [];
$path_len = 0;
while ( $segments ) {
@ -124,9 +119,10 @@ class Less_Environment {
switch ( $segment ) {
case '.':
break;
break;
case '..':
// @phan-suppress-next-line PhanTypeInvalidDimOffset False positive
if ( !$path_len || ( $path[$path_len - 1] === '..' ) ) {
$path[] = $segment;
$path_len++;
@ -134,12 +130,12 @@ class Less_Environment {
array_pop( $path );
$path_len--;
}
break;
break;
default:
$path[] = $segment;
$path_len++;
break;
break;
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Chunk Exception
*
* @package Less
* @subpackage exception
* @private
*/
class Less_Exception_Chunk extends Less_Exception_Parser {
@ -15,13 +11,11 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
protected $input_len;
/**
* Constructor
*
* @param string $input
* @param Exception $previous Previous exception
* @param integer $index The current parser index
* @param Less_FileInfo|string $currentFile The file
* @param integer $code The exception code
* @param Exception|null $previous Previous exception
* @param int|null $index The current parser index
* @param array|null $currentFile The file
* @param int $code The exception code
*/
public function __construct( $input, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
$this->message = 'ParseError: Unexpected input'; // default message
@ -40,7 +34,6 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
/**
* See less.js chunks()
* We don't actually need the chunks
*
*/
protected function Chunks() {
$level = 0;
@ -95,7 +88,9 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
break;
// \
case 92:
if ( $this->parserCurrentIndex < $this->input_len - 1 ) { $this->parserCurrentIndex++; break;
if ( $this->parserCurrentIndex < $this->input_len - 1 ) {
$this->parserCurrentIndex++;
break;
}
return $this->fail( "unescaped `\\`" );
@ -105,11 +100,12 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
case 96:
$matched = 0;
$currentChunkStartIndex = $this->parserCurrentIndex;
for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
for ( $this->parserCurrentIndex += 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( $cc2 > 96 ) { continue;
}
if ( $cc2 == $cc ) { $matched = 1; break;
if ( $cc2 == $cc ) { $matched = 1;
break;
}
if ( $cc2 == 92 ) { // \
if ( $this->parserCurrentIndex == $this->input_len - 1 ) {
@ -129,15 +125,15 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
$cc2 = $this->CharCode( $this->parserCurrentIndex + 1 );
if ( $cc2 == 47 ) {
// //, find lnfeed
for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
for ( $this->parserCurrentIndex += 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( ( $cc2 <= 13 ) && ( ( $cc2 == 10 ) || ( $cc2 == 13 ) ) ) { break;
}
}
} else if ( $cc2 == 42 ) {
} elseif ( $cc2 == 42 ) {
// /*, find */
$lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex;
for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) {
for ( $this->parserCurrentIndex += 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( $cc2 == 125 ) { $lastMultiCommentEndBrace = $this->parserCurrentIndex;
}
@ -167,7 +163,7 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
} else {
return $this->fail( "missing closing `}`", $lastOpening );
}
} else if ( $parenLevel !== 0 ) {
} elseif ( $parenLevel !== 0 ) {
return $this->fail( "missing closing `)`", $lastParen );
}
@ -186,7 +182,7 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
} else {
$this->index = $index;
}
$this->message = 'ParseError: '.$msg;
$this->message = 'ParseError: ' . $msg;
}
/*

View file

@ -2,9 +2,6 @@
/**
* Compiler Exception
*
* @package Less
* @subpackage exception
*/
class Less_Exception_Compiler extends Less_Exception_Parser {

View file

@ -2,46 +2,36 @@
/**
* Parser Exception
*
* @package Less
* @subpackage exception
*/
class Less_Exception_Parser extends Exception {
/**
* The current file
*
* @var Less_ImportedFile
* @var array
*/
public $currentFile;
/**
* The current parser index
*
* @var integer
* @var int
*/
public $index;
protected $input;
protected $details = array();
protected $details = [];
/**
* Constructor
*
* @param string $message
* @param Exception $previous Previous exception
* @param integer $index The current parser index
* @param Less_FileInfo|string $currentFile The file
* @param integer $code The exception code
* @param string|null $message
* @param Exception|null $previous Previous exception
* @param int|null $index The current parser index
* @param array|null $currentFile The file
* @param int $code The exception code
*/
public function __construct( $message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
if ( PHP_VERSION_ID < 50300 ) {
$this->previous = $previous;
parent::__construct( $message, $code );
} else {
parent::__construct( $message, $code, $previous );
}
parent::__construct( $message, $code, $previous );
$this->currentFile = $currentFile;
$this->index = $index;
@ -56,20 +46,18 @@ class Less_Exception_Parser extends Exception {
}
/**
* Converts the exception to string
*
* @return string
* Set a message based on the exception info
*/
public function genMessage() {
if ( $this->currentFile && $this->currentFile['filename'] ) {
$this->message .= ' in '.basename( $this->currentFile['filename'] );
$this->message .= ' in ' . basename( $this->currentFile['filename'] );
}
if ( $this->index !== null ) {
$this->getInput();
if ( $this->input ) {
$line = self::getLineNumber();
$this->message .= ' on line '.$line.', column '.self::getColumn();
$this->message .= ' on line ' . $line . ', column ' . self::getColumn();
$lines = explode( "\n", $this->input );
@ -78,17 +66,16 @@ class Less_Exception_Parser extends Exception {
$last_line = min( $count, $start_line + 6 );
$num_len = strlen( $last_line );
for ( $i = $start_line; $i < $last_line; $i++ ) {
$this->message .= "\n".str_pad( $i + 1, $num_len, '0', STR_PAD_LEFT ).'| '.$lines[$i];
$this->message .= "\n" . str_pad( (string)( $i + 1 ), $num_len, '0', STR_PAD_LEFT ) . '| ' . $lines[$i];
}
}
}
}
/**
* Returns the line number the error was encountered
*
* @return integer
* @return int
*/
public function getLineNumber() {
if ( $this->index ) {
@ -105,7 +92,7 @@ class Less_Exception_Parser extends Exception {
/**
* Returns the column the error was encountered
*
* @return integer
* @return int
*/
public function getColumn() {
$part = substr( $this->input, 0, $this->index );

View file

@ -2,34 +2,33 @@
/**
* Builtin functions
*
* @package Less
* @subpackage function
* @see http://lesscss.org/functions/
* @see https://lesscss.org/functions/
*/
class Less_Functions {
public $env;
public $currentFileInfo;
function __construct( $env, $currentFileInfo = null ) {
function __construct( $env, array $currentFileInfo = null ) {
$this->env = $env;
$this->currentFileInfo = $currentFileInfo;
}
/**
* @param string $op
* @param float $a
* @param float $b
*/
public static function operate( $op, $a, $b ) {
switch ( $op ) {
case '+':
return $a + $b;
return $a + $b;
case '-':
return $a - $b;
return $a - $b;
case '*':
return $a * $b;
return $a * $b;
case '/':
return $a / $b;
return $a / $b;
}
}
@ -52,7 +51,7 @@ return $a / $b;
public static function number( $n ) {
if ( $n instanceof Less_Tree_Dimension ) {
return floatval( $n->unit->is( '%' ) ? $n->value / 100 : $n->value );
} else if ( is_numeric( $n ) ) {
} elseif ( is_numeric( $n ) ) {
return $n;
} else {
throw new Less_Exception_Compiler( "color functions take numbers as parameters" );
@ -63,20 +62,20 @@ return $a / $b;
if ( $n instanceof Less_Tree_Dimension && $n->unit->is( '%' ) ) {
return (float)$n->value * $size / 100;
} else {
return Less_Functions::number( $n );
return self::number( $n );
}
}
public function rgb( $r = null, $g = null, $b = null ) {
if ( is_null( $r ) || is_null( $g ) || is_null( $b ) ) {
if ( $r === null || $g === null || $b === null ) {
throw new Less_Exception_Compiler( "rgb expects three parameters" );
}
return $this->rgba( $r, $g, $b, 1.0 );
}
public function rgba( $r = null, $g = null, $b = null, $a = null ) {
$rgb = array( $r, $g, $b );
$rgb = array_map( array( 'Less_Functions','scaled' ), $rgb );
$rgb = [ $r, $g, $b ];
$rgb = array_map( [ 'Less_Functions','scaled' ], $rgb );
$a = self::number( $a );
return new Less_Tree_Color( $rgb, $a );
@ -96,18 +95,30 @@ return $a / $b;
$m1 = $l * 2 - $m2;
return $this->rgba( self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
self::hsla_hue( $h, $m1, $m2 ) * 255,
self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255,
$a );
return $this->rgba(
self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
self::hsla_hue( $h, $m1, $m2 ) * 255,
self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255,
$a
);
}
/**
* @param double $h
* @param float $h
* @param float $m1
* @param float $m2
*/
public function hsla_hue( $h, $m1, $m2 ) {
$h = $h < 0 ? $h + 1 : ( $h > 1 ? $h - 1 : $h );
if ( $h * 6 < 1 ) return $m1 + ( $m2 - $m1 ) * $h * 6; else if ( $h * 2 < 1 ) return $m2; else if ( $h * 3 < 2 ) return $m1 + ( $m2 - $m1 ) * ( 2 / 3 - $h ) * 6; else return $m1;
if ( $h * 6 < 1 ) {
return $m1 + ( $m2 - $m1 ) * $h * 6;
} elseif ( $h * 2 < 1 ) {
return $m2;
} elseif ( $h * 3 < 2 ) {
return $m1 + ( $m2 - $m1 ) * ( 2 / 3 - $h ) * 6;
} else {
return $m1;
}
}
public function hsv( $h, $s, $v ) {
@ -115,33 +126,42 @@ return $a / $b;
}
/**
* @param double $a
* @param Less_Tree|float $h
* @param Less_Tree|float $s
* @param Less_Tree|float $v
* @param float $a
*/
public function hsva( $h, $s, $v, $a ) {
$h = ( ( Less_Functions::number( $h ) % 360 ) / 360 ) * 360;
$s = Less_Functions::number( $s );
$v = Less_Functions::number( $v );
$a = Less_Functions::number( $a );
$h = ( ( self::number( $h ) % 360 ) / 360 ) * 360;
$s = self::number( $s );
$v = self::number( $v );
$a = self::number( $a );
$i = floor( ( $h / 60 ) % 6 );
$i = floor( (int)( $h / 60 ) % 6 );
$f = ( $h / 60 ) - $i;
$vs = array( $v,
$v * ( 1 - $s ),
$v * ( 1 - $f * $s ),
$v * ( 1 - ( 1 - $f ) * $s ) );
$vs = [
$v,
$v * ( 1 - $s ),
$v * ( 1 - $f * $s ),
$v * ( 1 - ( 1 - $f ) * $s )
];
$perm = array( array( 0, 3, 1 ),
array( 2, 0, 1 ),
array( 1, 0, 3 ),
array( 1, 2, 0 ),
array( 3, 1, 0 ),
array( 0, 1, 2 ) );
$perm = [
[ 0, 3, 1 ],
[ 2, 0, 1 ],
[ 1, 0, 3 ],
[ 1, 2, 0 ],
[ 3, 1, 0 ],
[ 0, 1, 2 ]
];
return $this->rgba( $vs[$perm[$i][0]] * 255,
$vs[$perm[$i][1]] * 255,
$vs[$perm[$i][2]] * 255,
$a );
return $this->rgba(
$vs[$perm[$i][0]] * 255,
$vs[$perm[$i][1]] * 255,
$vs[$perm[$i][2]] * 255,
$a
);
}
public function hue( $color = null ) {
@ -275,7 +295,8 @@ return $a / $b;
}
/**
* @param Less_Tree_Dimension $amount
* @param Less_Tree_Color|null $color
* @param Less_Tree_Dimension|null $amount
*/
public function desaturate( $color = null, $amount = null ) {
if ( !$color instanceof Less_Tree_Color ) {
@ -286,7 +307,6 @@ return $a / $b;
}
$hsl = $color->toHSL();
$hsl['s'] -= $amount->value / 100;
$hsl['s'] = self::clamp( $hsl['s'] );
@ -385,11 +405,13 @@ return $a / $b;
//
// Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
// http://sass-lang.com
// https://sass-lang.com/
//
/**
* @param Less_Tree_Color $color1
* @param Less_Tree|null $color1
* @param Less_Tree|null $color2
* @param Less_Tree|null $weight
*/
public function mix( $color1 = null, $color2 = null, $weight = null ) {
if ( !$color1 instanceof Less_Tree_Color ) {
@ -414,9 +436,11 @@ return $a / $b;
$w1 = ( ( ( ( $w * $a ) == -1 ) ? $w : ( $w + $a ) / ( 1 + $w * $a ) ) + 1 ) / 2;
$w2 = 1 - $w1;
$rgb = array( $color1->rgb[0] * $w1 + $color2->rgb[0] * $w2,
$color1->rgb[1] * $w1 + $color2->rgb[1] * $w2,
$color1->rgb[2] * $w1 + $color2->rgb[2] * $w2 );
$rgb = [
$color1->rgb[0] * $w1 + $color2->rgb[0] * $w2,
$color1->rgb[1] * $w1 + $color2->rgb[1] * $w2,
$color1->rgb[2] * $w1 + $color2->rgb[2] * $w2
];
$alpha = $color1->alpha * $p + $color2->alpha * ( 1 - $p );
@ -456,7 +480,7 @@ return $a / $b;
if ( !$threshold ) {
$threshold = 0.43;
} else {
$threshold = Less_Functions::number( $threshold );
$threshold = self::number( $threshold );
}
if ( $color->luma() < $threshold ) {
@ -474,7 +498,7 @@ return $a / $b;
}
public function escape( $str ) {
$revert = array( '%21' => '!', '%2A' => '*', '%27' => "'",'%3F' => '?','%26' => '&','%2C' => ',','%2F' => '/','%40' => '@','%2B' => '+','%24' => '$' );
$revert = [ '%21' => '!', '%2A' => '*', '%27' => "'",'%3F' => '?','%26' => '&','%2C' => ',','%2F' => '/','%40' => '@','%2B' => '+','%24' => '$' ];
return new Less_Tree_Anonymous( strtr( rawurlencode( $str->value ), $revert ) );
}
@ -486,7 +510,7 @@ return $a / $b;
public function replace( $string, $pattern, $replacement, $flags = null ) {
$result = $string->value;
$expr = '/'.str_replace( '/', '\\/', $pattern->value ).'/';
$expr = '/' . str_replace( '/', '\\/', $pattern->value ) . '/';
if ( $flags && $flags->value ) {
$expr .= self::replace_flags( $flags->value );
}
@ -507,11 +531,11 @@ return $a / $b;
switch ( $flag ) {
case 'e':
case 'g':
break;
break;
default:
$new_flags .= $flag;
break;
break;
}
}
@ -642,7 +666,7 @@ return $a / $b;
}
$args[0] = (float)$args[0]->value;
return new Less_Tree_Dimension( call_user_func_array( $fn, $args ), $unit );
} else if ( is_numeric( $args[0] ) ) {
} elseif ( is_numeric( $args[0] ) ) {
return call_user_func_array( $fn, $args );
} else {
throw new Less_Exception_Compiler( "math functions take numbers as parameters" );
@ -650,7 +674,8 @@ return $a / $b;
}
/**
* @param boolean $isMin
* @param bool $isMin
* @param array<Less_Tree> $args
*/
private function _minmax( $isMin, $args ) {
$arg_count = count( $args );
@ -663,18 +688,24 @@ return $a / $b;
$unitClone = null;
$unitStatic = null;
$order = array(); // elems only contains original argument values.
$values = array(); // key is the unit.toString() for unified tree.Dimension values,
// value is the index into the order array.
// elems only contains original argument values.
$order = [];
// key is the unit.toString() for unified tree.Dimension values,
// value is the index into the order array.
$values = [];
for ( $i = 0; $i < $arg_count; $i++ ) {
$current = $args[$i];
if ( !( $current instanceof Less_Tree_Dimension ) ) {
if ( is_array( $args[$i]->value ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
if ( property_exists( $args[$i], 'value' ) && is_array( $args[$i]->value ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
$args[] = $args[$i]->value;
}
continue;
}
// PhanTypeInvalidDimOffset -- False positive, safe after continue or non-first iterations
'@phan-var non-empty-list<Less_Tree_Dimension> $order';
if ( $current->unit->toString() === '' && !$unitClone ) {
$temp = new Less_Tree_Dimension( $current->value, $unitClone );
@ -725,11 +756,11 @@ return $a / $b;
if ( count( $order ) == 1 ) {
return $order[0];
}
$args = array();
$args = [];
foreach ( $order as $a ) {
$args[] = $a->toCSS( $this->env );
$args[] = $a->toCSS();
}
return new Less_Tree_Anonymous( ( $isMin ? 'min(' : 'max(' ) . implode( Less_Environment::$_outputMap[','], $args ).')' );
return new Less_Tree_Anonymous( ( $isMin ? 'min(' : 'max(' ) . implode( Less_Environment::$_outputMap[','], $args ) . ')' );
}
public function min() {
@ -807,10 +838,12 @@ return $a / $b;
}
/**
* @param string $unit
* @param Less_Tree $n
* @param Less_Tree|string $unit
*/
public function isunit( $n, $unit ) {
if ( is_object( $unit ) && property_exists( $unit, 'value' ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
$unit = $unit->value;
}
@ -818,6 +851,7 @@ return $a / $b;
}
/**
* @param Less_Tree $n
* @param string $type
*/
private function _isa( $n, $type ) {
@ -866,15 +900,16 @@ return $a / $b;
$filePath = str_replace( '\\', '/', $filePath );
if ( Less_Environment::isPathRelative( $filePath ) ) {
$currentFileInfo = $this->currentFileInfo;
'@phan-var array $currentFileInfo';
if ( Less_Parser::$options['relativeUrls'] ) {
$temp = $this->currentFileInfo['currentDirectory'];
$temp = $currentFileInfo['currentDirectory'];
} else {
$temp = $this->currentFileInfo['entryPath'];
$temp = $currentFileInfo['entryPath'];
}
if ( !empty( $temp ) ) {
$filePath = Less_Environment::normalizePath( rtrim( $temp, '/' ).'/'.$filePath );
$filePath = Less_Environment::normalizePath( rtrim( $temp, '/' ) . '/' . $filePath );
}
}
@ -895,7 +930,7 @@ return $a / $b;
$mimetype = Less_Mime::lookup( $filePath );
$charset = Less_Mime::charsets_lookup( $mimetype );
$useBase64 = !in_array( $charset, array( 'US-ASCII', 'UTF-8' ) );
$useBase64 = !in_array( $charset, [ 'US-ASCII', 'UTF-8' ] );
if ( $useBase64 ) { $mimetype .= ';base64';
}
@ -914,8 +949,8 @@ return $a / $b;
$DATA_URI_MAX_KB = 32;
$fileSizeInKB = round( strlen( $buf ) / 1024 );
if ( $fileSizeInKB >= $DATA_URI_MAX_KB ) {
$url = new Less_Tree_Url( ( $filePathNode ? $filePathNode : $mimetypeNode ), $this->currentFileInfo );
return $url->compile( $this );
$url = new Less_Tree_Url( ( $filePathNode ?: $mimetypeNode ), $this->currentFileInfo );
return $url->compile( $this->env );
}
if ( $buf ) {
@ -994,9 +1029,9 @@ return $a / $b;
$returner .= '</' . $gradientType . 'Gradient><rect ' . $rectangleDimension . ' fill="url(#gradient)" /></svg>';
if ( $useBase64 ) {
$returner = "'data:image/svg+xml;base64,".base64_encode( $returner )."'";
$returner = "'data:image/svg+xml;base64," . base64_encode( $returner ) . "'";
} else {
$returner = "'data:image/svg+xml,".$returner."'";
$returner = "'data:image/svg+xml," . $returner . "'";
}
return new Less_Tree_URL( new Less_Tree_Anonymous( $returner ) );
@ -1009,17 +1044,18 @@ return $a / $b;
* @return string The encoded string
*/
public static function encodeURIComponent( $string ) {
$revert = array( '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' );
$revert = [ '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' ];
return strtr( rawurlencode( $string ), $revert );
}
// Color Blending
// ref: http://www.w3.org/TR/compositing-1
// ref: https://www.w3.org/TR/compositing-1/
public function colorBlend( $mode, $color1, $color2 ) {
$ab = $color1->alpha; // backdrop
$as = $color2->alpha; // source
$r = array(); // result
// backdrop
$ab = $color1->alpha;
// source
$as = $color2->alpha;
$result = [];
$ar = $as + $ab * ( 1 - $as );
for ( $i = 0; $i < 3; $i++ ) {
@ -1029,10 +1065,10 @@ return $a / $b;
if ( $ar ) {
$cr = ( $as * $cs + $ab * ( $cb - $as * ( $cb + $cs - $cr ) ) ) / $ar;
}
$r[$i] = $cr * 255;
$result[$i] = $cr * 255;
}
return new Less_Tree_Color( $r, $ar );
return new Less_Tree_Color( $result, $ar );
}
public function multiply( $color1 = null, $color2 = null ) {
@ -1043,7 +1079,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to multiply must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendMultiply' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendMultiply' ], $color1, $color2 );
}
private function colorBlendMultiply( $cb, $cs ) {
@ -1058,7 +1094,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to screen must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendScreen' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendScreen' ], $color1, $color2 );
}
private function colorBlendScreen( $cb, $cs ) {
@ -1073,7 +1109,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to overlay must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendOverlay' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendOverlay' ], $color1, $color2 );
}
private function colorBlendOverlay( $cb, $cs ) {
@ -1091,7 +1127,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to softlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendSoftlight' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendSoftlight' ], $color1, $color2 );
}
private function colorBlendSoftlight( $cb, $cs ) {
@ -1113,7 +1149,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to hardlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendHardlight' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendHardlight' ], $color1, $color2 );
}
private function colorBlendHardlight( $cb, $cs ) {
@ -1128,7 +1164,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to difference must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendDifference' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendDifference' ], $color1, $color2 );
}
private function colorBlendDifference( $cb, $cs ) {
@ -1143,7 +1179,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to exclusion must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendExclusion' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendExclusion' ], $color1, $color2 );
}
private function colorBlendExclusion( $cb, $cs ) {
@ -1158,7 +1194,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to average must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendAverage' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendAverage' ], $color1, $color2 );
}
// non-w3c functions:
@ -1174,7 +1210,7 @@ return $a / $b;
throw new Less_Exception_Compiler( 'The second argument to negation must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
}
return $this->colorBlend( array( $this,'colorBlendNegation' ), $color1, $color2 );
return $this->colorBlend( [ $this,'colorBlendNegation' ], $color1, $color2 );
}
public function colorBlendNegation( $cb, $cs ) {

View file

@ -1,37 +1,32 @@
<?php
/**
* Mime lookup
*
* @package Less
* @subpackage node
* @private
*/
class Less_Mime {
// this map is intentionally incomplete
// if you want more, install 'mime' dep
static $_types = array(
'.htm' => 'text/html',
'.html' => 'text/html',
'.gif' => 'image/gif',
'.jpg' => 'image/jpeg',
'.jpeg' => 'image/jpeg',
'.png' => 'image/png',
'.ttf' => 'application/x-font-ttf',
'.otf' => 'application/x-font-otf',
'.eot' => 'application/vnd.ms-fontobject',
'.woff' => 'application/x-font-woff',
'.svg' => 'image/svg+xml',
);
private static $types = [
'.htm' => 'text/html',
'.html' => 'text/html',
'.gif' => 'image/gif',
'.jpg' => 'image/jpeg',
'.jpeg' => 'image/jpeg',
'.png' => 'image/png',
'.ttf' => 'application/x-font-ttf',
'.otf' => 'application/x-font-otf',
'.eot' => 'application/vnd.ms-fontobject',
'.woff' => 'application/x-font-woff',
'.svg' => 'image/svg+xml',
];
public static function lookup( $filepath ) {
$parts = explode( '.', $filepath );
$ext = '.'.strtolower( array_pop( $parts ) );
$ext = '.' . strtolower( array_pop( $parts ) );
if ( !isset( self::$_types[$ext] ) ) {
return null;
}
return self::$_types[$ext];
return self::$types[$ext] ?? null;
}
public static function charsets_lookup( $type = null ) {

View file

@ -1,26 +1,24 @@
<?php
/**
* Parser output
*
* @package Less
* @subpackage output
* @private
*/
class Less_Output {
/**
* Output holder
*
* @var string
* @var string[]
*/
protected $strs = array();
protected $strs = [];
/**
* Adds a chunk to the stack
*
* @param string $chunk The chunk to output
* @param Less_FileInfo $fileInfo The file information
* @param integer $index The index
* @param array|null $fileInfo The file information
* @param int $index The index
* @param mixed $mapLines
*/
public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
@ -30,7 +28,7 @@ class Less_Output {
/**
* Is the output empty?
*
* @return boolean
* @return bool
*/
public function isEmpty() {
return count( $this->strs ) === 0;

View file

@ -1,10 +1,8 @@
<?php
/**
* Parser output with source map
*
* @package Less
* @subpackage Output
* @private
*/
class Less_Output_Mapped extends Less_Output {
@ -18,14 +16,14 @@ class Less_Output_Mapped extends Less_Output {
/**
* Current line
*
* @var integer
* @var int
*/
protected $lineNumber = 0;
/**
* Current column
*
* @var integer
* @var int
*/
protected $column = 0;
@ -34,7 +32,7 @@ class Less_Output_Mapped extends Less_Output {
*
* @var array
*/
protected $contentsMap = array();
protected $contentsMap = [];
/**
* Constructor
@ -52,8 +50,8 @@ class Less_Output_Mapped extends Less_Output {
* The $index for less.php may be different from less.js since less.php does not chunkify inputs
*
* @param string $chunk
* @param string $fileInfo
* @param integer $index
* @param array|null $fileInfo
* @param int $index
* @param mixed $mapLines
*/
public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
@ -62,7 +60,7 @@ class Less_Output_Mapped extends Less_Output {
return;
}
$sourceLines = array();
$sourceLines = [];
$sourceColumns = ' ';
if ( $fileInfo ) {
@ -74,7 +72,7 @@ class Less_Output_Mapped extends Less_Output {
$sourceLines = explode( "\n", $inputSource );
$sourceColumns = end( $sourceLines );
} else {
throw new Exception( 'Filename '.$url.' not in contentsMap' );
throw new Exception( 'Filename ' . $url . ' not in contentsMap' );
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,31 +1,29 @@
<?php
/**
* Encode / Decode Base64 VLQ.
*
* @package Less
* @subpackage SourceMap
* @private
*/
class Less_SourceMap_Base64VLQ {
/**
* Shift
*
* @var integer
* @var int
*/
private $shift = 5;
/**
* Mask
*
* @var integer
* @var int
*/
private $mask = 0x1F; // == (1 << shift) == 0b00011111
/**
* Continuation bit
*
* @var integer
* @var int
*/
private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000
@ -34,7 +32,7 @@ class Less_SourceMap_Base64VLQ {
*
* @var array
*/
private $charToIntMap = array(
private $charToIntMap = [
'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
'H' => 7,'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
@ -44,14 +42,14 @@ class Less_SourceMap_Base64VLQ {
'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48,
'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56,
5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63,
);
];
/**
* Integer to char map
*
* @var array
*/
private $intToCharMap = array(
private $intToCharMap = [
0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G',
7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N',
14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U',
@ -62,7 +60,7 @@ class Less_SourceMap_Base64VLQ {
49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3',
56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+',
63 => '/',
);
];
/**
* Constructor
@ -83,7 +81,7 @@ class Less_SourceMap_Base64VLQ {
* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
* We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297,
* even on a 64 bit machine.
* @param string $aValue
* @param int $aValue
*/
public function toVLQSigned( $aValue ) {
return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 );
@ -98,7 +96,7 @@ class Less_SourceMap_Base64VLQ {
* Hence
* 1 becomes -2147483648
* even on a 64 bit machine.
* @param integer $aValue
* @param int $aValue
*/
public function fromVLQSigned( $aValue ) {
return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 );
@ -107,7 +105,7 @@ class Less_SourceMap_Base64VLQ {
/**
* Return the base 64 VLQ encoded value.
*
* @param string $aValue The value to encode
* @param int $aValue The value to encode
* @return string The encoded value
*/
public function encode( $aValue ) {
@ -130,7 +128,7 @@ class Less_SourceMap_Base64VLQ {
* Return the value decoded from base 64 VLQ.
*
* @param string $encoded The encoded value to decode
* @return integer The decoded value
* @return int The decoded value
*/
public function decode( $encoded ) {
$vlq = 0;
@ -148,9 +146,9 @@ class Less_SourceMap_Base64VLQ {
/**
* Right shift with zero fill.
*
* @param integer $a number to shift
* @param integer $b number of bits to shift
* @return integer
* @param int $a number to shift
* @param int $b number of bits to shift
* @return int
*/
public function zeroFill( $a, $b ) {
return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) );
@ -159,13 +157,13 @@ class Less_SourceMap_Base64VLQ {
/**
* Encode single 6-bit digit as base64.
*
* @param integer $number
* @param int $number
* @return string
* @throws Exception If the number is invalid
*/
public function base64Encode( $number ) {
if ( $number < 0 || $number > 63 ) {
throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', $number ) );
throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', (string)$number ) );
}
return $this->intToCharMap[$number];
}
@ -174,7 +172,7 @@ class Less_SourceMap_Base64VLQ {
* Decode single 6-bit digit from base64
*
* @param string $char
* @return number
* @return int
* @throws Exception If the number is invalid
*/
public function base64Decode( $char ) {

View file

@ -1,10 +1,8 @@
<?php
/**
* Source map generator
*
* @package Less
* @subpackage Output
* @private
*/
class Less_SourceMap_Generator extends Less_Configurable {
@ -18,7 +16,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
*
* @var array
*/
protected $defaultOptions = array(
protected $defaultOptions = [
// an optional source root, useful for relocating source files
// on a server or removing repeated values in the 'sources' entry.
// This value is prepended to the individual entries in the 'source' field.
@ -41,7 +39,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
// base path for filename normalization
'sourceMapBasepath' => ''
);
];
/**
* The base64 VLQ encoder
@ -55,7 +53,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
*
* @var array
*/
protected $mappings = array();
protected $mappings = [];
/**
* The root node
@ -69,23 +67,24 @@ class Less_SourceMap_Generator extends Less_Configurable {
*
* @var array
*/
protected $contentsMap = array();
protected $contentsMap = [];
/**
* File to content map
*
* @var array
*/
protected $sources = array();
protected $source_keys = array();
protected $sources = [];
protected $source_keys = [];
/**
* Constructor
*
* @param Less_Tree_Ruleset $root The root node
* @param array $contentsMap
* @param array $options Array of options
*/
public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = array() ) {
public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = [] ) {
$this->root = $root;
$this->contentsMap = $contentsMap;
$this->encoder = new Less_SourceMap_Base64VLQ();
@ -182,20 +181,20 @@ class Less_SourceMap_Generator extends Less_Configurable {
/**
* Adds a mapping
*
* @param integer $generatedLine The line number in generated file
* @param integer $generatedColumn The column number in generated file
* @param integer $originalLine The line number in original file
* @param integer $originalColumn The column number in original file
* @param string $sourceFile The original source file
* @param int $generatedLine The line number in generated file
* @param int $generatedColumn The column number in generated file
* @param int $originalLine The line number in original file
* @param int $originalColumn The column number in original file
* @param array $fileInfo The original source file
*/
public function addMapping( $generatedLine, $generatedColumn, $originalLine, $originalColumn, $fileInfo ) {
$this->mappings[] = array(
$this->mappings[] = [
'generated_line' => $generatedLine,
'generated_column' => $generatedColumn,
'original_line' => $originalLine,
'original_column' => $originalColumn,
'source_file' => $fileInfo['currentUri']
);
];
$this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
}
@ -207,7 +206,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#
*/
protected function generateJson() {
$sourceMap = array();
$sourceMap = [];
$mappings = $this->generateMappings();
// File version (always the first entry in the object) and must be a positive integer.
@ -226,13 +225,13 @@ class Less_SourceMap_Generator extends Less_Configurable {
}
// A list of original sources used by the 'mappings' entry.
$sourceMap['sources'] = array();
$sourceMap['sources'] = [];
foreach ( $this->sources as $source_uri => $source_filename ) {
$sourceMap['sources'][] = $this->normalizeFilename( $source_filename );
}
// A list of symbol names used by the 'mappings' entry.
$sourceMap['names'] = array();
$sourceMap['names'] = [];
// A string with the encoded mapping data.
$sourceMap['mappings'] = $mappings;
@ -261,7 +260,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
if ( empty( $this->sources ) ) {
return;
}
$content = array();
$content = [];
foreach ( $this->sources as $sourceFile ) {
$content[] = file_get_contents( $sourceFile );
}
@ -281,7 +280,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
$this->source_keys = array_flip( array_keys( $this->sources ) );
// group mappings by generated line number.
$groupedMap = $groupedMapEncoded = array();
$groupedMap = $groupedMapEncoded = [];
foreach ( $this->mappings as $m ) {
$groupedMap[$m['generated_line']][] = $m;
}
@ -294,7 +293,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
$groupedMapEncoded[] = ';';
}
$lineMapEncoded = array();
$lineMapEncoded = [];
$lastGeneratedColumn = 0;
foreach ( $line_map as $m ) {
@ -330,7 +329,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* Finds the index for the filename
*
* @param string $filename
* @return integer|false
* @return int|false
*/
protected function findFileIndex( $filename ) {
return $this->source_keys[$filename];
@ -339,6 +338,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
/**
* fix windows paths
* @param string $path
* @param bool $addEndSlash
* @return string
*/
public function fixWindowsPath( $path, $addEndSlash = false ) {

View file

@ -3,12 +3,18 @@
/**
* Tree
*
* @package Less
* @subpackage tree
* TODO: Callers often use `property_exists(, 'value')` to distinguish
* tree nodes that are considerd value-holding. Refactor this to move
* the 'value' property that most subclasses implement to there, and use
* something else (special value, method, or intermediate class?) to
* signal whether a subclass is considered value-holding.
*/
class Less_Tree {
public $cache_string;
public $parensInOp = false;
public $extendOnEveryPath;
public $allExtends;
public function toCSS() {
$output = new Less_Output();
@ -25,7 +31,12 @@ class Less_Tree {
public function genCSS( $output ) {
}
public function compile( $env ) {
return $this;
}
/**
* @param Less_Output $output
* @param Less_Tree_Ruleset[] $rules
*/
public static function outputRuleset( $output, $rules ) {
@ -45,8 +56,8 @@ class Less_Tree {
}
// Non-compressed
$tabSetStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
$tabRuleStr = $tabSetStr.Less_Parser::$options['indentation'];
$tabSetStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
$tabRuleStr = $tabSetStr . Less_Parser::$options['indentation'];
$output->add( " {" );
for ( $i = 0; $i < $ruleCnt; $i++ ) {
@ -54,8 +65,7 @@ class Less_Tree {
$rules[$i]->genCSS( $output );
}
Less_Environment::$tabLevel--;
$output->add( $tabSetStr.'}' );
$output->add( $tabSetStr . '}' );
}
public function accept( $visitor ) {
@ -64,6 +74,7 @@ class Less_Tree {
public static function ReferencedArray( $rules ) {
foreach ( $rules as $rule ) {
if ( method_exists( $rule, 'markReferenced' ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
$rule->markReferenced();
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Alpha
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Alpha extends Less_Tree {
public $value;

View file

@ -1,10 +1,6 @@
<?php
/**
* Anonymous
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Anonymous extends Less_Tree {
public $value;
@ -15,8 +11,8 @@ class Less_Tree_Anonymous extends Less_Tree {
public $type = 'Anonymous';
/**
* @param integer $index
* @param boolean $mapLines
* @param int $index
* @param bool|null $mapLines
*/
public function __construct( $value, $index = null, $currentFileInfo = null, $mapLines = null ) {
$this->value = $value;
@ -25,7 +21,7 @@ class Less_Tree_Anonymous extends Less_Tree {
$this->currentFileInfo = $currentFileInfo;
}
public function compile() {
public function compile( $env ) {
return new Less_Tree_Anonymous( $this->value, $this->index, $this->currentFileInfo, $this->mapLines );
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Assignment
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Assignment extends Less_Tree {

View file

@ -1,10 +1,6 @@
<?php
/**
* Attribute
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Attribute extends Less_Tree {

View file

@ -1,10 +1,6 @@
<?php
/**
* Call
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Call extends Less_Tree {
public $value;
@ -39,7 +35,7 @@ class Less_Tree_Call extends Less_Tree {
// The function should receive the value, not the variable.
//
public function compile( $env = null ) {
$args = array();
$args = [];
foreach ( $this->args as $a ) {
$args[] = $a->compile( $env );
}
@ -48,41 +44,44 @@ class Less_Tree_Call extends Less_Tree {
switch ( $nameLC ) {
case '%':
$nameLC = '_percent';
break;
break;
case 'get-unit':
$nameLC = 'getunit';
break;
break;
case 'data-uri':
$nameLC = 'datauri';
break;
break;
case 'svg-gradient':
$nameLC = 'svggradient';
break;
break;
}
$result = null;
if ( $nameLC === 'default' ) {
$result = Less_Tree_DefaultFunc::compile();
} else {
if ( method_exists( 'Less_Functions', $nameLC ) ) { // 1.
try {
$func = new Less_Functions( $env, $this->currentFileInfo );
$result = call_user_func_array( array( $func,$nameLC ), $args );
} catch ( Exception $e ) {
throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index );
}
$func = null;
if ( method_exists( 'Less_Functions', $nameLC ) ) {
$functions = new Less_Functions( $env, $this->currentFileInfo );
$func = [ $functions, $nameLC ];
} elseif ( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
$func = $env->functions[$nameLC];
}
// If the function name isn't known to LESS, output it unchanged as CSS.
if ( $func ) {
try {
$result = call_user_func_array( $env->functions[$nameLC], $args );
$result = call_user_func_array( $func, $args );
} catch ( Exception $e ) {
throw new Less_Exception_Compiler( 'error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index );
// Preserve original trace, especially from custom functions.
// https://github.com/wikimedia/less.php/issues/38
throw new Less_Exception_Compiler(
'error evaluating function `' . $this->name . '` ' . $e->getMessage()
. ' index: ' . $this->index,
$e
);
}
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Color
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Color extends Less_Tree {
public $rgb;
@ -20,25 +16,21 @@ class Less_Tree_Color extends Less_Tree {
return;
}
$this->rgb = array();
$this->rgb = [];
if ( is_array( $rgb ) ) {
$this->rgb = $rgb;
} else if ( strlen( $rgb ) == 6 ) {
} elseif ( strlen( $rgb ) == 6 ) {
foreach ( str_split( $rgb, 2 ) as $c ) {
$this->rgb[] = hexdec( $c );
}
} else {
foreach ( str_split( $rgb, 1 ) as $c ) {
$this->rgb[] = hexdec( $c.$c );
$this->rgb[] = hexdec( $c . $c );
}
}
$this->alpha = is_numeric( $a ) ? $a : 1;
}
public function compile() {
return $this;
}
public function luma() {
$r = $this->rgb[0] / 255;
$g = $this->rgb[1] / 255;
@ -73,7 +65,7 @@ class Less_Tree_Color extends Less_Tree {
return 'transparent';
}
$values = array();
$values = [];
foreach ( $this->rgb as $c ) {
$values[] = Less_Functions::clamp( round( $c ), 255 );
}
@ -89,7 +81,7 @@ class Less_Tree_Color extends Less_Tree {
// Convert color to short format
if ( $color[1] === $color[2] && $color[3] === $color[4] && $color[5] === $color[6] ) {
$color = '#'.$color[1] . $color[3] . $color[5];
$color = '#' . $color[1] . $color[3] . $color[5];
}
}
@ -108,7 +100,7 @@ class Less_Tree_Color extends Less_Tree {
* @param string $op
*/
public function operate( $op, $other ) {
$rgb = array();
$rgb = [];
$alpha = $this->alpha * ( 1 - $other->alpha ) + $other->alpha;
for ( $c = 0; $c < 3; $c++ ) {
$rgb[$c] = Less_Functions::operate( $op, $this->rgb[$c], $other->rgb[$c] );
@ -136,16 +128,19 @@ class Less_Tree_Color extends Less_Tree {
$s = $l > 0.5 ? $d / ( 2 - $max - $min ) : $d / ( $max + $min );
switch ( $max ) {
case $r: $h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 );
break;
case $g: $h = ( $b - $r ) / $d + 2;
break;
case $b: $h = ( $r - $g ) / $d + 4;
break;
case $r:
$h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 );
break;
case $g:
$h = ( $b - $r ) / $d + 2;
break;
case $b:
$h = ( $r - $g ) / $d + 4;
break;
}
$h /= 6;
}
return array( 'h' => $h * 360, 's' => $s, 'l' => $l, 'a' => $a );
return [ 'h' => $h * 360, 's' => $s, 'l' => $l, 'a' => $a ];
}
// Adapted from http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
@ -170,16 +165,19 @@ break;
$h = 0;
if ( $max !== $min ) {
switch ( $max ) {
case $r: $h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 );
break;
case $g: $h = ( $b - $r ) / $d + 2;
break;
case $b: $h = ( $r - $g ) / $d + 4;
break;
case $r:
$h = ( $g - $b ) / $d + ( $g < $b ? 6 : 0 );
break;
case $g:
$h = ( $b - $r ) / $d + 2;
break;
case $b:
$h = ( $r - $g ) / $d + 4;
break;
}
$h /= 6;
}
return array( 'h' => $h * 360, 's' => $s, 'v' => $v, 'a' => $a );
return [ 'h' => $h * 360, 's' => $s, 'v' => $v, 'a' => $a ];
}
public function toARGB() {
@ -223,7 +221,7 @@ break;
}
if ( $keyword === 'transparent' ) {
return new Less_Tree_Color( array( 0, 0, 0 ), 0, true );
return new Less_Tree_Color( [ 0, 0, 0 ], 0, true );
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Comment
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Comment extends Less_Tree {
@ -16,7 +12,7 @@ class Less_Tree_Comment extends Less_Tree {
public function __construct( $value, $silent, $index = null, $currentFileInfo = null ) {
$this->value = $value;
$this->silent = !!$silent;
$this->silent = (bool)$silent;
$this->currentFileInfo = $currentFileInfo;
}
@ -40,10 +36,6 @@ class Less_Tree_Comment extends Less_Tree {
return $this->silent || $isReference || $isCompressed;
}
public function compile() {
return $this;
}
public function markReferenced() {
$this->isReferenced = true;
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Condition
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Condition extends Less_Tree {
@ -35,11 +31,11 @@ class Less_Tree_Condition extends Less_Tree {
switch ( $this->op ) {
case 'and':
$result = $a && $b;
break;
break;
case 'or':
$result = $a || $b;
break;
break;
default:
if ( Less_Parser::is_method( $a, 'compare' ) ) {
@ -53,17 +49,17 @@ class Less_Tree_Condition extends Less_Tree {
switch ( $result ) {
case -1:
$result = $this->op === '<' || $this->op === '=<' || $this->op === '<=';
break;
break;
case 0:
case 0:
$result = $this->op === '=' || $this->op === '>=' || $this->op === '=<' || $this->op === '<=';
break;
break;
case 1:
case 1:
$result = $this->op === '>' || $this->op === '>=';
break;
break;
}
break;
break;
}
return $this->negate ? !$result : $result;

View file

@ -1,10 +1,6 @@
<?php
/**
* DefaultFunc
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_DefaultFunc {

View file

@ -1,10 +1,6 @@
<?php
/**
* DetachedRuleset
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_DetachedRuleset extends Less_Tree {

View file

@ -1,10 +1,6 @@
<?php
/**
* Dimension
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Dimension extends Less_Tree {
@ -18,7 +14,7 @@ class Less_Tree_Dimension extends Less_Tree {
if ( $unit && ( $unit instanceof Less_Tree_Unit ) ) {
$this->unit = $unit;
} elseif ( $unit ) {
$this->unit = new Less_Tree_Unit( array( $unit ) );
$this->unit = new Less_Tree_Unit( [ $unit ] );
} else {
$this->unit = new Less_Tree_Unit();
}
@ -28,12 +24,8 @@ class Less_Tree_Dimension extends Less_Tree {
$this->unit = $visitor->visitObj( $this->unit );
}
public function compile() {
return $this;
}
public function toColor() {
return new Less_Tree_Color( array( $this->value, $this->value, $this->value ) );
return new Less_Tree_Color( [ $this->value, $this->value, $this->value ] );
}
/**
@ -41,7 +33,7 @@ class Less_Tree_Dimension extends Less_Tree {
*/
public function genCSS( $output ) {
if ( Less_Parser::$options['strictUnits'] && !$this->unit->isSingular() ) {
throw new Less_Exception_Compiler( "Multiple units in dimension. Correct the units or use the unit function. Bad unit: ".$this->unit->toString() );
throw new Less_Exception_Compiler( "Multiple units in dimension. Correct the units or use the unit function. Bad unit: " . $this->unit->toString() );
}
$value = Less_Functions::fround( $this->value );
@ -49,7 +41,7 @@ class Less_Tree_Dimension extends Less_Tree {
if ( $value !== 0 && $value < 0.000001 && $value > -0.000001 ) {
// would be output 1e-6 etc.
$strValue = number_format( $strValue, 10 );
$strValue = number_format( (float)$strValue, 10 );
$strValue = preg_replace( '/\.?0+$/', '', $strValue );
}
@ -57,7 +49,7 @@ class Less_Tree_Dimension extends Less_Tree {
// Zero values doesn't need a unit
if ( $value === 0 && $this->unit->isLength() ) {
$output->add( $strValue );
return $strValue;
return;
}
// Float values doesn't need a leading zero
@ -146,7 +138,7 @@ class Less_Tree_Dimension extends Less_Tree {
}
public function unify() {
return $this->convertTo( array( 'length' => 'px', 'duration' => 's', 'angle' => 'rad' ) );
return $this->convertTo( [ 'length' => 'px', 'duration' => 's', 'angle' => 'rad' ] );
}
public function convertTo( $conversions ) {
@ -154,10 +146,10 @@ class Less_Tree_Dimension extends Less_Tree {
$unit = clone $this->unit;
if ( is_string( $conversions ) ) {
$derivedConversions = array();
$derivedConversions = [];
foreach ( Less_Tree_UnitConversions::$groups as $i ) {
if ( isset( Less_Tree_UnitConversions::${$i}[$conversions] ) ) {
$derivedConversions = array( $i => $conversions );
$derivedConversions = [ $i => $conversions ];
}
}
$conversions = $derivedConversions;

View file

@ -1,10 +1,6 @@
<?php
/**
* Directive
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Directive extends Less_Tree {
@ -51,7 +47,7 @@ class Less_Tree_Directive extends Less_Tree {
$this->value->genCSS( $output );
}
if ( $this->rules ) {
Less_Tree::outputRuleset( $output, array( $this->rules ) );
Less_Tree::outputRuleset( $output, [ $this->rules ] );
} else {
$output->add( ';' );
}

View file

@ -1,28 +1,34 @@
<?php
/**
* Element
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Element extends Less_Tree {
public $combinator = '';
public $value = '';
/** @var string */
public $combinator;
/** @var bool Whether combinator is null (represented by empty string) or child (single space) */
public $combinatorIsEmptyOrWhitespace;
/** @var string|Less_Tree */
public $value;
public $index;
public $currentFileInfo;
public $type = 'Element';
public $value_is_object = false;
/**
* @param null|string $combinator
* @param string|Less_Tree $value
* @param int|null $index
* @param array|null $currentFileInfo
*/
public function __construct( $combinator, $value, $index = null, $currentFileInfo = null ) {
$this->value = $value;
$this->value_is_object = is_object( $value );
if ( $combinator ) {
$this->combinator = $combinator;
}
// see less-2.5.3.js#Combinator
$this->combinator = $combinator ?? '';
$this->combinatorIsEmptyOrWhitespace = ( $combinator === null || trim( $combinator ) === '' );
$this->index = $index;
$this->currentFileInfo = $currentFileInfo;
@ -35,15 +41,12 @@ class Less_Tree_Element extends Less_Tree {
}
public function compile( $env ) {
if ( Less_Environment::$mixin_stack ) {
return new Less_Tree_Element( $this->combinator, ( $this->value_is_object ? $this->value->compile( $env ) : $this->value ), $this->index, $this->currentFileInfo );
}
if ( $this->value_is_object ) {
$this->value = $this->value->compile( $env );
}
return $this;
return new Less_Tree_Element(
$this->combinator,
( $this->value_is_object ? $this->value->compile( $env ) : $this->value ),
$this->index,
$this->currentFileInfo
);
}
/**

View file

@ -1,16 +1,11 @@
<?php
/**
* Expression
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Expression extends Less_Tree {
public $value = array();
/** @var array */
public $value = [];
public $parens = false;
public $parensInOp = false;
public $type = 'Expression';
public function __construct( $value, $parens = null ) {
@ -36,7 +31,7 @@ class Less_Tree_Expression extends Less_Tree {
if ( $count > 1 ) {
$ret = array();
$ret = [];
foreach ( $this->value as $e ) {
$ret[] = $e->compile( $env );
}
@ -82,7 +77,7 @@ class Less_Tree_Expression extends Less_Tree {
public function throwAwayComments() {
if ( is_array( $this->value ) ) {
$new_value = array();
$new_value = [];
foreach ( $this->value as $v ) {
if ( $v instanceof Less_Tree_Comment ) {
continue;

View file

@ -1,17 +1,13 @@
<?php
/**
* Extend
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Extend extends Less_Tree {
public $selector;
public $option;
public $index;
public $selfSelectors = array();
public $selfSelectors = [];
public $allowBefore;
public $allowAfter;
public $firstExtendOnThisSelectorPath;
@ -19,10 +15,10 @@ class Less_Tree_Extend extends Less_Tree {
public $ruleset;
public $object_id;
public $parent_ids = array();
public $parent_ids = [];
/**
* @param integer $index
* @param int $index
*/
public function __construct( $selector, $option, $index ) {
static $i = 0;
@ -34,20 +30,20 @@ class Less_Tree_Extend extends Less_Tree {
case "all":
$this->allowBefore = true;
$this->allowAfter = true;
break;
break;
default:
$this->allowBefore = false;
$this->allowAfter = false;
break;
break;
}
// This must use a string (instead of int) so that array_merge()
// preserves keys on arrays that use IDs in their keys.
$this->object_id = 'id_' . $i++;
$this->parent_ids = array(
$this->parent_ids = [
$this->object_id => true
);
];
}
public function accept( $visitor ) {
@ -62,7 +58,7 @@ class Less_Tree_Extend extends Less_Tree {
}
public function findSelfSelectors( $selectors ) {
$selfElements = array();
$selfElements = [];
for ( $i = 0, $selectors_len = count( $selectors ); $i < $selectors_len; $i++ ) {
$selectorElements = $selectors[$i]->elements;
@ -74,7 +70,7 @@ class Less_Tree_Extend extends Less_Tree {
$selfElements = array_merge( $selfElements, $selectors[$i]->elements );
}
$this->selfSelectors = array( new Less_Tree_Selector( $selfElements ) );
$this->selfSelectors = [ new Less_Tree_Selector( $selfElements ) ];
}
}

View file

@ -1,7 +1,6 @@
<?php
/**
* CSS @import node
* CSS `@import` node
*
* The general strategy here is that we don't want to wait
* for the parsing to be completed, before we start importing
@ -12,8 +11,7 @@
* `import,push`, we also pass it a callback, which it'll call once
* the file has been fetched, and parsed.
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Import extends Less_Tree {
@ -35,13 +33,16 @@ class Less_Tree_Import extends Less_Tree {
$this->currentFileInfo = $currentFileInfo;
if ( is_array( $options ) ) {
$this->options += array( 'inline' => false );
$this->options += [ 'inline' => false ];
if ( isset( $this->options['less'] ) || $this->options['inline'] ) {
$this->css = !isset( $this->options['less'] ) || !$this->options['less'] || $this->options['inline'];
} else {
$pathValue = $this->getPath();
if ( $pathValue && preg_match( '/css([\?;].*)?$/', $pathValue ) ) {
// Leave any ".css" file imports as literals for the browser.
// Also leave any remote HTTP resources as literals regardless of whether
// they contain ".css" in their filename.
if ( $pathValue && preg_match( '/^(https?:)?\/\/|\.css$/i', $pathValue ) ) {
$this->css = true;
}
}
@ -97,13 +98,17 @@ class Less_Tree_Import extends Less_Tree {
}
/**
* @return string
* @return string|null
*/
public function getPath() {
if ( $this->path instanceof Less_Tree_Quoted ) {
$path = $this->path->value;
$path = ( isset( $this->css ) || preg_match( '/(\.[a-z]*$)|([\?;].*)$/', $path ) ) ? $path : $path . '.less';
} else if ( $this->path instanceof Less_Tree_URL ) {
// During the first pass, Less_Tree_URL may contain a Less_Tree_Variable (not yet expanded),
// and thus has no value property defined yet. Return null until we reach the next phase.
// https://github.com/wikimedia/less.php/issues/29
} elseif ( $this->path instanceof Less_Tree_URL && !( $this->path->value instanceof Less_Tree_Variable ) ) {
$path = $this->path->value->value;
} else {
return null;
@ -129,7 +134,7 @@ class Less_Tree_Import extends Less_Tree {
$pathValue = $path->value;
// Add the base path if the import is relative
if ( $pathValue && Less_Environment::isPathRelative( $pathValue ) ) {
$path->value = $this->currentFileInfo['uri_root'].$pathValue;
$path->value = $this->currentFileInfo['uri_root'] . $pathValue;
}
}
$path->value = Less_Environment::normalizePath( $path->value );
@ -159,26 +164,27 @@ class Less_Tree_Import extends Less_Tree {
// import once
if ( $evald->skip( $full_path, $env ) ) {
return array();
return [];
}
'@phan-var string $full_path';
if ( $this->options['inline'] ) {
// todo needs to reference css file not import
//$contents = new Less_Tree_Anonymous($this->root, 0, array('filename'=>$this->importedFilename), true );
Less_Parser::AddParsedFile( $full_path );
$contents = new Less_Tree_Anonymous( file_get_contents( $full_path ), 0, array(), true );
$contents = new Less_Tree_Anonymous( file_get_contents( $full_path ), 0, [], true );
if ( $this->features ) {
return new Less_Tree_Media( array( $contents ), $this->features->value );
return new Less_Tree_Media( [ $contents ], $this->features->value );
}
return array( $contents );
return [ $contents ];
}
// optional (need to be before "CSS" to support optional CSS imports. CSS should be checked only if empty($this->currentFileInfo))
if ( isset( $this->options['optional'] ) && $this->options['optional'] && !file_exists( $full_path ) && ( !$evald->css || !empty( $this->currentFileInfo ) ) ) {
return array();
return [];
}
// css ?
@ -192,15 +198,13 @@ class Less_Tree_Import extends Less_Tree {
/**
* Using the import directories, get the full absolute path and uri of the import
*
* @param Less_Tree_Import $evald
*/
public function PathAndUri() {
$evald_path = $this->getPath();
if ( $evald_path ) {
$import_dirs = array();
$import_dirs = [];
if ( Less_Environment::isPathRelative( $evald_path ) ) {
// if the path is relative, the file should be in the current directory
@ -226,20 +230,20 @@ class Less_Tree_Import extends Less_Tree {
list( $path, $uri ) = call_user_func( $rooturi, $evald_path );
if ( is_string( $path ) ) {
$full_path = $path;
return array( $full_path, $uri );
return [ $full_path, $uri ];
}
} elseif ( !empty( $rootpath ) ) {
$path = rtrim( $rootpath, '/\\' ).'/'.ltrim( $evald_path, '/\\' );
$path = rtrim( $rootpath, '/\\' ) . '/' . ltrim( $evald_path, '/\\' );
if ( file_exists( $path ) ) {
$full_path = Less_Environment::normalizePath( $path );
$uri = Less_Environment::normalizePath( dirname( $rooturi.$evald_path ) );
return array( $full_path, $uri );
} elseif ( file_exists( $path.'.less' ) ) {
$full_path = Less_Environment::normalizePath( $path.'.less' );
$uri = Less_Environment::normalizePath( dirname( $rooturi.$evald_path.'.less' ) );
return array( $full_path, $uri );
$uri = Less_Environment::normalizePath( dirname( $rooturi . $evald_path ) );
return [ $full_path, $uri ];
} elseif ( file_exists( $path . '.less' ) ) {
$full_path = Less_Environment::normalizePath( $path . '.less' );
$uri = Less_Environment::normalizePath( dirname( $rooturi . $evald_path . '.less' ) );
return [ $full_path, $uri ];
}
}
}
@ -249,6 +253,9 @@ class Less_Tree_Import extends Less_Tree {
/**
* Parse the import url and return the rules
*
* @param string $full_path
* @param string|null $uri
* @param mixed $env
* @return Less_Tree_Media|array
*/
public function ParseImport( $full_path, $uri, $env ) {
@ -264,7 +271,7 @@ class Less_Tree_Import extends Less_Tree {
$parser = new Less_Parser( $import_env );
$root = $parser->parseFile( $full_path, $uri, true );
$ruleset = new Less_Tree_Ruleset( array(), $root->rules );
$ruleset = new Less_Tree_Ruleset( null, $root->rules );
$ruleset->evalImports( $import_env );
return $this->features ? new Less_Tree_Media( $ruleset->rules, $this->features->value ) : $ruleset->rules;
@ -273,9 +280,11 @@ class Less_Tree_Import extends Less_Tree {
/**
* Should the import be skipped?
*
* @return boolean|null
* @param string|null $path
* @param Less_Environment $env
* @return bool|null
*/
private function Skip( $path, $env ) {
private function skip( $path, $env ) {
$path = Less_Parser::AbsPath( $path, true );
if ( $path && Less_Parser::FileParsed( $path ) ) {
@ -286,6 +295,5 @@ class Less_Tree_Import extends Less_Tree {
return !isset( $this->options['multiple'] ) && !$env->importMultiple;
}
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Javascript
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Javascript extends Less_Tree {
@ -14,8 +10,8 @@ class Less_Tree_Javascript extends Less_Tree {
public $index;
/**
* @param boolean $index
* @param boolean $escaped
* @param bool $index
* @param bool $escaped
*/
public function __construct( $string, $index, $escaped ) {
$this->escaped = $escaped;
@ -23,7 +19,7 @@ class Less_Tree_Javascript extends Less_Tree {
$this->index = $index;
}
public function compile() {
public function compile( $env ) {
return new Less_Tree_Anonymous( '/* Sorry, can not do JavaScript evaluation in PHP... :( */' );
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Keyword
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Keyword extends Less_Tree {
@ -18,10 +14,6 @@ class Less_Tree_Keyword extends Less_Tree {
$this->value = $value;
}
public function compile() {
return $this;
}
/**
* @see Less_Tree::genCSS
*/

View file

@ -1,10 +1,6 @@
<?php
/**
* Media
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Media extends Less_Tree {
@ -15,7 +11,7 @@ class Less_Tree_Media extends Less_Tree {
public $isReferenced;
public $type = 'Media';
public function __construct( $value = array(), $features = array(), $index = null, $currentFileInfo = null ) {
public function __construct( $value = [], $features = [], $index = null, $currentFileInfo = null ) {
$this->index = $index;
$this->currentFileInfo = $currentFileInfo;
@ -23,7 +19,7 @@ class Less_Tree_Media extends Less_Tree {
$this->features = new Less_Tree_Value( $features );
$this->rules = array( new Less_Tree_Ruleset( $selectors, $value ) );
$this->rules = [ new Less_Tree_Ruleset( $selectors, $value ) ];
$this->rules[0]->allowImports = true;
}
@ -39,11 +35,15 @@ class Less_Tree_Media extends Less_Tree {
$output->add( '@media ', $this->currentFileInfo, $this->index );
$this->features->genCSS( $output );
Less_Tree::outputRuleset( $output, $this->rules );
}
/**
* @param Less_Environment $env
* @return Less_Tree_Media|Less_Tree_Ruleset
* @see less-2.5.3.js#Media.prototype.eval
*/
public function compile( $env ) {
$media = new Less_Tree_Media( array(), array(), $this->index, $this->currentFileInfo );
$media = new Less_Tree_Media( [], [], $this->index, $this->currentFileInfo );
$strictMathBypass = false;
if ( Less_Parser::$options['strictMath'] === false ) {
@ -61,7 +61,7 @@ class Less_Tree_Media extends Less_Tree {
$env->mediaBlocks[] = $media;
array_unshift( $env->frames, $this->rules[0] );
$media->rules = array( $this->rules[0]->compile( $env ) );
$media->rules = [ $this->rules[0]->compile( $env ) ];
array_shift( $env->frames );
array_pop( $env->mediaPath );
@ -79,7 +79,7 @@ class Less_Tree_Media extends Less_Tree {
public function emptySelectors() {
$el = new Less_Tree_Element( '', '&', $this->index, $this->currentFileInfo );
$sels = array( new Less_Tree_Selector( array( $el ), array(), null, $this->index, $this->currentFileInfo ) );
$sels = [ new Less_Tree_Selector( [ $el ], [], null, $this->index, $this->currentFileInfo ) ];
$sels[0]->mediaEmpty = true;
return $sels;
}
@ -100,20 +100,26 @@ class Less_Tree_Media extends Less_Tree {
$result->multiMedia = true;
}
$env->mediaBlocks = array();
$env->mediaPath = array();
$env->mediaBlocks = [];
$env->mediaPath = [];
return $result;
}
/**
* @param Less_Environment $env
* @return Less_Tree_Ruleset
*/
public function compileNested( $env ) {
$path = array_merge( $env->mediaPath, array( $this ) );
$path = array_merge( $env->mediaPath, [ $this ] );
'@phan-var array<Less_Tree_Media> $path';
// Extract the media-query conditions separated with `,` (OR).
foreach ( $path as $key => $p ) {
$value = $p->features instanceof Less_Tree_Value ? $p->features->value : $p->features;
$path[$key] = is_array( $value ) ? $value : array( $value );
$path[$key] = is_array( $value ) ? $value : [ $value ];
}
'@phan-var array<array<Less_Tree>> $path';
// Trace all permutations to generate the resulting media-query.
//
@ -124,7 +130,7 @@ class Less_Tree_Media extends Less_Tree {
// b and c and e
$permuted = $this->permute( $path );
$expressions = array();
$expressions = [];
foreach ( $permuted as $path ) {
for ( $i = 0, $len = count( $path ); $i < $len; $i++ ) {
@ -132,7 +138,7 @@ class Less_Tree_Media extends Less_Tree {
}
for ( $i = count( $path ) - 1; $i > 0; $i-- ) {
array_splice( $path, $i, 0, array( new Less_Tree_Anonymous( 'and' ) ) );
array_splice( $path, $i, 0, [ new Less_Tree_Anonymous( 'and' ) ] );
}
$expressions[] = new Less_Tree_Expression( $path );
@ -140,23 +146,25 @@ class Less_Tree_Media extends Less_Tree {
$this->features = new Less_Tree_Value( $expressions );
// Fake a tree-node that doesn't output anything.
return new Less_Tree_Ruleset( array(), array() );
return new Less_Tree_Ruleset( [], [] );
}
public function permute( $arr ) {
if ( !$arr )
return array();
if ( !$arr ) {
return [];
}
if ( count( $arr ) == 1 )
if ( count( $arr ) == 1 ) {
return $arr[0];
}
$result = array();
$result = [];
$rest = $this->permute( array_slice( $arr, 1 ) );
foreach ( $rest as $r ) {
foreach ( $arr[0] as $a ) {
$result[] = array_merge(
is_array( $a ) ? $a : array( $a ),
is_array( $r ) ? $r : array( $r )
is_array( $a ) ? $a : [ $a ],
is_array( $r ) ? $r : [ $r ]
);
}
}
@ -165,9 +173,11 @@ class Less_Tree_Media extends Less_Tree {
}
public function bubbleSelectors( $selectors ) {
if ( !$selectors ) return;
if ( !$selectors ) {
return;
}
$this->rules = array( new Less_Tree_Ruleset( $selectors, array( $this->rules[0] ) ) );
$this->rules = [ new Less_Tree_Ruleset( $selectors, [ $this->rules[0] ] ) ];
}
}

View file

@ -1,5 +1,7 @@
<?php
/**
* @private
*/
class Less_Tree_Mixin_Call extends Less_Tree {
public $selector;
@ -28,16 +30,16 @@ class Less_Tree_Mixin_Call extends Less_Tree {
//}
public function compile( $env ) {
$rules = array();
$rules = [];
$match = false;
$isOneFound = false;
$candidates = array();
$candidates = [];
$defaultUsed = false;
$conditionResult = array();
$conditionResult = [];
$args = array();
$args = [];
foreach ( $this->arguments as $a ) {
$args[] = array( 'name' => $a['name'], 'value' => $a['value']->compile( $env ) );
$args[] = [ 'name' => $a['name'], 'value' => $a['value']->compile( $env ) ];
}
foreach ( $env->frames as $frame ) {
@ -68,14 +70,17 @@ class Less_Tree_Mixin_Call extends Less_Tree {
if ( $mixin->matchArgs( $args, $env ) ) {
$candidate = array( 'mixin' => $mixin, 'group' => $defNone );
$candidate = [ 'mixin' => $mixin, 'group' => $defNone ];
if ( $mixin instanceof Less_Tree_Ruleset ) {
for ( $f = 0; $f < 2; $f++ ) {
Less_Tree_DefaultFunc::value( $f );
$conditionResult[$f] = $mixin->matchCondition( $args, $env );
}
// PhanTypeInvalidDimOffset -- False positive
'@phan-var array{0:bool,1:bool} $conditionResult';
if ( $conditionResult[0] || $conditionResult[1] ) {
if ( $conditionResult[0] != $conditionResult[1] ) {
$candidate['group'] = $conditionResult[1] ? $defTrue : $defFalse;
@ -93,7 +98,7 @@ class Less_Tree_Mixin_Call extends Less_Tree {
Less_Tree_DefaultFunc::reset();
$count = array( 0, 0, 0 );
$count = [ 0, 0, 0 ];
for ( $m = 0; $m < count( $candidates ); $m++ ) {
$count[ $candidates[$m]['group'] ]++;
}
@ -116,7 +121,7 @@ class Less_Tree_Mixin_Call extends Less_Tree {
try{
$mixin = $candidates[$m]['mixin'];
if ( !( $mixin instanceof Less_Tree_Mixin_Definition ) ) {
$mixin = new Less_Tree_Mixin_Definition( '', array(), $mixin->rules, null, false );
$mixin = new Less_Tree_Mixin_Definition( '', [], $mixin->rules, null, false );
$mixin->originalRuleset = $mixins[$m]->originalRuleset;
}
$rules = array_merge( $rules, $mixin->evalCall( $env, $args, $this->important )->rules );
@ -137,12 +142,11 @@ class Less_Tree_Mixin_Call extends Less_Tree {
}
if ( $isOneFound ) {
throw new Less_Exception_Compiler( 'No matching definition was found for `'.$this->Format( $args ).'`', null, $this->index, $this->currentFileInfo );
throw new Less_Exception_Compiler( 'No matching definition was found for `' . $this->Format( $args ) . '`', null, $this->index, $this->currentFileInfo );
} else {
throw new Less_Exception_Compiler( trim( $this->selector->toCSS() ) . " is undefined in ".$this->currentFileInfo['filename'], null, $this->index );
throw new Less_Exception_Compiler( trim( $this->selector->toCSS() ) . " is undefined in " . $this->currentFileInfo['filename'], null, $this->index );
}
}
/**
@ -150,7 +154,7 @@ class Less_Tree_Mixin_Call extends Less_Tree {
*
*/
private function Format( $args ) {
$message = array();
$message = [];
if ( $args ) {
foreach ( $args as $a ) {
$argValue = '';

View file

@ -1,22 +1,24 @@
<?php
/**
* @private
*/
class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
public $name;
public $selectors;
public $params;
public $arity = 0;
public $arity = 0;
public $rules;
public $lookups = array();
public $lookups = [];
public $required = 0;
public $frames = array();
public $frames = [];
public $condition;
public $variadic;
public $type = 'MixinDefinition';
public $type = 'MixinDefinition';
// less.js : /lib/less/tree/mixin.js : tree.mixin.Definition
public function __construct( $name, $params, $rules, $condition, $variadic = false, $frames = array() ) {
public function __construct( $name, $params, $rules, $condition, $variadic = false, $frames = [] ) {
$this->name = $name;
$this->selectors = array( new Less_Tree_Selector( array( new Less_Tree_Element( null, $name ) ) ) );
$this->selectors = [ new Less_Tree_Selector( [ new Less_Tree_Element( null, $name ) ] ) ];
$this->params = $params;
$this->condition = $condition;
@ -47,8 +49,8 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
}
// less.js : /lib/less/tree/mixin.js : tree.mixin.Definition.evalParams
public function compileParams( $env, $mixinFrames, $args = array(), &$evaldArguments = array() ) {
$frame = new Less_Tree_Ruleset( null, array() );
public function compileParams( $env, $mixinFrames, $args = [], &$evaldArguments = [] ) {
$frame = new Less_Tree_Ruleset( null, [] );
$params = $this->params;
$mixinEnv = null;
$argsLength = 0;
@ -75,7 +77,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
$argsLength--;
continue;
} else {
throw new Less_Exception_Compiler( "Named argument for " . $this->name .' '.$args[$i]['name'] . ' not found' );
throw new Less_Exception_Compiler( "Named argument for " . $this->name . ' ' . $args[$i]['name'] . ' not found' );
}
}
}
@ -95,7 +97,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
if ( isset( $param['name'] ) && $param['name'] ) {
if ( isset( $param['variadic'] ) ) {
$varargs = array();
$varargs = [];
for ( $j = $argIndex; $j < $argsLength; $j++ ) {
$varargs[] = $args[$j]['value']->compile( $env );
}
@ -106,11 +108,11 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
if ( $val ) {
$val = $val->compile( $env );
} else if ( isset( $param['value'] ) ) {
} elseif ( isset( $param['value'] ) ) {
if ( !$mixinEnv ) {
$mixinEnv = new Less_Environment();
$mixinEnv->frames = array_merge( array( $frame ), $mixinFrames );
$mixinEnv->frames = array_merge( [ $frame ], $mixinFrames );
}
$val = $param['value']->compile( $mixinEnv );
@ -145,10 +147,10 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
return new Less_Tree_Mixin_Definition( $this->name, $this->params, $this->rules, $this->condition, $this->variadic, $env->frames );
}
public function evalCall( $env, $args = NULL, $important = NULL ) {
public function evalCall( $env, $args = null, $important = null ) {
Less_Environment::$mixin_stack++;
$_arguments = array();
$_arguments = [];
if ( $this->frames ) {
$mixinFrames = array_merge( $this->frames, $env->frames );
@ -165,7 +167,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
$ruleset->originalRuleset = $this->ruleset_id;
$ruleSetEnv = new Less_Environment();
$ruleSetEnv->frames = array_merge( array( $this, $frame ), $mixinFrames );
$ruleSetEnv->frames = array_merge( [ $this, $frame ], $mixinFrames );
$ruleset = $ruleset->compile( $ruleSetEnv );
if ( $important ) {
@ -177,6 +179,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
return $ruleset;
}
/** @return bool */
public function matchCondition( $args, $env ) {
if ( !$this->condition ) {
return true;
@ -184,14 +187,14 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
// set array to prevent error on array_merge
if ( !is_array( $this->frames ) ) {
$this->frames = array();
$this->frames = [];
}
$frame = $this->compileParams( $env, array_merge( $this->frames, $env->frames ), $args );
$compile_env = new Less_Environment();
$compile_env->frames = array_merge(
array( $frame ), // the parameter variables
[ $frame ], // the parameter variables
$this->frames, // the parent namespace/mixin frames
$env->frames // the current environment frames
);
@ -201,7 +204,7 @@ class Less_Tree_Mixin_Definition extends Less_Tree_Ruleset {
return (bool)$this->condition->compile( $compile_env );
}
public function matchArgs( $args, $env = NULL ) {
public function matchArgs( $args, $env = null ) {
$argsLength = count( $args );
if ( !$this->variadic ) {

View file

@ -1,14 +1,14 @@
<?php
/**
* A simple css name-value pair
* ex: width:100px;
* A simple CSS name-value pair, e.g. `width: 100px;`
*
* In bootstrap, there are about 600-1,000 simple name-value pairs (depending on how forgiving the match is) -vs- 6,020 dynamic rules (Less_Tree_Rule)
* Using the name-value object can speed up bootstrap compilation slightly, but it breaks color keyword interpretation: color:red -> color:#FF0000;
* In bootstrap, there are about 600-1000 simple name-value pairs (depending on
* how forgiving the match is) -vs- 6,020 dynamic rules (Less_Tree_Rule).
*
* @package Less
* @subpackage tree
* Using the name-value object can speed up bootstrap compilation slightly, but
* it breaks color keyword interpretation: `color: red` -> `color: #FF0000`.
*
* @private
*/
class Less_Tree_NameValue extends Less_Tree {

View file

@ -1,10 +1,6 @@
<?php
/**
* Negative
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Negative extends Less_Tree {
@ -29,7 +25,7 @@ class Less_Tree_Negative extends Less_Tree {
public function compile( $env ) {
if ( Less_Environment::isMathOn() ) {
$ret = new Less_Tree_Operation( '*', array( new Less_Tree_Dimension( -1 ), $this->value ) );
$ret = new Less_Tree_Operation( '*', [ new Less_Tree_Dimension( -1 ), $this->value ] );
return $ret->compile( $env );
}
return new Less_Tree_Negative( $this->value->compile( $env ) );

View file

@ -1,10 +1,6 @@
<?php
/**
* Operation
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Operation extends Less_Tree {
@ -47,7 +43,7 @@ class Less_Tree_Operation extends Less_Tree {
return $a->operate( $this->op, $b );
}
return new Less_Tree_Operation( $this->op, array( $a, $b ), $this->isSpaced );
return new Less_Tree_Operation( $this->op, [ $a, $b ], $this->isSpaced );
}
/**

View file

@ -1,16 +1,16 @@
<?php
/**
* Paren
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Paren extends Less_Tree {
/** @var Less_Tree $value */
public $value;
public $type = 'Paren';
/**
* @param Less_Tree $value
*/
public function __construct( $value ) {
$this->value = $value;
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Quoted
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Quoted extends Less_Tree {
public $escaped;
@ -45,7 +41,7 @@ class Less_Tree_Quoted extends Less_Tree {
if ( preg_match_all( '/`([^`]+)`/', $this->value, $matches ) ) {
foreach ( $matches as $i => $match ) {
$js = new Less_Tree_JavaScript( $matches[1], $this->index, true );
$js = $js->compile()->value;
$js = $js->compile( $env )->value;
$value = str_replace( $matches[0][$i], $js, $value );
}
}

View file

@ -1,14 +1,11 @@
<?php
/**
* Rule
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Rule extends Less_Tree {
public $name;
/** @var Less_Tree $value */
public $value;
public $important;
public $merge;
@ -23,7 +20,9 @@ class Less_Tree_Rule extends Less_Tree {
*/
public function __construct( $name, $value = null, $important = null, $merge = null, $index = null, $currentFileInfo = null, $inline = false ) {
$this->name = $name;
$this->value = ( $value instanceof Less_Tree_Value || $value instanceof Less_Tree_Ruleset ) ? $value : new Less_Tree_Value( array( $value ) );
$this->value = ( $value instanceof Less_Tree )
? $value
: new Less_Tree_Value( [ $value ] );
$this->important = $important ? ' ' . trim( $important ) : '';
$this->merge = $merge;
$this->index = $index;
@ -52,6 +51,10 @@ class Less_Tree_Rule extends Less_Tree {
$output->add( $this->important . ( ( $this->inline || ( Less_Environment::$lastRule && Less_Parser::$options['compress'] ) ) ? "" : ";" ), $this->currentFileInfo, $this->index );
}
/**
* @param Less_Environment $env
* @return Less_Tree_Rule
*/
public function compile( $env ) {
$name = $this->name;
if ( is_array( $name ) ) {
@ -84,7 +87,7 @@ class Less_Tree_Rule extends Less_Tree {
$return = $this;
}
}catch ( Less_Exception_Parser $e ) {
} catch ( Less_Exception_Parser $e ) {
if ( !is_numeric( $e->index ) ) {
$e->index = $this->index;
$e->currentFile = $this->currentFileInfo;

View file

@ -1,10 +1,6 @@
<?php
/**
* Ruleset
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Ruleset extends Less_Tree {
@ -42,18 +38,23 @@ class Less_Tree_Ruleset extends Less_Tree {
}
}
/**
* @param null|Less_Tree_Selector[] $selectors
* @param Less_Tree[] $rules
* @param null|bool $strictImports
*/
public function __construct( $selectors, $rules, $strictImports = null ) {
$this->selectors = $selectors;
$this->rules = $rules;
$this->lookups = array();
$this->lookups = [];
$this->strictImports = $strictImports;
$this->SetRulesetIndex();
}
public function accept( $visitor ) {
if ( $this->paths ) {
if ( $this->paths !== null ) {
$paths_len = count( $this->paths );
for ( $i = 0,$paths_len; $i < $paths_len; $i++ ) {
for ( $i = 0; $i < $paths_len; $i++ ) {
$this->paths[$i] = $visitor->visitArray( $this->paths[$i] );
}
} elseif ( $this->selectors ) {
@ -65,6 +66,11 @@ class Less_Tree_Ruleset extends Less_Tree {
}
}
/**
* @param Less_Environment $env
* @return Less_Tree_Ruleset
* @see less-2.5.3.js#Ruleset.prototype.eval
*/
public function compile( $env ) {
$ruleset = $this->PrepareRuleset( $env );
@ -72,15 +78,13 @@ class Less_Tree_Ruleset extends Less_Tree {
// so they can be evaluated like closures when the time comes.
$rsRuleCnt = count( $ruleset->rules );
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
// These checks are the equivalent of the rule.evalFirst property in less.js
if ( $ruleset->rules[$i] instanceof Less_Tree_Mixin_Definition || $ruleset->rules[$i] instanceof Less_Tree_DetachedRuleset ) {
$ruleset->rules[$i] = $ruleset->rules[$i]->compile( $env );
}
}
$mediaBlockCount = 0;
if ( $env instanceof Less_Environment ) {
$mediaBlockCount = count( $env->mediaBlocks );
}
$mediaBlockCount = count( $env->mediaBlocks );
// Evaluate mixin calls.
$this->EvalMixinCalls( $ruleset, $env, $rsRuleCnt );
@ -107,7 +111,7 @@ class Less_Tree_Ruleset extends Less_Tree {
for ( $j = 0; $j < count( $rule->rules ); $j++ ) {
$subRule = $rule->rules[$j];
if ( !( $subRule instanceof Less_Tree_Rule ) || !$subRule->variable ) {
array_splice( $ruleset->rules, ++$i, 0, array( $subRule ) );
array_splice( $ruleset->rules, ++$i, 0, [ $subRule ] );
$rsRuleCnt++;
}
}
@ -133,7 +137,7 @@ class Less_Tree_Ruleset extends Less_Tree {
* Compile Less_Tree_Mixin_Call objects
*
* @param Less_Tree_Ruleset $ruleset
* @param integer $rsRuleCnt
* @param int $rsRuleCnt
*/
private function EvalMixinCalls( $ruleset, $env, &$rsRuleCnt ) {
for ( $i = 0; $i < $rsRuleCnt; $i++ ) {
@ -142,7 +146,7 @@ class Less_Tree_Ruleset extends Less_Tree {
if ( $rule instanceof Less_Tree_Mixin_Call ) {
$rule = $rule->compile( $env );
$temp = array();
$temp = [];
foreach ( $rule as $r ) {
if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) {
// do not pollute the scope if the variable is
@ -164,7 +168,7 @@ class Less_Tree_Ruleset extends Less_Tree {
} elseif ( $rule instanceof Less_Tree_RulesetCall ) {
$rule = $rule->compile( $env );
$rules = array();
$rules = [];
foreach ( $rule->rules as $r ) {
if ( ( $r instanceof Less_Tree_Rule ) && $r->variable ) {
continue;
@ -185,14 +189,21 @@ class Less_Tree_Ruleset extends Less_Tree {
/**
* Compile the selectors and create a new ruleset object for the compile() method
*
* @param Less_Environment $env
* @return Less_Tree_Ruleset
*/
private function PrepareRuleset( $env ) {
// NOTE: Preserve distinction between null and empty array when compiling
// $this->selectors to $selectors
$thisSelectors = $this->selectors;
$selectors = null;
$hasOnePassingSelector = false;
$selectors = array();
if ( $this->selectors ) {
if ( $thisSelectors ) {
Less_Tree_DefaultFunc::error( "it is currently only allowed in parametric mixin guards," );
foreach ( $this->selectors as $s ) {
$selectors = [];
foreach ( $thisSelectors as $s ) {
$selector = $s->compile( $env );
$selectors[] = $selector;
if ( $selector->evaldCondition ) {
@ -206,15 +217,15 @@ class Less_Tree_Ruleset extends Less_Tree {
}
if ( $this->rules && $hasOnePassingSelector ) {
// Copy the array (no need for slice in PHP)
$rules = $this->rules;
} else {
$rules = array();
$rules = [];
}
$ruleset = new Less_Tree_Ruleset( $selectors, $rules, $this->strictImports );
$ruleset->originalRuleset = $this->ruleset_id;
$ruleset->root = $this->root;
$ruleset->firstRoot = $this->firstRoot;
$ruleset->allowImports = $this->allowImports;
@ -243,7 +254,7 @@ class Less_Tree_Ruleset extends Less_Tree {
$i += $temp_count;
$rules_len += $temp_count;
} else {
array_splice( $this->rules, $i, 1, array( $rules ) );
array_splice( $this->rules, $i, 1, [ $rules ] );
}
$this->resetCache();
@ -252,7 +263,7 @@ class Less_Tree_Ruleset extends Less_Tree {
}
function makeImportant() {
$important_rules = array();
$important_rules = [];
foreach ( $this->rules as $rule ) {
if ( $rule instanceof Less_Tree_Rule || $rule instanceof Less_Tree_Ruleset || $rule instanceof Less_Tree_NameValue ) {
$important_rules[] = $rule->makeImportant();
@ -264,7 +275,7 @@ class Less_Tree_Ruleset extends Less_Tree {
return new Less_Tree_Ruleset( $this->selectors, $important_rules, $this->strictImports );
}
public function matchArgs( $args ) {
public function matchArgs( $args, $env = null ) {
return !$args;
}
@ -284,11 +295,11 @@ class Less_Tree_Ruleset extends Less_Tree {
function resetCache() {
$this->_rulesets = null;
$this->_variables = null;
$this->lookups = array();
$this->lookups = [];
}
public function variables() {
$this->_variables = array();
$this->_variables = [];
foreach ( $this->rules as $r ) {
if ( $r instanceof Less_Tree_Rule && $r->variable === true ) {
$this->_variables[$r->name] = $r;
@ -296,11 +307,15 @@ class Less_Tree_Ruleset extends Less_Tree {
}
}
/**
* @param string $name
* @return Less_Tree_Rule|null
*/
public function variable( $name ) {
if ( is_null( $this->_variables ) ) {
if ( $this->_variables === null ) {
$this->variables();
}
return isset( $this->_variables[$name] ) ? $this->_variables[$name] : null;
return $this->_variables[$name] ?? null;
}
public function find( $selector, $self = null ) {
@ -312,7 +327,7 @@ class Less_Tree_Ruleset extends Less_Tree {
$self = $this->ruleset_id;
}
$this->lookups[$key] = array();
$this->lookups[$key] = [];
$first_oelement = $selector->_oelements[0];
@ -351,19 +366,24 @@ class Less_Tree_Ruleset extends Less_Tree {
$tabRuleStr = $tabSetStr = '';
if ( !Less_Parser::$options['compress'] ) {
if ( Less_Environment::$tabLevel ) {
$tabRuleStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel );
$tabSetStr = "\n".str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
$tabRuleStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel );
$tabSetStr = "\n" . str_repeat( Less_Parser::$options['indentation'], Less_Environment::$tabLevel - 1 );
} else {
$tabSetStr = $tabRuleStr = "\n";
}
}
$ruleNodes = array();
$rulesetNodes = array();
$ruleNodes = [];
$rulesetNodes = [];
foreach ( $this->rules as $rule ) {
$class = get_class( $rule );
if ( ( $class === 'Less_Tree_Media' ) || ( $class === 'Less_Tree_Directive' ) || ( $this->root && $class === 'Less_Tree_Comment' ) || ( $class === 'Less_Tree_Ruleset' && $rule->rules ) ) {
if (
( $class === 'Less_Tree_Media' ) ||
( $class === 'Less_Tree_Directive' ) ||
( $this->root && $class === 'Less_Tree_Comment' ) ||
( $rule instanceof Less_Tree_Ruleset && $rule->rules )
) {
$rulesetNodes[] = $rule;
} else {
$ruleNodes[] = $rule;
@ -373,16 +393,6 @@ class Less_Tree_Ruleset extends Less_Tree {
// If this is the root node, we don't render
// a selector, or {}.
if ( !$this->root ) {
/*
debugInfo = tree.debugInfo(env, this, tabSetStr);
if (debugInfo) {
output.add(debugInfo);
output.add(tabSetStr);
}
*/
$paths_len = count( $this->paths );
for ( $i = 0; $i < $paths_len; $i++ ) {
$path = $this->paths[$i];
@ -443,7 +453,6 @@ class Less_Tree_Ruleset extends Less_Tree {
if ( !Less_Parser::$options['compress'] && $this->firstRoot ) {
$output->add( "\n" );
}
}
function markReferenced() {
@ -455,9 +464,14 @@ class Less_Tree_Ruleset extends Less_Tree {
}
}
/**
* @param Less_Tree_Selector[][] $context
* @param Less_Tree_Selector[]|null $selectors
* @return Less_Tree_Selector[][]
*/
public function joinSelectors( $context, $selectors ) {
$paths = array();
if ( is_array( $selectors ) ) {
$paths = [];
if ( $selectors !== null ) {
foreach ( $selectors as $selector ) {
$this->joinSelector( $paths, $context, $selector );
}
@ -465,26 +479,37 @@ class Less_Tree_Ruleset extends Less_Tree {
return $paths;
}
public function joinSelector( &$paths, $context, $selector ) {
$hasParentSelector = false;
public function joinSelector( array &$paths, array $context, Less_Tree_Selector $selector ) {
$newPaths = [];
$hadParentSelector = $this->replaceParentSelector( $newPaths, $context, $selector );
foreach ( $selector->elements as $el ) {
if ( $el->value === '&' ) {
$hasParentSelector = true;
}
}
if ( !$hasParentSelector ) {
if ( !$hadParentSelector ) {
if ( $context ) {
foreach ( $context as $context_el ) {
$paths[] = array_merge( $context_el, array( $selector ) );
$newPaths = [];
foreach ( $context as $path ) {
$newPaths[] = array_merge( $path, [ $selector ] );
}
} else {
$paths[] = array( $selector );
$newPaths = [ [ $selector ] ];
}
return;
}
foreach ( $newPaths as $newPath ) {
$paths[] = $newPath;
}
}
/**
* Replace all parent selectors inside $inSelector with $context.
*
* @param array &$paths Resulting selectors are appended to $paths.
* @param mixed $context
* @param Less_Tree_Selector $inSelector Inner selector from Less_Tree_Paren
* @return bool True if $inSelector contained at least one parent selector
*/
private function replaceParentSelector( array &$paths, $context, Less_Tree_Selector $inSelector ) {
$hadParentSelector = false;
// The paths are [[Selector]]
// The first list is a list of comma separated selectors
// The inner list is a list of inheritance separated selectors
@ -497,86 +522,70 @@ class Less_Tree_Ruleset extends Less_Tree {
//
// the elements from the current selector so far
$currentElements = array();
$currentElements = [];
// the current list of new selectors to add to the path.
// We will build it up. We initiate it with one empty selector as we "multiply" the new selectors
// by the parents
$newSelectors = array( array() );
$newSelectors = [
[]
];
foreach ( $selector->elements as $el ) {
// non parent reference elements just get added
foreach ( $inSelector->elements as $el ) {
// non-parent reference elements just get added
if ( $el->value !== '&' ) {
$currentElements[] = $el;
$nestedSelector = $this->findNestedSelector( $el );
if ( $nestedSelector !== null ) {
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
$nestedPaths = [];
$replacedNewSelectors = [];
$replaced = $this->replaceParentSelector( $nestedPaths, $context, $nestedSelector );
$hadParentSelector = $hadParentSelector || $replaced;
// $nestedPaths is populated by replaceParentSelector()
// $nestedPaths should have exactly one TODO, replaceParentSelector does not multiply selectors
foreach ( $nestedPaths as $nestedPath ) {
$replacementSelector = $this->createSelector( $nestedPath, $el );
// join selector path from $newSelectors with every selector path in $addPaths array.
// $el contains the element that is being replaced by $addPaths
//
// @see less-2.5.3.js#Ruleset-addAllReplacementsIntoPath
$addPaths = [ $replacementSelector ];
foreach ( $newSelectors as $newSelector ) {
$replacedNewSelectors[] = $this->addReplacementIntoPath( $newSelector, $addPaths, $el, $inSelector );
}
}
$newSelectors = $replacedNewSelectors;
$currentElements = [];
} else {
$currentElements[] = $el;
}
} else {
$hadParentSelector = true;
// the new list of selectors to add
$selectorsMultiplied = array();
$selectorsMultiplied = [];
// merge the current list of non parent selector elements
// on to the current list of selectors to add
if ( $currentElements ) {
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
}
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
// loop through our current selectors
foreach ( $newSelectors as $sel ) {
// if we don't have any parent paths, the & might be in a mixin so that it can be used
// whether there are parents or not
if ( !$context ) {
// the combinator used on el should now be applied to the next element instead so that
// it is not lost
if ( $sel ) {
$sel[0]->elements = array_slice( $sel[0]->elements, 0 );
$sel[0]->elements[] = new Less_Tree_Element( $el->combinator, '', $el->index, $el->currentFileInfo );
}
$selectorsMultiplied[] = $sel;
} else {
// and the parent selectors
foreach ( $context as $parentSel ) {
// We need to put the current selectors
// then join the last selector's elements on to the parents selectors
// our new selector path
$newSelectorPath = array();
// selectors from the parent after the join
$afterParentJoin = array();
$newJoinedSelectorEmpty = true;
// construct the joined selector - if & is the first thing this will be empty,
// if not newJoinedSelector will be the last set of elements in the selector
if ( $sel ) {
$newSelectorPath = $sel;
$lastSelector = array_pop( $newSelectorPath );
$newJoinedSelector = $selector->createDerived( array_slice( $lastSelector->elements, 0 ) );
$newJoinedSelectorEmpty = false;
} else {
$newJoinedSelector = $selector->createDerived( array() );
}
// put together the parent selectors after the join
if ( count( $parentSel ) > 1 ) {
$afterParentJoin = array_merge( $afterParentJoin, array_slice( $parentSel, 1 ) );
}
if ( $parentSel ) {
$newJoinedSelectorEmpty = false;
// join the elements so far with the first part of the parent
$newJoinedSelector->elements[] = new Less_Tree_Element( $el->combinator, $parentSel[0]->elements[0]->value, $el->index, $el->currentFileInfo );
$newJoinedSelector->elements = array_merge( $newJoinedSelector->elements, array_slice( $parentSel[0]->elements, 1 ) );
}
if ( !$newJoinedSelectorEmpty ) {
// now add the joined selector
$newSelectorPath[] = $newJoinedSelector;
}
// and the rest of the parent
$newSelectorPath = array_merge( $newSelectorPath, $afterParentJoin );
$newSelectorPath = $this->addReplacementIntoPath( $sel, $parentSel, $el, $inSelector );
// add that to our new set of selectors
$selectorsMultiplied[] = $newSelectorPath;
}
@ -585,30 +594,130 @@ class Less_Tree_Ruleset extends Less_Tree {
// our new selectors has been multiplied, so reset the state
$newSelectors = $selectorsMultiplied;
$currentElements = array();
$currentElements = [];
}
}
// if we have any elements left over (e.g. .a& .b == .b)
// add them on to all the current selectors
if ( $currentElements ) {
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
}
foreach ( $newSelectors as $new_sel ) {
if ( $new_sel ) {
$paths[] = $new_sel;
$this->mergeElementsOnToSelectors( $currentElements, $newSelectors );
foreach ( $newSelectors as &$sel ) {
$length = count( $sel );
if ( $length ) {
$paths[] = $sel;
$lastSelector = $sel[$length - 1];
$sel[$length - 1] = $lastSelector->createDerived( $lastSelector->elements, $inSelector->extendList );
}
}
return $hadParentSelector;
}
/**
* @param array $elementsToPak
* @param Less_Tree_Element $originalElement
* @return Less_Tree_Selector
*/
private function createSelector( array $elementsToPak, $originalElement ) {
if ( !$elementsToPak ) {
// This is an invalid call. Kept to match less.js. Appears unreachable.
// @phan-suppress-next-line PhanTypeMismatchArgumentProbablyReal
$containedElement = new Less_Tree_Paren( null );
} else {
$insideParent = [];
foreach ( $elementsToPak as $elToPak ) {
$insideParent[] = new Less_Tree_Element( null, $elToPak, $originalElement->index, $originalElement->currentFileInfo );
}
$containedElement = new Less_Tree_Paren( new Less_Tree_Selector( $insideParent ) );
}
$element = new Less_Tree_Element( null, $containedElement, $originalElement->index, $originalElement->currentFileInfo );
return new Less_Tree_Selector( [ $element ] );
}
/**
* @param Less_Tree_Element $element
* @return Less_Tree_Selector|null
*/
private function findNestedSelector( $element ) {
$maybeParen = $element->value;
if ( !( $maybeParen instanceof Less_Tree_Paren ) ) {
return null;
}
$maybeSelector = $maybeParen->value;
if ( !( $maybeSelector instanceof Less_Tree_Selector ) ) {
return null;
}
return $maybeSelector;
}
/**
* joins selector path from $beginningPath with selector path in $addPath.
*
* $replacedElement contains the element that is being replaced by $addPath
*
* @param Less_Tree_Selector[] $beginningPath
* @param Less_Tree_Selector[] $addPath
* @param Less_Tree_Element $replacedElement
* @param Less_Tree_Selector $originalSelector
* @return Less_Tree_Selector[] Concatenated path
* @see less-2.5.3.js#Ruleset-addReplacementIntoPath
*/
private function addReplacementIntoPath( array $beginningPath, array $addPath, $replacedElement, $originalSelector ) {
// our new selector path
$newSelectorPath = [];
// construct the joined selector - if `&` is the first thing this will be empty,
// if not newJoinedSelector will be the last set of elements in the selector
if ( $beginningPath ) {
// NOTE: less.js uses Array slice() to copy. In PHP, arrays are naturally copied by value.
$newSelectorPath = $beginningPath;
$lastSelector = array_pop( $newSelectorPath );
$newJoinedSelector = $originalSelector->createDerived( $lastSelector->elements );
} else {
$newJoinedSelector = $originalSelector->createDerived( [] );
}
if ( $addPath ) {
// if the & does not have a combinator that is "" or " " then
// and there is a combinator on the parent, then grab that.
// this also allows `+ a { & .b { .a & { ...`
$combinator = $replacedElement->combinator;
$parentEl = $addPath[0]->elements[0];
if ( $replacedElement->combinatorIsEmptyOrWhitespace && !$parentEl->combinatorIsEmptyOrWhitespace ) {
$combinator = $parentEl->combinator;
}
// join the elements so far with the first part of the parent
$newJoinedSelector->elements[] = new Less_Tree_Element( $combinator, $parentEl->value, $replacedElement->index, $replacedElement->currentFileInfo );
$newJoinedSelector->elements = array_merge(
$newJoinedSelector->elements,
array_slice( $addPath[0]->elements, 1 )
);
}
// now add the joined selector - but only if it is not empty
if ( $newJoinedSelector->elements ) {
$newSelectorPath[] = $newJoinedSelector;
}
// put together the parent selectors after the join (e.g. the rest of the parent)
if ( count( $addPath ) > 1 ) {
$newSelectorPath = array_merge( $newSelectorPath, array_slice( $addPath, 1 ) );
}
return $newSelectorPath;
}
function mergeElementsOnToSelectors( $elements, &$selectors ) {
if ( !$elements ) {
return;
}
if ( !$selectors ) {
$selectors[] = array( new Less_Tree_Selector( $elements ) );
$selectors[] = [ new Less_Tree_Selector( $elements ) ];
return;
}
foreach ( $selectors as &$sel ) {
// if the previous thing in sel is a parent this needs to join on to it
if ( $sel ) {
$last = count( $sel ) - 1;

View file

@ -1,16 +1,15 @@
<?php
/**
* RulesetCall
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_RulesetCall extends Less_Tree {
public $variable;
public $type = "RulesetCall";
/**
* @param string $variable
*/
public function __construct( $variable ) {
$this->variable = $variable;
}
@ -21,6 +20,7 @@ class Less_Tree_RulesetCall extends Less_Tree {
public function compile( $env ) {
$variable = new Less_Tree_Variable( $this->variable );
$detachedRuleset = $variable->compile( $env );
'@phan-var Less_Tree_DetachedRuleset $detachedRuleset';
return $detachedRuleset->callEval( $env );
}
}

View file

@ -1,21 +1,17 @@
<?php
/**
* Selector
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Selector extends Less_Tree {
public $elements;
public $condition;
public $extendList = array();
public $extendList = [];
public $_css;
public $index;
public $evaldCondition = false;
public $type = 'Selector';
public $currentFileInfo = array();
public $currentFileInfo = [];
public $isReferenced;
public $mediaEmpty;
@ -27,9 +23,9 @@ class Less_Tree_Selector extends Less_Tree {
public $cacheable = true;
/**
* @param boolean $isReferenced
* @param bool $isReferenced
*/
public function __construct( $elements, $extendList = array(), $condition = null, $index = null, $currentFileInfo = null, $isReferenced = null ) {
public function __construct( $elements, $extendList = [], $condition = null, $index = null, $currentFileInfo = null, $isReferenced = null ) {
$this->elements = $elements;
$this->elements_len = count( $elements );
$this->extendList = $extendList;
@ -58,8 +54,16 @@ class Less_Tree_Selector extends Less_Tree {
}
public function createDerived( $elements, $extendList = null, $evaldCondition = null ) {
$newSelector = new Less_Tree_Selector( $elements, ( $extendList ? $extendList : $this->extendList ), null, $this->index, $this->currentFileInfo, $this->isReferenced );
$newSelector->evaldCondition = $evaldCondition ? $evaldCondition : $this->evaldCondition;
$newSelector = new Less_Tree_Selector(
$elements,
( $extendList ?: $this->extendList ),
null,
$this->index,
$this->currentFileInfo,
$this->isReferenced
);
$newSelector->evaldCondition = $evaldCondition ?: $this->evaldCondition;
$newSelector->mediaEmpty = $this->mediaEmpty;
return $newSelector;
}
@ -78,8 +82,8 @@ class Less_Tree_Selector extends Less_Tree {
}
public function CacheElements() {
$this->_oelements = array();
$this->_oelements_assoc = array();
$this->_oelements = [];
$this->_oelements_assoc = [];
$css = '';
@ -119,12 +123,12 @@ class Less_Tree_Selector extends Less_Tree {
}
public function compile( $env ) {
$elements = array();
$elements = [];
foreach ( $this->elements as $el ) {
$elements[] = $el->compile( $env );
}
$extendList = array();
$extendList = [];
foreach ( $this->extendList as $el ) {
$extendList[] = $el->compile( $el );
}

View file

@ -1,10 +1,6 @@
<?php
/**
* UnicodeDescriptor
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_UnicodeDescriptor extends Less_Tree {
@ -21,8 +17,4 @@ class Less_Tree_UnicodeDescriptor extends Less_Tree {
public function genCSS( $output ) {
$output->add( $this->value );
}
public function compile() {
return $this;
}
}

View file

@ -1,19 +1,15 @@
<?php
/**
* Unit
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Unit extends Less_Tree {
var $numerator = array();
var $denominator = array();
var $numerator = [];
var $denominator = [];
public $backupUnit;
public $type = 'Unit';
public function __construct( $numerator = array(), $denominator = array(), $backupUnit = null ) {
public function __construct( $numerator = [], $denominator = [], $backupUnit = null ) {
$this->numerator = $numerator;
$this->denominator = $denominator;
$this->backupUnit = $backupUnit;
@ -39,7 +35,7 @@ class Less_Tree_Unit extends Less_Tree {
public function toString() {
$returnStr = implode( '*', $this->numerator );
foreach ( $this->denominator as $d ) {
$returnStr .= '/'.$d;
$returnStr .= '/' . $d;
}
return $returnStr;
}
@ -61,7 +57,7 @@ class Less_Tree_Unit extends Less_Tree {
public function isLength() {
$css = $this->toCSS();
return !!preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css );
return (bool)preg_match( '/px|em|%|in|cm|mm|pc|pt|ex/', $css );
}
public function isAngle() {
@ -77,7 +73,7 @@ class Less_Tree_Unit extends Less_Tree {
}
public function usedUnits() {
$result = array();
$result = [];
foreach ( Less_Tree_UnitConversions::$groups as $groupName ) {
$group = Less_Tree_UnitConversions::${$groupName};
@ -99,25 +95,25 @@ class Less_Tree_Unit extends Less_Tree {
}
public function cancel() {
$counter = array();
$counter = [];
$backup = null;
foreach ( $this->numerator as $atomicUnit ) {
if ( !$backup ) {
$backup = $atomicUnit;
}
$counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) + 1;
$counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) + 1;
}
foreach ( $this->denominator as $atomicUnit ) {
if ( !$backup ) {
$backup = $atomicUnit;
}
$counter[$atomicUnit] = ( isset( $counter[$atomicUnit] ) ? $counter[$atomicUnit] : 0 ) - 1;
$counter[$atomicUnit] = ( $counter[$atomicUnit] ?? 0 ) - 1;
}
$this->numerator = array();
$this->denominator = array();
$this->numerator = [];
$this->denominator = [];
foreach ( $counter as $atomicUnit => $count ) {
if ( $count > 0 ) {

View file

@ -1,16 +1,12 @@
<?php
/**
* UnitConversions
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_UnitConversions {
public static $groups = array( 'length','duration','angle' );
public static $groups = [ 'length','duration','angle' ];
public static $length = array(
public static $length = [
'm' => 1,
'cm' => 0.01,
'mm' => 0.001,
@ -18,18 +14,18 @@ class Less_Tree_UnitConversions {
'px' => 0.000264583, // 0.0254 / 96,
'pt' => 0.000352778, // 0.0254 / 72,
'pc' => 0.004233333, // 0.0254 / 72 * 12
);
];
public static $duration = array(
public static $duration = [
's' => 1,
'ms' => 0.001
);
];
public static $angle = array(
public static $angle = [
'rad' => 0.1591549430919, // 1/(2*M_PI),
'deg' => 0.002777778, // 1/360,
'grad' => 0.0025, // 1/400,
'turn' => 1
);
];
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Url
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Url extends Less_Tree {
@ -14,6 +10,11 @@ class Less_Tree_Url extends Less_Tree {
public $isEvald;
public $type = 'Url';
/**
* @param Less_Tree_Variable|Less_Tree_Quoted|Less_Tree_Anonymous $value
* @param array|null $currentFileInfo
* @param bool|null $isEvald
*/
public function __construct( $value, $currentFileInfo = null, $isEvald = null ) {
$this->value = $value;
$this->currentFileInfo = $currentFileInfo;
@ -34,7 +35,7 @@ class Less_Tree_Url extends Less_Tree {
}
/**
* @param Less_Functions $ctx
* @param Less_Environment $ctx
*/
public function compile( $ctx ) {
$val = $this->value->compile( $ctx );

View file

@ -1,16 +1,15 @@
<?php
/**
* Value
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Value extends Less_Tree {
public $type = 'Value';
public $value;
/**
* @param array<Less_Tree> $value
*/
public function __construct( $value ) {
$this->value = $value;
}
@ -20,7 +19,7 @@ class Less_Tree_Value extends Less_Tree {
}
public function compile( $env ) {
$ret = array();
$ret = [];
$i = 0;
foreach ( $this->value as $i => $v ) {
$ret[] = $v->compile( $env );

View file

@ -1,10 +1,6 @@
<?php
/**
* Variable
*
* @package Less
* @subpackage tree
* @private
*/
class Less_Tree_Variable extends Less_Tree {
@ -23,9 +19,18 @@ class Less_Tree_Variable extends Less_Tree {
$this->currentFileInfo = $currentFileInfo;
}
/**
* @param Less_Environment $env
* @return Less_Tree
* @see less-2.5.3.js#Ruleset.prototype.eval
*/
public function compile( $env ) {
if ( $this->name[1] === '@' ) {
$v = new Less_Tree_Variable( substr( $this->name, 1 ), $this->index + 1, $this->currentFileInfo );
// While some Less_Tree nodes have no 'value', we know these can't ocurr after a variable
// assignment (would have been a ParseError).
// TODO: Solve better (https://phabricator.wikimedia.org/T327082).
// @phan-suppress-next-line PhanUndeclaredProperty
$name = '@' . $v->compile( $env )->value;
} else {
$name = $this->name;
@ -45,7 +50,7 @@ class Less_Tree_Variable extends Less_Tree {
}
}
throw new Less_Exception_Compiler( "variable " . $name . " is undefined in file ".$this->currentFileInfo["filename"], null, $this->index, $this->currentFileInfo );
throw new Less_Exception_Compiler( "variable " . $name . " is undefined in file " . $this->currentFileInfo["filename"], null, $this->index, $this->currentFileInfo );
}
}

View file

@ -2,13 +2,10 @@
/**
* Release numbers
*
* @package Less
* @subpackage version
*/
class Less_Version {
public const version = '3.1.0'; // The current build number of less.php
public const version = '3.2.1'; // The current build number of less.php
public const less_version = '2.5.3'; // The less.js version that this build should be compatible with
public const cache_version = '253'; // The parser cache version

View file

@ -1,15 +1,11 @@
<?php
/**
* Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_Visitor {
protected $methods = array();
protected $_visitFnCache = array();
protected $methods = [];
protected $_visitFnCache = [];
public function __construct() {
$this->_visitFnCache = get_class_methods( get_class( $this ) );
@ -17,7 +13,7 @@ class Less_Visitor {
}
public function visitObj( $node ) {
$funcName = 'visit'.$node->type;
$funcName = 'visit' . $node->type;
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$visitDeeper = true;
@ -27,7 +23,7 @@ class Less_Visitor {
$node->accept( $this );
}
$funcName = $funcName . "Out";
$funcName .= "Out";
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$this->$funcName( $node );
}
@ -40,7 +36,9 @@ class Less_Visitor {
}
public function visitArray( $nodes ) {
array_map( array( $this,'visitObj' ), $nodes );
foreach ( $nodes as $node ) {
$this->visitObj( $node );
}
return $nodes;
}
}

View file

@ -1,20 +1,16 @@
<?php
/**
* Extend Finder Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_Visitor_extendFinder extends Less_Visitor {
public $contexts = array();
public $contexts = [];
public $allExtendsStack;
public $foundExtends;
public function __construct() {
$this->contexts = array();
$this->allExtendsStack = array( array() );
$this->contexts = [];
$this->allExtendsStack = [ [] ];
parent::__construct();
}
@ -40,7 +36,7 @@ class Less_Visitor_extendFinder extends Less_Visitor {
return;
}
$allSelectorsExtendList = array();
$allSelectorsExtendList = [];
// get &:extend(.a); rules which apply to all selectors in this ruleset
if ( $rulesetNode->rules ) {
@ -90,7 +86,7 @@ class Less_Visitor_extendFinder extends Less_Visitor {
}
public function visitMedia( $mediaNode ) {
$mediaNode->allExtends = array();
$mediaNode->allExtends = [];
$this->allExtendsStack[] =& $mediaNode->allExtends;
}
@ -99,7 +95,7 @@ class Less_Visitor_extendFinder extends Less_Visitor {
}
public function visitDirective( $directiveNode ) {
$directiveNode->allExtends = array();
$directiveNode->allExtends = [];
$this->allExtendsStack[] =& $directiveNode->allExtends;
}

View file

@ -1,14 +1,10 @@
<?php
/**
* Join Selector Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_Visitor_joinSelector extends Less_Visitor {
public $contexts = array( array() );
public $contexts = [ [] ];
/**
* @param Less_Tree_Ruleset $root
@ -26,31 +22,34 @@ class Less_Visitor_joinSelector extends Less_Visitor {
}
public function visitRuleset( $rulesetNode ) {
$paths = array();
$context = end( $this->contexts );
$paths = [];
if ( !$rulesetNode->root ) {
$selectors = array();
if ( $rulesetNode->selectors && $rulesetNode->selectors ) {
foreach ( $rulesetNode->selectors as $selector ) {
$selectors = $rulesetNode->selectors;
if ( $selectors !== null ) {
$filtered = [];
foreach ( $selectors as $selector ) {
if ( $selector->getIsOutput() ) {
$selectors[] = $selector;
$filtered[] = $selector;
}
}
$selectors = $rulesetNode->selectors = $filtered ?: null;
if ( $selectors ) {
$paths = $rulesetNode->joinSelectors( $context, $selectors );
}
}
if ( !$selectors ) {
$rulesetNode->selectors = null;
if ( $selectors === null ) {
$rulesetNode->rules = null;
} else {
$context = end( $this->contexts ); // $context = $this->contexts[ count($this->contexts) - 1];
$paths = $rulesetNode->joinSelectors( $context, $selectors );
}
$rulesetNode->paths = $paths;
}
$this->contexts[] = $paths; // different from less.js. Placed after joinSelectors() so that $this->contexts will get correct $paths
// NOTE: Assigned here instead of at the start like less.js,
// because PHP arrays aren't by-ref
$this->contexts[] = $paths;
}
public function visitRulesetOut() {
@ -58,9 +57,9 @@ class Less_Visitor_joinSelector extends Less_Visitor {
}
public function visitMedia( $mediaNode ) {
$context = end( $this->contexts ); // $context = $this->contexts[ count($this->contexts) - 1];
$context = end( $this->contexts );
if ( !count( $context ) || ( is_object( $context[0] ) && $context[0]->multiMedia ) ) {
if ( count( $context ) === 0 || ( is_object( $context[0] ) && $context[0]->multiMedia ) ) {
$mediaNode->rules[0]->root = true;
}
}

View file

@ -1,10 +1,6 @@
<?php
/**
* Process Extends Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_Visitor_processExtends extends Less_Visitor {
@ -22,7 +18,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
$root->allExtends = $this->doExtendChaining( $root->allExtends, $root->allExtends );
$this->allExtendsStack = array();
$this->allExtendsStack = [];
$this->allExtendsStack[] = &$root->allExtends;
return $this->visitObj( $root );
@ -38,7 +34,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
// this is also the most expensive.. and a match on one selector can cause an extension of a selector we had already processed if
// we look at each selector at a time, as is done in visitRuleset
$extendsToAdd = array();
$extendsToAdd = [];
// loop through comparing every extend with every target extend.
// a target extend is the one on the ruleset we are looking at copy/edit/pasting in place
@ -52,14 +48,14 @@ class Less_Visitor_processExtends extends Less_Visitor {
$extend = $extendsList[$extendIndex];
$targetExtend = $extendsListTarget[$targetExtendIndex];
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
if ( \array_key_exists( $targetExtend->object_id, $extend->parent_ids ) ) {
// ignore circular references
continue;
}
// find a match in the target extends self selector (the bit before :extend)
$selectorPath = array( $targetExtend->selfSelectors[0] );
$selectorPath = [ $targetExtend->selfSelectors[0] ];
$matches = $this->findMatch( $extend, $selectorPath );
if ( $matches ) {
@ -75,7 +71,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
$newExtend->selfSelectors = $newSelector;
// add the extend onto the list of extends for that selector
end( $newSelector )->extendList = array( $newExtend );
end( $newSelector )->extendList = [ $newExtend ];
// $newSelector[ count($newSelector)-1]->extendList = array($newExtend);
// record that we need to add it.
@ -171,6 +167,11 @@ class Less_Visitor_processExtends extends Less_Visitor {
}
}
/**
* @param Less_Tree_Extend $extend
* @param Less_Tree_Selector[] $haystackSelectorPath
* @return false|array<array{index:int,initialCombinator:string}>
*/
private function findMatch( $extend, $haystackSelectorPath ) {
if ( !$this->HasMatches( $extend, $haystackSelectorPath ) ) {
return false;
@ -181,10 +182,10 @@ class Less_Visitor_processExtends extends Less_Visitor {
// returns an array of selector matches that can then be replaced
//
$needleElements = $extend->selector->elements;
$potentialMatches = array();
$potentialMatches = [];
$potentialMatches_len = 0;
$potentialMatch = null;
$matches = array();
$matches = [];
// loop through the haystack elements
$haystack_path_len = count( $haystackSelectorPath );
@ -198,7 +199,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
// if we allow elements before our match we can add a potential match every time. otherwise only at the first element.
if ( $extend->allowBefore || ( $haystackSelectorIndex === 0 && $hackstackElementIndex === 0 ) ) {
$potentialMatches[] = array( 'pathIndex' => $haystackSelectorIndex, 'index' => $hackstackElementIndex, 'matched' => 0, 'initialCombinator' => $haystackElement->combinator );
$potentialMatches[] = [ 'pathIndex' => $haystackSelectorIndex, 'index' => $hackstackElementIndex, 'matched' => 0, 'initialCombinator' => $haystackElement->combinator ];
$potentialMatches_len++;
}
@ -222,7 +223,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
$potentialMatch['length'] = $extend->selector->elements_len;
$potentialMatch['endPathIndex'] = $haystackSelectorIndex;
$potentialMatch['endPathElementIndex'] = $hackstackElementIndex + 1; // index after end of match
$potentialMatches = array(); // we don't allow matches to overlap, so start matching again
$potentialMatches = []; // we don't allow matches to overlap, so start matching again
$potentialMatches_len = 0;
$matches[] = $potentialMatch;
}
@ -253,7 +254,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
return true;
}
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
// Optimisation: Explicit reference, <https://github.com/wikimedia/less.php/pull/14>
if ( \array_key_exists( $first_el, $hackstackSelector->_oelements_assoc ) ) {
return true;
}
@ -263,7 +264,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
}
/**
* @param integer $hackstackElementIndex
* @param int $hackstackElementIndex
*/
private function PotentialMatch( $potentialMatch, $needleElements, $haystackElement, $hackstackElementIndex ) {
if ( $potentialMatch['matched'] > 0 ) {
@ -292,6 +293,11 @@ class Less_Visitor_processExtends extends Less_Visitor {
return $potentialMatch;
}
/**
* @param string|Less_Tree_Attribute|Less_Tree_Dimension|Less_Tree_Keyword $elementValue1
* @param string|Less_Tree_Attribute|Less_Tree_Dimension|Less_Tree_Keyword $elementValue2
* @return bool
*/
private function isElementValuesEqual( $elementValue1, $elementValue2 ) {
if ( $elementValue1 === $elementValue2 ) {
return true;
@ -353,8 +359,8 @@ class Less_Visitor_processExtends extends Less_Visitor {
return true;
}
$elementValue1 = ( $elementValue1->value->value ? $elementValue1->value->value : $elementValue1->value );
$elementValue2 = ( $elementValue2->value->value ? $elementValue2->value->value : $elementValue2->value );
$elementValue1 = ( $elementValue1->value->value ?: $elementValue1->value );
$elementValue2 = ( $elementValue2->value->value ?: $elementValue2->value );
return $elementValue1 === $elementValue2;
}
@ -364,7 +370,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
$currentSelectorPathIndex = 0;
$currentSelectorPathElementIndex = 0;
$path = array();
$path = [];
$selectorPath_len = count( $selectorPath );
for ( $matchIndex = 0, $matches_len = count( $matches ); $matchIndex < $matches_len; $matchIndex++ ) {
@ -388,7 +394,7 @@ class Less_Visitor_processExtends extends Less_Visitor {
$newElements = array_merge(
array_slice( $selector->elements, $currentSelectorPathElementIndex, ( $match['index'] - $currentSelectorPathElementIndex ) ), // last parameter of array_slice is different than the last parameter of javascript's slice
array( $firstElement ),
[ $firstElement ],
array_slice( $replacementSelector->elements, 1 )
);

View file

@ -1,10 +1,6 @@
<?php
/**
* toCSS Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_Visitor_toCSS extends Less_VisitorReplacing {
@ -23,7 +19,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
public function visitRule( $ruleNode ) {
if ( $ruleNode->variable ) {
return array();
return [];
}
return $ruleNode;
}
@ -31,17 +27,17 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
public function visitMixinDefinition( $mixinNode ) {
// mixin definitions do not get eval'd - this means they keep state
// so we have to clear that state here so it isn't used if toCSS is called twice
$mixinNode->frames = array();
return array();
$mixinNode->frames = [];
return [];
}
public function visitExtend() {
return array();
return [];
}
public function visitComment( $commentNode ) {
if ( $commentNode->isSilent() ) {
return array();
return [];
}
return $commentNode;
}
@ -51,14 +47,14 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
$visitDeeper = false;
if ( !$mediaNode->rules ) {
return array();
return [];
}
return $mediaNode;
}
public function visitDirective( $directiveNode ) {
if ( isset( $directiveNode->currentFileInfo['reference'] ) && ( !property_exists( $directiveNode, 'isReferenced' ) || !$directiveNode->isReferenced ) ) {
return array();
return [];
}
if ( $directiveNode->name === '@charset' ) {
// Only output the debug info together with subsequent @charset definitions
@ -72,7 +68,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
// return $this->visit($comment);
//}
return array();
return [];
}
$this->charset = true;
}
@ -86,7 +82,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
foreach ( $rulesetNode->rules as $ruleNode ) {
if ( $ruleNode instanceof Less_Tree_Rule && !$ruleNode->variable ) {
$msg = "properties must be inside selector blocks, they cannot be in the root. Index ".$ruleNode->index.( $ruleNode->currentFileInfo ? ( ' Filename: '.$ruleNode->currentFileInfo['filename'] ) : null );
$msg = "properties must be inside selector blocks, they cannot be in the root. Index " . $ruleNode->index . ( $ruleNode->currentFileInfo ? ( ' Filename: ' . $ruleNode->currentFileInfo['filename'] ) : null );
throw new Less_Exception_Compiler( $msg );
}
}
@ -101,7 +97,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
return $this->visitRulesetRoot( $rulesetNode );
}
$rulesets = array();
$rulesets = [];
$rulesetNode->paths = $this->visitRulesetPaths( $rulesetNode );
// Compile rules and rulesets
@ -134,7 +130,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
// now decide whether we keep the ruleset
if ( $rulesetNode->paths ) {
// array_unshift($rulesets, $rulesetNode);
array_splice( $rulesets, 0, 0, array( $rulesetNode ) );
array_splice( $rulesets, 0, 0, [ $rulesetNode ] );
}
}
@ -156,7 +152,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
if ( $rulesetNode->firstRoot || $rulesetNode->rules ) {
return $rulesetNode;
}
return array();
return [];
}
/**
@ -165,7 +161,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
* @return array
*/
private function visitRulesetPaths( $rulesetNode ) {
$paths = array();
$paths = [];
foreach ( $rulesetNode->paths as $p ) {
if ( $p[0]->elements[0]->combinator === ' ' ) {
$p[0]->elements[0]->combinator = '';
@ -184,7 +180,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
protected function _removeDuplicateRules( &$rules ) {
// remove duplicates
$ruleCache = array();
$ruleCache = [];
for ( $i = count( $rules ) - 1; $i >= 0; $i-- ) {
$rule = $rules[$i];
if ( $rule instanceof Less_Tree_Rule || $rule instanceof Less_Tree_NameValue ) {
@ -195,7 +191,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
$ruleList =& $ruleCache[$rule->name];
if ( $ruleList instanceof Less_Tree_Rule || $ruleList instanceof Less_Tree_NameValue ) {
$ruleList = $ruleCache[$rule->name] = array( $ruleCache[$rule->name]->toCSS() );
$ruleList = $ruleCache[$rule->name] = [ $ruleCache[$rule->name]->toCSS() ];
}
$ruleCSS = $rule->toCSS();
@ -210,7 +206,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
}
protected function _mergeRules( &$rules ) {
$groups = array();
$groups = [];
// obj($rules);
@ -226,7 +222,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
}
if ( !isset( $groups[$key] ) ) {
$groups[$key] = array();
$groups[$key] = [];
} else {
array_splice( $rules, $i--, 1 );
$rules_len--;
@ -240,15 +236,15 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
if ( count( $parts ) > 1 ) {
$rule = $parts[0];
$spacedGroups = array();
$lastSpacedGroup = array();
$parts_mapped = array();
$spacedGroups = [];
$lastSpacedGroup = [];
$parts_mapped = [];
foreach ( $parts as $p ) {
if ( $p->merge === '+' ) {
if ( $lastSpacedGroup ) {
$spacedGroups[] = self::toExpression( $lastSpacedGroup );
}
$lastSpacedGroup = array();
$lastSpacedGroup = [];
}
$lastSpacedGroup[] = $p;
}
@ -257,11 +253,10 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
$rule->value = self::toValue( $spacedGroups );
}
}
}
public static function toExpression( $values ) {
$mapped = array();
$mapped = [];
foreach ( $values as $p ) {
$mapped[] = $p->value;
}
@ -271,7 +266,7 @@ class Less_Visitor_toCSS extends Less_VisitorReplacing {
public static function toValue( $values ) {
// return new Less_Tree_Value($values); ??
$mapped = array();
$mapped = [];
foreach ( $values as $p ) {
$mapped[] = $p;
}

View file

@ -1,15 +1,11 @@
<?php
/**
* Replacing Visitor
*
* @package Less
* @subpackage visitor
* @private
*/
class Less_VisitorReplacing extends Less_Visitor {
public function visitObj( $node ) {
$funcName = 'visit'.$node->type;
$funcName = 'visit' . $node->type;
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$visitDeeper = true;
@ -20,7 +16,7 @@ class Less_VisitorReplacing extends Less_Visitor {
$node->accept( $this );
}
$funcName = $funcName . "Out";
$funcName .= "Out";
if ( isset( $this->_visitFnCache[$funcName] ) ) {
$this->$funcName( $node );
}
@ -34,7 +30,7 @@ class Less_VisitorReplacing extends Less_Visitor {
}
public function visitArray( $nodes ) {
$newNodes = array();
$newNodes = [];
foreach ( $nodes as $node ) {
$evald = $this->visitObj( $node );
if ( $evald ) {

View file

@ -1,2 +0,0 @@
.easymin
Autoloader.php

View file

@ -0,0 +1,197 @@
Less.php API
========
## Basic use
#### Parse strings
```php
$parser = new Less_Parser();
$parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
$css = $parser->getCss();
```
#### Parse files
The `parseFile()` function takes two parameters:
* The absolute path to a `.less` file.
* The base URL for any relative image or CSS references in the `.less` file,
typically the same directory that contains the `.less` file or a public equivalent.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
```
#### Handle invalid syntax
An exception will be thrown if the compiler encounters invalid LESS.
```php
try{
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
} catch (Exception $e) {
echo $e->getMessage();
}
```
#### Parse multiple inputs
Less.php can parse multiple input sources (e.g. files and/or strings) and generate a single CSS output.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
$css = $parser->getCss();
```
#### Metadata
Less.php keeps track of which `.less` files have been parsed, i.e. the input
file(s) and any direct and indirect imports.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
$files = $parser->AllParsedFiles();
```
#### Compress output
You can tell Less.php to remove comments and whitespace to generate minified CSS.
```php
$options = [ 'compress' => true ];
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Get variables
You can use the `getVariables()` method to get an all variables defined and
their value in an associative array. Note that the input must be compiled first
by calling `getCss()`.
```php
$parser = new Less_Parser;
$parser->parseFile( '/var/www/mysite/bootstrap.less');
$css = $parser->getCss();
$variables = $parser->getVariables();
```
#### Set variables
Use the `ModifyVars()` method to inject additional variables, i.e. custom values
computed or accessed from your PHP code.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->ModifyVars( [ 'font-size-base' => '16px' ] );
$css = $parser->getCss();
```
#### Import directories
By default, Less.php will look for imported files in the directory of the file passed to `parseFile()`.
If you use `parse()`, or if need to enable additional import directories, you can specify these by
calling `SetImportDirs()`.
```php
$directories = [ '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' ];
$parser = new Less_Parser();
$parser->SetImportDirs( $directories );
$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
$css = $parser->getCss();
```
## Caching
Compiling LESS code into CSS can be a time-consuming process. It is recommended to cache your results.
#### Basic cache
Use the `Less_Cache` class to save and reuse the results of compiling LESS files.
This class will check the modified time and size of each LESS file (including imported files) and
either re-use or re-generate the CSS output accordingly.
The cache files are determinstically named, based on the full list of referenced LESS files and the metadata (file path, file mtime, file size) of each file. This means that each time a change is made, a different cache filename is used.
```php
$lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$cssOutputFile = Less_Cache::Get( $lessFiles, $options );
$css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
```
#### Caching with variables
Passing custom variables to `Less_Cache::Get()`:
```php
$lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$variables = [ 'width' => '100px' ];
$cssOutputFile = Less_Cache::Get( $lessFiles, $options, $variables );
$css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
```
#### Incremental caching
In addition to the whole-output caching described above, Less.php also has the ability to keep an internal cache which allows re-parses to be faster by effectively only re-compiling portions that have changed.
## Source maps
Less.php supports v3 sourcemaps.
#### Inline
The sourcemap will be appended to the generated CSS file.
```php
$options = [ 'sourceMap' => true ];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Saving to map file
```php
$options = [
'sourceMap' => true,
'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map',
'sourceMapURL' => '/mysite/writable_folder/filename.map',
];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
## Command line
An additional script has been included to use the Less.php compiler from the command line.
In its simplest invocation, you specify an input file and the compiled CSS is written to standard out:
```
$ lessc input.less > output.css
```
By using the `-w` flag you can watch a specified input file and have it compile as needed to the output file:
```
$ lessc -w input.less output.css
```
Errors from watch mode are written to standard out.
For more information, run `lessc --help`

View file

@ -0,0 +1,91 @@
# Changelog
## 4.1.0
* Add support for `@supports` blocks. (Anne Tomasevich) [T332923](http://phabricator.wikimedia.org/T332923)
* Less_Parser: Returning a URI from `SetImportDirs()` callbacks is now optional. (Timo Tijhof)
## 4.0.0
* Remove support for PHP 7.2 and 7.3. Raise requirement to PHP 7.4+.
* Remove support for `cache_method=php` and `cache_method=var_export`, only the faster and more secure `cache_method=serialize` is now available. The built-in cache remains disabled by default.
* Fix `url(#myid)` to be treated as absolute URL. [T331649](https://phabricator.wikimedia.org/T331688)
* Fix "Undefined property" PHP 8.1 warning when `calc()` is used with CSS `var()`. [T331688](https://phabricator.wikimedia.org/T331688)
* Less_Parser: Improve performance by removing MatchFuncs and NewObj overhead. (Timo Tijhof)
## 3.2.1
* Tree_Ruleset: Fix support for nested parent selectors (Timo Tijhof) [T204816](https://phabricator.wikimedia.org/T204816)
* Fix ParseError when interpolating variable after colon in selector (Timo Tijhof) [T327163](https://phabricator.wikimedia.org/T327163)
* Functions: Fix "Undefined property" warning on bad minmax arg
* Tree_Call: Include previous exception when catching functions (Robert Frunzke)
## 3.2.0
* Fix "Implicit conversion" PHP 8.1 warnings (Ayokunle Odusan)
* Fix "Creation of dynamic property" PHP 8.2 warnings (Bas Couwenberg)
* Fix "Creation of dynamic property" PHP 8.2 warnings (Rajesh Kumar)
* Tree_Url: Add support for "Url" type to `Parser::getVariables()` (ciroarcadio) [#51](https://github.com/wikimedia/less.php/pull/51)
* Tree_Import: Add support for importing URLs without file extension (Timo Tijhof) [#27](https://github.com/wikimedia/less.php/issues/27)
## 3.1.0
* Add PHP 8.0 support: Drop use of curly braces for sub-string eval (James D. Forrester)
* Make `Directive::__construct` $rules arg optional (fix PHP 7.4 warning) (Sam Reed)
* ProcessExtends: Improve performance by using a map for selectors and parents (Andrey Legayev)
## 3.0.0
* Raise PHP requirement from 7.1 to 7.2.9 (James Forrester)
## 2.0.0
* Relax PHP requirement down to 7.1, from 7.2.9 (Franz Liedke)
* Reflect recent breaking changes properly with the semantic versioning (James Forrester)
## 1.8.2
* Require PHP 7.2.9+, up from 5.3+ (James Forrester)
* release: Update Version.php with the current release ID (COBadger)
* Fix access array offset on value of type null (Michele Locati)
* Fix test suite on PHP 7.4 (Sergei Morozov)
## 1.8.1
* Another PHP 7.3 compatibility tweak
## 1.8.0
Library forked by Wikimedia, from [oyejorge/less.php](https://github.com/oyejorge/less.php).
* Supports up to PHP 7.3
* No longer tested against PHP 5, though it's still remains allowed in `composer.json` for HHVM compatibility
* Switched to [semantic versioning](https://semver.org/), hence version numbers now use 3 digits
## 1.7.0.13
* Fix composer.json (PSR-4 was invalid)
## 1.7.0.12
* set bin/lessc bit executable
* Add `gettingVariables` method to `Less_Parser`
## 1.7.0.11
* Fix realpath issue (windows)
* Set Less_Tree_Call property back to public ( Fix 258 266 267 issues from oyejorge/less.php)
## 1.7.0.10
* Add indentation option
* Add `optional` modifier for `@import`
* Fix $color in Exception messages
* take relative-url into account when building the cache filename
* urlArgs should be string no array()
* fix missing on NameValue type [#269](https://github.com/oyejorge/less.php/issues/269)
## 1.7.0.9
* Remove space at beginning of Version.php
* Revert require() paths in test interface

View file

@ -0,0 +1 @@
The development of this software is covered by a [Code of Conduct](https://www.mediawiki.org/wiki/Special:MyLanguage/Code_of_Conduct).

View file

@ -9,7 +9,7 @@ class Less_Environment {
*
* - rootpath: rootpath to append to URLs
*
* @var array|null $currentFileInfo;
* @var array|null $currentFileInfo
*/
public $currentFileInfo;

View file

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,18 @@
wikimedia/less.php. https://gerrit.wikimedia.org/g/mediawiki/libs/less.php
Copyright Matt Agar <https://github.com/agar>
Copyright Martin Jantošovič <https://github.com/Mordred>
Copyright Josh Schmidt <https://github.com/oyejorge>
Copyright Timo Tijhof <https://timotijhof.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -542,7 +542,27 @@ $g = intval( $g );
/**
* Set a list of directories or callbacks the parser should use for determining import paths
*
* @param array $dirs
* Import closures are called with a single `$path` argument containing the unquoted `@import`
* string an input LESS file. The string is unchanged, except for a statically appended ".less"
* suffix if the basename does not yet contain a dot. If a dot is present in the filename, you
* are responsible for choosing whether to expand "foo.bar" to "foo.bar.less". If your callback
* can handle this import statement, return an array with an absolute file path and an optional
* URI path, or return void/null to indicate that your callback does not handle this import
* statement.
*
* Example:
*
* function ( $path ) {
* if ( $path === 'virtual/something.less' ) {
* return [ '/srv/elsewhere/thing.less', null ];
* }
* }
*
*
* @param array<string|callable> $dirs The key should be a server directory from which LESS
* files may be imported. The value is an optional public URL or URL base path that corresponds to
* the same directory (use empty string otherwise). The value may also be a closure, in
* which case the key is ignored.
*/
public function SetImportDirs( $dirs ) {
self::$options['import_dirs'] = [];
@ -2188,6 +2208,7 @@ $g = intval( $g );
$hasIdentifier = false;
$hasExpression = false;
$hasUnknown = false;
$isRooted = true;
$value = $this->parseImport() ?? $this->parseMedia();
if ( $value ) {
@ -2230,6 +2251,7 @@ $g = intval( $g );
case "@right-middle":
case "@right-bottom":
hasBlock = true;
isRooted = true;
break;
*/
case "@charset":
@ -2245,9 +2267,12 @@ $g = intval( $g );
break;
case "@host":
case "@page":
$hasUnknown = true;
break;
case "@document":
case "@supports":
$hasUnknown = true;
$isRooted = false;
break;
}
@ -2275,7 +2300,7 @@ $g = intval( $g );
if ( $rules || ( !$hasBlock && $value && $this->MatchChar( ';' ) ) ) {
$this->forget();
return new Less_Tree_Directive( $name, $value, $rules, $index, $this->env->currentFileInfo );
return new Less_Tree_Directive( $name, $value, $rules, $index, $isRooted, $this->env->currentFileInfo );
}
$this->restore();

View file

@ -0,0 +1,77 @@
[![Packagist](https://img.shields.io/packagist/v/wikimedia/less.php.svg?style=flat)](https://packagist.org/packages/wikimedia/less.php)
Less.php
========
This is a PHP port of the [official LESS processor](https://lesscss.org).
## About
The code structure of Less.php mirrors that of upstream Less.js to ensure compatibility and help reduce maintenance. The port is currently compatible with Less.js 2.5.3. Please note that "inline JavaScript expressions" (via eval or backticks) are not supported.
* [API § Caching](./API.md#caching), Less.php includes a file-based cache.
* [API § Source maps](./API.md#source-maps), Less.php supports v3 sourcemaps.
* [API § Command line](./API.md#command-line), the `lessc` command includes a watch mode.
## Installation
You can install the library with Composer or standalone.
If you have [Composer](https://getcomposer.org/download/) installed:
1. Run `composer require wikimedia/less.php`
2. Use `Less_Parser` in your code.
Or standalone:
1. [Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz) and upload the PHP files to your server.
2. Include the library:
```php
require_once '[path to]/less.php/lib/Less/Autoloader.php';
Less_Autoloader::register();
```
3. Use `Less_Parser` in your code.
## Security
The LESS processor language is powerful and includes features that may read or embed arbitrary files that the web server has access to, and features that may be computationally exensive if misused.
In general you should treat LESS files as being in the same trust domain as other server-side executables, such as PHP code. In particular, it is not recommended to allow people that use your web service to provide arbitrary LESS code for server-side processing.
_See also [SECURITY](./SECURITY.md)._
## Who uses Less.php?
* **[Wikipedia](https://en.wikipedia.org/wiki/MediaWiki)** and the MediaWiki platform ([docs](https://www.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Styles)).
* **[Matomo](https://en.wikipedia.org/wiki/Matomo_(software))** ([docs](https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/css-topics/custom_preprocess.html)).
* **[Magento](https://en.wikipedia.org/wiki/Magento)** as part of Adobe Commerce ([docs](https://developer.matomo.org/guides/asset-pipeline#vanilla-javascript-css-and-less-files)).
* **[Icinga](https://en.wikipedia.org/wiki/Icinga)** in Icinga Web ([docs](https://github.com/Icinga/icingaweb2)).
* **[Shopware](https://de.wikipedia.org/wiki/Shopware)** ([docs](https://developers.shopware.com/designers-guide/less/)).
## Integrations
Less.php has been integrated with various other projects.
#### Transitioning from Leafo/lessphp
If you're looking to transition from the [Leafo/lessphp](https://github.com/leafo/lessphp) library, use the `lessc.inc.php` adapter file that comes with Less.php.
This allows Less.php to be a drop-in replacement for Leafo/lessphp.
[Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz), unzip the files into your project, and include its `lessc.inc.php` instead.
Note: The `setPreserveComments` option is ignored. Less.php already preserves CSS block comments by default, and removes LESS inline comments.
#### Drupal
Less.php can be used with [Drupal's less module](https://drupal.org/project/less) via the `lessc.inc.php` adapter. [Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz) and unzip it so that `lessc.inc.php` is located at `sites/all/libraries/lessphp/lessc.inc.php`, then install the Drupal less module as usual.
#### WordPress
* [wp_enqueue_less](https://github.com/Ed-ITSolutions/wp_enqueue_less) is a Composer package for use in WordPress themes and plugins. It provides a `wp_enqueue_less()` function to automatically manage caching and compilation on-demand, and loads the compressed CSS on the page.
* [JBST framework](https://github.com/bassjobsen/jamedo-bootstrap-start-theme) bundles a copy of Less.php.
* The [lessphp plugin](https://wordpress.org/plugins/lessphp/) bundles a copy of Less.php for use in other plugins or themes. This dependency can also be combined with the [TGM Library](http://tgmpluginactivation.com/).
## Credits
Less.php was originally ported to PHP in 2011 by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred) in 2012. From 2013 to 2017, [Josh Schmidt](https://github.com/oyejorge) lead development of the library. Since 2019, the library is maintained by Wikimedia Foundation.

View file

@ -0,0 +1,5 @@
# Security policy
Wikimedia takes security seriously. If you believe you have found a
security issue, see <https://www.mediawiki.org/wiki/Reporting_security_bugs>
for information on how to responsibly report it.

Some files were not shown because too many files have changed in this diff Show more