Skip to content

Commit 2d28b47

Browse files
authored
Merge pull request #36 from wmde/v-on-shorthand
Support `@` shorthand for `v-on:` directive
2 parents 4e6e484 + e025c92 commit 2d28b47

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/Component.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ private function stripEventHandlers( DOMNode $node ) {
7474
$attributesToRemove = [];
7575
/** @var DOMAttr $attribute */
7676
foreach ( $node->attributes as $attribute ) {
77-
if ( str_starts_with( $attribute->name, 'v-on:' ) ) {
77+
if (
78+
str_starts_with( $attribute->name, 'v-on:' ) ||
79+
str_starts_with( $attribute->name, '@' )
80+
) {
7881
$attributesToRemove[] = $attribute;
7982
}
8083
}

src/HtmlParser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function parseHtml( string $html ): DOMDocument {
4848
// discard "Tag xyz invalid" messages from libxml2 < 2.14.0(?)
4949
continue;
5050
}
51+
if ( $msg === "error parsing attribute name\n" ) {
52+
// discard these messages (e.g. @event="") from libxml < 2.14.0(?)
53+
continue;
54+
}
5155
$exception = new Exception( $msg, $error->code, $exception );
5256
}
5357
if ( $exception !== null ) {

tests/php/TemplatingTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,36 @@ public function testSingleFileComponent(): void {
3939
$this->assertSame( '<div></div>', $result );
4040
}
4141

42-
public function testTemplateHasOnClickHandler_RemoveHandlerFormOutput() {
42+
public function testTemplateHasOnClickHandler_RemoveHandlerFromOutput() {
4343
$result = $this->createAndRender( '<div v-on:click="doStuff"></div>', [] );
4444

4545
$this->assertSame( '<div></div>', $result );
4646
}
4747

48-
public function testTemplateHasOnClickHandlerAndPreventDefault_RemoveHandlerFormOutput() {
48+
public function testTemplateHasOnClickHandlerAndPreventDefault_RemoveHandlerFromOutput() {
4949
$result = $this->createAndRender( '<div v-on:click.prevent="doStuff"></div>', [] );
5050

5151
$this->assertSame( '<div></div>', $result );
5252
}
5353

54-
public function testTemplateHasOnClickHandlerInSomeChildNode_RemoveHandlerFormOutput() {
54+
public function testTemplateHasOnClickHandlerInSomeChildNode_RemoveHandlerFromOutput() {
5555
$result = $this->createAndRender( '<p><a v-on:click="doStuff"></a></p>', [] );
5656

5757
$this->assertSame( '<p><a></a></p>', $result );
5858
}
5959

60-
public function testTemplateHasOnClickHandlerInGrandChildNode_RemoveHandlerFormOutput() {
60+
public function testTemplateHasOnClickHandlerInGrandChildNode_RemoveHandlerFromOutput() {
6161
$result = $this->createAndRender( '<p><b><a v-on:click="doStuff"></a></b></p>', [] );
6262

6363
$this->assertSame( '<p><b><a></a></b></p>', $result );
6464
}
6565

66+
public function testTemplateHasOnClickHandlerWithShorthand_RemoveHandlerFromOutput(): void {
67+
$result = $this->createAndRender( '<p @click="x"></p>', [] );
68+
69+
$this->assertSame( '<p></p>', $result );
70+
}
71+
6672
public function testTemplateHasMultipleEventHandlers_RemoveAll(): void {
6773
$result = $this->createAndRender( '<p v-on:click="x" v-on:keypress="y"></p>', [] );
6874

0 commit comments

Comments
 (0)