Skip to content

Commit 67d182b

Browse files
Handle kebab-case attributes in component calls
libxml2 throws away the capitalization of attribute names (we always see them all-lowercase), so component props must be specified in kebab-case (e.g. :property-url= rather than :propertyUrl=); for this to work, php-vuejs-templating needs to map the prop name back to camelCase somewhere. Do this in handleComponent(). (Alternatively, it could probably happen in App::renderComponentToDOM() or Component::render(), but that would suggest that PHP entry points can provide props in either format, and that feels bad to me.)
1 parent 2d28b47 commit 67d182b

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Component.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ private function handleComponent( DOMElement $node, array $data ): bool {
126126
$name = $attribute->name;
127127
$value = $attribute->value;
128128
}
129+
// template kebab-case -> JS camelCase
130+
$name = preg_replace_callback( '/-(\w)/', fn ( $m ) => strtoupper( $m[1] ), $name );
129131
$componentData[$name] = $value;
130132
}
131133
$rendered = $this->app->renderComponentToDOM( $componentName, $componentData );

tests/php/AppTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,17 @@ public function testNestedComponentObjectProp(): void {
6161
$this->assertSame( '<div><p>obj = { a: A, b: B }</p></div>', $result );
6262
}
6363

64+
public function testComponentPropKebabCase(): void {
65+
$app = new App( [] );
66+
$app->registerComponentTemplate(
67+
'root',
68+
'<div><x-a some-long-prop="A B C"></x-a><x-a :some-long-prop="someLongVar"></x-a></div>'
69+
);
70+
$app->registerComponentTemplate( 'x-a', '<p>{{ someLongProp }}</p>' );
71+
72+
$result = $app->renderComponent( 'root', [ 'someLongVar' => 'X Y Z' ] );
73+
74+
$this->assertSame( '<div><p>A B C</p><p>X Y Z</p></div>', $result );
75+
}
76+
6477
}

0 commit comments

Comments
 (0)