From 5929ddd9da3615704244d4784c996dc88c138a78 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Thu, 20 Aug 2026 10:49:20 -0400 Subject: [PATCH 1/2] fix: Ordered attributes Fix #31 --- src/Element.php | 60 +++++++++++--------------- tests/ElementExtensionBaselineTest.php | 2 +- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/src/Element.php b/src/Element.php index 3927335..45c0383 100644 --- a/src/Element.php +++ b/src/Element.php @@ -58,58 +58,48 @@ protected function omitEndTagClosingString(): string protected function propertiesString(): string { - if (count($this->properties()) === 0) { + if ($this->properties() === []) { return ''; } - $orderedAttributes = array_fill_keys(static::ORDERED, ""); - $otherAttributes = []; - $booleanAttributes = []; + $ordered = []; + $other = []; + $boolean = []; - $build = []; foreach ($this->properties() as $property) { - if (strlen($property) === 0) { + [$attr, $value] = array_pad(explode(' ', $property, 2), 2, ''); + + if ($value === '') { continue; } - list($attr, $content) = explode(' ', $property, 2); - - if (strlen($content) > 0) { - if (array_key_exists($attr, $orderedAttributes)) { - $orderedAttributes[$attr] = $content; - - } elseif ($attr === $content) { - $booleanAttributes[$attr] = $content; - - } else { - $otherAttributes[$attr] = $content; - - } + if (in_array($attr, self::ORDERED, true)) { + $ordered[$attr] = $value; + } elseif ($attr === $value) { + $boolean[$attr] = true; + } else { + $other[$attr] = $value; } } - $orderedAttributes = array_filter($orderedAttributes, fn($c) => strlen($c) > 0); + $parts = []; - ksort($otherAttributes); - - ksort($booleanAttributes); - - $b = []; - foreach ($orderedAttributes as $prop => $content) { - $b[] = $prop . '="' . $content . '"'; + foreach (self::ORDERED as $attr) { + if (isset($ordered[$attr])) { + $parts[] = $attr . '="' . $ordered[$attr] . '"'; + } } - foreach ($otherAttributes as $prop => $content) { - $b[] = $prop . '="' . $content . '"'; + ksort($other); + foreach ($other as $attr => $value) { + $parts[] = $attr . '="' . $value . '"'; } - foreach ($booleanAttributes as $prop => $content) { - $b[] = $prop; + ksort($boolean); + foreach (array_keys($boolean) as $attr) { + $parts[] = $attr; } - if (count($b) === 0) { - return ''; - } - return ' ' . implode(' ', $b); + return $parts === [] ? '' : ' ' . implode(' ', $parts); } } diff --git a/tests/ElementExtensionBaselineTest.php b/tests/ElementExtensionBaselineTest.php index 1212a3c..07f7418 100644 --- a/tests/ElementExtensionBaselineTest.php +++ b/tests/ElementExtensionBaselineTest.php @@ -14,7 +14,7 @@ class ElementExtensionBaselineTest extends TestCase public function has_ordered_properties(): void // phpcs:ignore { $exptected = <<link + link html; $result = (string) ElementExtension::a('link')->props( From 329b1d3eb9526b2034f1aba9c3fd8e3cfd32544b Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Thu, 20 Aug 2026 10:50:41 -0400 Subject: [PATCH 2/2] update: Order attr expected output --- tests/ElementExtensionBaselineTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ElementExtensionBaselineTest.php b/tests/ElementExtensionBaselineTest.php index 07f7418..b25a0c0 100644 --- a/tests/ElementExtensionBaselineTest.php +++ b/tests/ElementExtensionBaselineTest.php @@ -14,7 +14,7 @@ class ElementExtensionBaselineTest extends TestCase public function has_ordered_properties(): void // phpcs:ignore { $exptected = <<link + link html; $result = (string) ElementExtension::a('link')->props( @@ -22,7 +22,8 @@ public function has_ordered_properties(): void // phpcs:ignore 'href https://8fold.pro', 'class some-style', 'id unique', - 'data-testing test' + 'data-testing test', + 'is link' ); $this->assertSame($exptected, $result);