Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <body> tag prevents the addition of an implied <p> tag when the
// $source starts with plain text
$tmpDoc = $htmlParser->parseHtml( '<body>' . $source . '</body>' );
foreach ( $htmlParser->getBodyElement( $tmpDoc )->childNodes as $node ) {
$node = $parent->ownerDocument->importNode( $node, true );
$parent->appendChild( $node );
Expand Down
89 changes: 48 additions & 41 deletions tests/php/TemplatingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,56 @@ public function testTemplateWithVariableAndValueInKorean_ReplacesVariableWithEnc
$this->assertSame( '<p>한국어</p>', $result );
}

public function testTemplateWithVhtmlVariable_ReplacesVariableWithGivenValue() {
$result = $this->createAndRender(
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>some value</p>' ]
);

$this->assertSame( '<div><div><p>some value</p></div></div>', $result );
}

public function testTemplateWithVhtmlVariableNestedData_ReplacesVariableWithGivenValue() {
$result = $this->createAndRender(
'<div><div v-html="value.html"></div></div>',
[ 'value' => [ 'html' => '<p>some value</p>' ] ]
);

$this->assertSame( '<div><div><p>some value</p></div></div>', $result );
}

public function testTemplateWithVhtmlVariableAndAttributeBinding_ReplacesBoth(): void {
$result = $this->createAndRender(
'<div><div :data-a="a" v-html="html"></div></div>',
[ 'a' => 'A', 'html' => '<p>HTML</p>' ]
);

$this->assertSame( '<div><div data-a="A"><p>HTML</p></div></div>', $result );
}

public function testTemplateWithVhtmlAndDiacritcsInValue_ReplacesVariableWithEncodedValue() {
$result = $this->createAndRender(
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>inglés</p>' ]
);

$this->assertSame( '<div><div><p>inglés</p></div></div>', $result );
public static function provideVHtmlValues(): iterable {
return [
'plain text does not get wrapped' => [
'<div v-html="value"></div>',
[ 'value' => 'plain text' ],
'<div>plain text</div>',
],
'starting with plain text, containing html tag' => [
'<div v-html="value"></div>',
[ 'value' => 'beginning text <div>internal div</div> ending text' ],
'<div>beginning text <div>internal div</div> ending text</div>',
],
'variable in child node is replaced' => [
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>some value</p>' ],
'<div><div><p>some value</p></div></div>',
],
'variable with nested value' => [
'<div><div v-html="value.html"></div></div>',
[ 'value' => [ 'html' => '<p>some value</p>' ] ],
'<div><div><p>some value</p></div></div>'
],
'template with v-html and attribute binding' => [
'<div><div :data-a="a" v-html="html"></div></div>',
[ 'a' => 'A', 'html' => '<p>HTML</p>' ],
'<div><div data-a="A"><p>HTML</p></div></div>'
],
'with diacritics in the value' => [
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>inglés</p>' ],
'<div><div><p>inglés</p></div></div>'
],
'with value in Korean' => [
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>한국어</p>' ],
'<div><div><p>한국어</p></div></div>'
]
];
}

public function testTemplateWithVhtmlAndValueInKorean_ReplacesVariableWithEncodedValue() {
$result = $this->createAndRender(
'<div><div v-html="value"></div></div>',
[ 'value' => '<p>한국어</p>' ]
);

$this->assertSame( '<div><div><p>한국어</p></div></div>', $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() {
Expand Down