diff --git a/src/Component.php b/src/Component.php
index 97fc18d..cfb06a5 100644
--- a/src/Component.php
+++ b/src/Component.php
@@ -323,7 +323,10 @@ private function handleFor( DOMNode $node, array $data ) {
private function appendHTML( DOMNode $parent, $source ) {
$htmlParser = new HtmlParser();
- $tmpDoc = $htmlParser->parseHtml( $source );
+
+ // Wrapping $source in a
tag prevents the addition of an implied tag when the
+ // $source starts with plain text
+ $tmpDoc = $htmlParser->parseHtml( '
' . $source . '' );
foreach ( $htmlParser->getBodyElement( $tmpDoc )->childNodes as $node ) {
$node = $parent->ownerDocument->importNode( $node, true );
$parent->appendChild( $node );
diff --git a/tests/php/TemplatingTest.php b/tests/php/TemplatingTest.php
index 21c04ea..1ea1a97 100644
--- a/tests/php/TemplatingTest.php
+++ b/tests/php/TemplatingTest.php
@@ -93,49 +93,56 @@ public function testTemplateWithVariableAndValueInKorean_ReplacesVariableWithEnc
$this->assertSame( '한국어
', $result );
}
- public function testTemplateWithVhtmlVariable_ReplacesVariableWithGivenValue() {
- $result = $this->createAndRender(
- '',
- [ 'value' => 'some value
' ]
- );
-
- $this->assertSame( '', $result );
- }
-
- public function testTemplateWithVhtmlVariableNestedData_ReplacesVariableWithGivenValue() {
- $result = $this->createAndRender(
- '',
- [ 'value' => [ 'html' => 'some value
' ] ]
- );
-
- $this->assertSame( '', $result );
- }
-
- public function testTemplateWithVhtmlVariableAndAttributeBinding_ReplacesBoth(): void {
- $result = $this->createAndRender(
- '',
- [ 'a' => 'A', 'html' => 'HTML
' ]
- );
-
- $this->assertSame( '', $result );
- }
-
- public function testTemplateWithVhtmlAndDiacritcsInValue_ReplacesVariableWithEncodedValue() {
- $result = $this->createAndRender(
- '',
- [ 'value' => 'inglés
' ]
- );
-
- $this->assertSame( '', $result );
+ public static function provideVHtmlValues(): iterable {
+ return [
+ 'plain text does not get wrapped' => [
+ '',
+ [ 'value' => 'plain text' ],
+ 'plain text
',
+ ],
+ 'starting with plain text, containing html tag' => [
+ '',
+ [ 'value' => 'beginning text internal div
ending text' ],
+ 'beginning text
internal div
ending text
',
+ ],
+ 'variable in child node is replaced' => [
+ '',
+ [ 'value' => 'some value
' ],
+ '',
+ ],
+ 'variable with nested value' => [
+ '',
+ [ 'value' => [ 'html' => 'some value
' ] ],
+ ''
+ ],
+ 'template with v-html and attribute binding' => [
+ '',
+ [ 'a' => 'A', 'html' => 'HTML
' ],
+ ''
+ ],
+ 'with diacritics in the value' => [
+ '',
+ [ 'value' => 'inglés
' ],
+ ''
+ ],
+ 'with value in Korean' => [
+ '',
+ [ 'value' => '한국어
' ],
+ ''
+ ]
+ ];
}
- public function testTemplateWithVhtmlAndValueInKorean_ReplacesVariableWithEncodedValue() {
- $result = $this->createAndRender(
- '',
- [ 'value' => '한국어
' ]
- );
-
- $this->assertSame( '', $result );
+ /**
+ * @dataProvider provideVHtmlValues
+ */
+ public function testTemplateWithVHtmlVariable(
+ string $template,
+ array $data,
+ string $expected
+ ) {
+ $result = $this->createAndRender( $template, $data );
+ $this->assertSame( $expected, $result );
}
public function testTemplateWithMustacheVariable_VariableIsUndefined_ThrowsException() {