Skip to content

Commit e025c92

Browse files
Support @ shorthand for v-on: directive
This is used in some WikibaseLexeme templates (albeit never in the server-rendered path as far as I can tell), so we should support it. There are two related problems: old versions of libxml2 complain about @-prefixed attribute names, and our code only checked for v-on:. Both are fairly simple to resolve. The libxml2 error doesn’t happen on my system, so I assume it was fixed in some recent-ish version; libxml2 2.14.0 [1], which boasts a tokenizer “conform[ing] fully to HTML5” and “several non-standard syntax warnings […] removed”, seems like the most likely candidate, but I haven’t tested the behavior with different versions. But in any event, libxml2 is clearly able to parse the markup and produce a DOM that we can work with (as evidenced by the fact that the WikibaseLexeme template have worked for years before we started raising the libxml2 errors as exceptions in ba3f8e0 / #32), so we can just ignore the errors. [1]: https://download.gnome.org/sources/libxml2/2.14/libxml2-2.14.0.news
1 parent 1ab28dc commit e025c92

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public function testTemplateHasOnClickHandlerInGrandChildNode_RemoveHandlerFromO
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)