Skip to content

Commit

Permalink
Fix compiling of PHP-injected variables with false, null or empty string
Browse files Browse the repository at this point in the history
Less.php 5.1.0 could no longer compile variables injected via
Less_Parser->ModifyVars if they have an empty value.

This bug was introduced in Ie8d5da3ed47b817bc9dd79070488509a7aa2feb2.
In Less.js v3 and above, this remains an unsolved bug.

In Less.js < 3.0 and Less.php < 5.1, the following could compile:

```
@foo : ;

div{
    color: @foo;
}
```

Currently, the block above returns an error. We decided to work around this by using "~" with an empty string to serialize an empty value. We caught this in CI while drafting a mediawiki/vendor patch for the
Less.php 5.1.0 upgrade at https://gerrit.wikimedia.org/r/1060459.

Change-Id: I1bafdbc527a2fef62a0eb7ed36740b38726cb1c4
  • Loading branch information
Hannah Okwelum committed Aug 8, 2024
1 parent b66f357 commit ee28276
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3222,6 +3222,9 @@ public static function serializeVars( $vars ) {
$s = '';

foreach ( $vars as $name => $value ) {
if ( strval( $value ) === "" ) {
$value = '~""';
}
$s .= ( ( $name[0] === '@' ) ? '' : '@' ) . $name . ': ' . $value . ( ( substr( $value, -1 ) === ';' ) ? '' : ';' );
}

Expand Down
26 changes: 25 additions & 1 deletion test/phpunit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,36 @@ public function testOptionRootpath() {
$this->assertEquals( $expected, $css );
}

public function testModifyVars() {
$lessCode = <<<CSS
div{
border-width: @border-width;
line-height: unit(@line-height, px);
border-radius: @border-radius;
margin: @margin;
}
CSS;
$expected = <<<CSS
div {
border-width: ;
line-height: 1px;
border-radius: ;
margin: ;
}
CSS;
$parser = new Less_Parser;
$parser->ModifyVars( [ 'border-width' => false, 'line-height' => true, 'border-radius' => null, 'margin' => '' ] );
$parser->parse( $lessCode );
$css = trim( $parser->getCss() );
$this->assertSame( $expected, $css, 'static callback' );
}

public function testOptionFunctions() {
$lessCode = <<<CSS
#test {
border-width: add(7, 6);
}
CSS;
CSS;
$expected = <<<CSS
#test {
border-width: 13;
Expand Down

0 comments on commit ee28276

Please sign in to comment.