Skip to content

Commit edb6eca

Browse files
authored
Add support for Javascript expressions in v-html attributes (#48)
`v-html` attributes currently only support direct references to top-level variables in the template data. Use the Peast Javascript expression parsing support to make it possible to evaluate expressions in `v-html` attributes. Bug: T396098
1 parent 5b6318a commit edb6eca

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Component.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ private function handleRawHtml( DOMNode $node, array $data ) {
316316

317317
/** @var DOMElement $node */
318318
if ( $node->hasAttribute( 'v-html' ) ) {
319-
$variableName = $node->getAttribute( 'v-html' );
319+
$htmlExpression = $node->getAttribute( 'v-html' );
320320
$node->removeAttribute( 'v-html' );
321321

322-
$this->appendHTML( $node, $data[$variableName] );
322+
$this->appendHTML( $node, $this->app->evaluateExpression( $htmlExpression, $data ) );
323323
}
324324
}
325325

tests/php/TemplatingTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ public function testTemplateWithVhtmlVariable_ReplacesVariableWithGivenValue() {
102102
$this->assertSame( '<div><div><p>some value</p></div></div>', $result );
103103
}
104104

105+
public function testTemplateWithVhtmlVariableNestedData_ReplacesVariableWithGivenValue() {
106+
$result = $this->createAndRender(
107+
'<div><div v-html="value.html"></div></div>',
108+
[ 'value' => [ 'html' => '<p>some value</p>' ] ]
109+
);
110+
111+
$this->assertSame( '<div><div><p>some value</p></div></div>', $result );
112+
}
113+
105114
public function testTemplateWithVhtmlVariableAndAttributeBinding_ReplacesBoth(): void {
106115
$result = $this->createAndRender(
107116
'<div><div :data-a="a" v-html="html"></div></div>',

0 commit comments

Comments
 (0)