Skip to content

Commit 1255b0f

Browse files
authored
Merge pull request #34 from wmde/cleanup
Two minor cleanups
2 parents 8847d9b + a7343b8 commit 1255b0f

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/Component.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ private function stripEventHandlers( DOMNode $node ) {
7474
if ( $this->isTextNode( $node ) ) {
7575
return;
7676
}
77+
// Removing items while iterating breaks iteration, so defer attribute removal
78+
$attributesToRemove = [];
7779
/** @var DOMAttr $attribute */
7880
foreach ( $node->attributes as $attribute ) {
7981
if ( str_starts_with( $attribute->name, 'v-on:' ) ) {
80-
$node->removeAttribute( $attribute->name );
82+
$attributesToRemove[] = $attribute;
8183
}
8284
}
85+
foreach ( $attributesToRemove as $attribute ) {
86+
$node->removeAttributeNode( $attribute );
87+
}
8388
}
8489

8590
/**
@@ -110,7 +115,7 @@ private function replaceMustacheVariables( DOMNode $node, array $data ) {
110115
private function handleAttributeBinding( DOMElement $node, array $data ) {
111116
/** @var DOMAttr $attribute */
112117
foreach ( iterator_to_array( $node->attributes ) as $attribute ) {
113-
if ( !preg_match( '/^:[\w-]+$/', $attribute->name ) ) {
118+
if ( !str_starts_with( $attribute->name, ':' ) ) {
114119
continue;
115120
}
116121

src/JsParsing/BasicJsExpressionParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public function __construct( array $methods ) {
1919
*/
2020
public function parse( $expression ) {
2121
$expression = $this->normalizeExpression( $expression );
22-
if ( strncmp( $expression, '!', 1 ) === 0 ) {
22+
if ( str_starts_with( $expression, '!' ) ) {
2323
return new NegationOperator( $this->parse( substr( $expression, 1 ) ) );
24-
} elseif ( strncmp( $expression, "'", 1 ) === 0 ) {
24+
} elseif ( str_starts_with( $expression, "'" ) && str_ends_with( $expression, "'" ) ) {
2525
return new StringLiteral( substr( $expression, 1, -1 ) );
2626
} elseif ( preg_match( '/^(\w+)\((.*)\)$/', $expression, $matches ) ) {
2727
$methodName = $matches[1];

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_RemoveHandlerFormO
6363
$this->assertSame( '<p><b><a></a></b></p>', $result );
6464
}
6565

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

0 commit comments

Comments
 (0)