Skip to content
This repository was archived by the owner on Dec 5, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Less/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Less_Environment{

public static $mixin_stack = 0;

/**
* @var array
*/
public $functions = array();


public function Init(){

Expand Down
18 changes: 18 additions & 0 deletions lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,25 @@ public function SetOption($option,$value){
Less_Parser::$options[$option] = $value;
}

/**
* Registers a new custom function
*
* @param string $name function name
* @param callable $callback callback
*/
public function registerFunction($name, $callback) {
$this->env->functions[$name] = $callback;
}

/**
* Removed an already registered function
*
* @param string $name function name
*/
public function unregisterFunction($name) {
if( isset($this->env->functions[$name]) )
unset($this->env->functions[$name]);
}


/**
Expand Down
6 changes: 6 additions & 0 deletions lib/Less/Tree/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public function compile($env=null){
} catch (Exception $e) {
throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
}
} elseif( isset( $env->functions[$nameLC] ) && is_callable( $env->functions[$nameLC] ) ) {
try {
$result = call_user_func_array( $env->functions[$nameLC], $args );
} catch (Exception $e) {
throw new Less_Exception_Compiler('error evaluating function `' . $this->name . '` '.$e->getMessage().' index: '. $this->index);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/Fixtures/functions/css/f1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body:before {
content: '0987654321';
}
3 changes: 3 additions & 0 deletions test/Fixtures/functions/less/f1.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body:before {
content: myfunc-reverse('1234567890');
}
29 changes: 29 additions & 0 deletions test/phpunit/FunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class phpunit_FunctionTest extends phpunit_bootstrap{
/**
* Test
*/
public function testFunction() {
echo "\nBegin Tests";

$less_file = $this->fixtures_dir.'/functions/less/f1.less';
$expected_css = file_get_contents( $this->fixtures_dir.'/functions/css/f1.css' );

$parser = new Less_Parser();

$parser->registerFunction( 'myfunc-reverse', array( __CLASS__, 'reverse' ) );

$parser->parseFile( $less_file );
$generated_css = $parser->getCss();

$this->assertEquals( $expected_css, $generated_css );
}

public static function reverse( $arg ) {
if( is_a( $arg, 'Less_Tree_Quoted' ) ) {
$arg->value = strrev( $arg->value );
return $arg;
}
}
}