From ee282761d9d1b841c2e912faa335d521f154c93e Mon Sep 17 00:00:00 2001 From: Hannah Okwelum Date: Thu, 8 Aug 2024 21:53:06 +0100 Subject: [PATCH] Fix compiling of PHP-injected variables with false, null or empty string 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 --- lib/Less/Parser.php | 3 +++ test/phpunit/ParserTest.php | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Less/Parser.php b/lib/Less/Parser.php index c788e153..1432a479 100644 --- a/lib/Less/Parser.php +++ b/lib/Less/Parser.php @@ -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 ) === ';' ) ? '' : ';' ); } diff --git a/test/phpunit/ParserTest.php b/test/phpunit/ParserTest.php index 176a7f09..146aa27c 100644 --- a/test/phpunit/ParserTest.php +++ b/test/phpunit/ParserTest.php @@ -277,12 +277,36 @@ public function testOptionRootpath() { $this->assertEquals( $expected, $css ); } + public function testModifyVars() { + $lessCode = <<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 = <<