From 2f7b8966cf0cf8dd16c4c7631c3be6205a177aeb Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 6 Nov 2026 11:15:25 +0000 Subject: [PATCH 1/3] test: reproduce space-separated class binding failure --- test/phpunit/DocumentBinderTest.php | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/phpunit/DocumentBinderTest.php b/test/phpunit/DocumentBinderTest.php index 8085189..598055b 100644 --- a/test/phpunit/DocumentBinderTest.php +++ b/test/phpunit/DocumentBinderTest.php @@ -514,6 +514,43 @@ public function testBindKeyValue_classToggle():void { self::assertSame("secondary", $img->className); } + public function testBindKeyValue_multipleClassToggles():void { + $document = new HTMLDocument('0,000'); + $sut = new DocumentBinder($document); + $sut->setDependencies(...$this->documentBinderDependencies($document)); + $span = $document->querySelector("span"); + + $sut->bindKeyValue("overweight", true); + self::assertTrue($span->classList->contains("overweight")); + self::assertFalse($span->classList->contains("oversize")); + self::assertSame(":oversize", $span->getAttribute("data-bind:class")); + + $sut->bindKeyValue("oversize", true); + self::assertTrue($span->classList->contains("overweight")); + self::assertTrue($span->classList->contains("oversize")); + self::assertTrue($span->classList->contains("existing")); + self::assertFalse($span->hasAttribute("data-bind:class")); + } + + public function testBindKeyValue_multipleClassToggles_rebind():void { + $document = new HTMLDocument(''); + $sut = new DocumentBinder($document); + $sut->setDependencies(...$this->documentBinderDependencies($document)); + $span = $document->querySelector("span"); + + $sut->bindKeyValue("oversize", true); + $sut->bindKeyValue("overweight", true); + self::assertTrue($span->classList->contains("overweight")); + self::assertTrue($span->classList->contains("oversize")); + + $sut->bindKeyValue("overweight", false); + self::assertFalse($span->classList->contains("overweight")); + self::assertTrue($span->classList->contains("oversize")); + $sut->bindKeyValue("oversize", false); + self::assertSame("", $span->className); + self::assertSame(":overweight :oversize", $span->getAttribute("data-bind:class")); + } + public function testBindKeyValue_classToggle_differentClassNameToBindKey():void { $document = new HTMLDocument(HTMLPageContent::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); From c33d55e732cdd5fe9f3680d8e29a42c3358d53a0 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 6 Nov 2026 11:15:50 +0000 Subject: [PATCH 2/3] fix: support space-separated class binding expressions --- src/HTMLAttributeBinder.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/HTMLAttributeBinder.php b/src/HTMLAttributeBinder.php index d7df557..ece8a0f 100644 --- a/src/HTMLAttributeBinder.php +++ b/src/HTMLAttributeBinder.php @@ -44,7 +44,12 @@ public function bind( } $bindProperty = $this->getBindProperty($element, $attributeName); - [$modifier, $remainingExpressions] = $this->resolveModifier($key, $attribute->value); + $attributeValue = $attribute->value; + if(strtolower($bindProperty) === "class") { + // A new colon modifier starts another class binding expression. + $attributeValue = preg_replace('/\s+(?=!:|:)/', '; ', $attributeValue) ?? $attributeValue; + } + [$modifier, $remainingExpressions] = $this->resolveModifier($key, $attributeValue); if($modifier === false) { continue; } From a52b5079272da31ba8d4adb6b39a71551cf81a24 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 6 Nov 2026 11:47:39 +0000 Subject: [PATCH 3/3] fix: recognise question mark and inverse class binding modifiers --- src/HTMLAttributeBinder.php | 4 ++-- test/phpunit/DocumentBinderTest.php | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/HTMLAttributeBinder.php b/src/HTMLAttributeBinder.php index ece8a0f..1e07942 100644 --- a/src/HTMLAttributeBinder.php +++ b/src/HTMLAttributeBinder.php @@ -46,8 +46,8 @@ public function bind( $bindProperty = $this->getBindProperty($element, $attributeName); $attributeValue = $attribute->value; if(strtolower($bindProperty) === "class") { - // A new colon modifier starts another class binding expression. - $attributeValue = preg_replace('/\s+(?=!:|:)/', '; ', $attributeValue) ?? $attributeValue; + // A new modifier starts another class binding expression. + $attributeValue = preg_replace('/\s+(?=!?[:?])/', '; ', $attributeValue) ?? $attributeValue; } [$modifier, $remainingExpressions] = $this->resolveModifier($key, $attributeValue); if($modifier === false) { diff --git a/test/phpunit/DocumentBinderTest.php b/test/phpunit/DocumentBinderTest.php index 598055b..5373852 100644 --- a/test/phpunit/DocumentBinderTest.php +++ b/test/phpunit/DocumentBinderTest.php @@ -551,6 +551,26 @@ public function testBindKeyValue_multipleClassToggles_rebind():void { self::assertSame(":overweight :oversize", $span->getAttribute("data-bind:class")); } + public function testBindKeyValue_multipleClassBindings_mixedModifiers():void { + $document = new HTMLDocument(''); + $sut = new DocumentBinder($document); + $sut->setDependencies(...$this->documentBinderDependencies($document)); + $span = $document->querySelector("span"); + + $sut->bindKeyValue("isAdmin", "admin"); + self::assertSame("admin", $span->className); + self::assertSame(":featured; !?loggedIn", $span->getAttribute("data-bind:class")); + + $sut->bindKeyValue("featured", true); + self::assertTrue($span->classList->contains("admin")); + self::assertTrue($span->classList->contains("featured")); + self::assertSame("!?loggedIn", $span->getAttribute("data-bind:class")); + + $sut->bindKeyValue("loggedIn", true); + self::assertFalse($span->hasAttribute("class")); + self::assertFalse($span->hasAttribute("data-bind:class")); + } + public function testBindKeyValue_classToggle_differentClassNameToBindKey():void { $document = new HTMLDocument(HTMLPageContent::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document);