diff --git a/src/Component.php b/src/Component.php index 7e519b5..1a74856 100644 --- a/src/Component.php +++ b/src/Component.php @@ -74,12 +74,17 @@ private function stripEventHandlers( DOMNode $node ) { if ( $this->isTextNode( $node ) ) { return; } + // Removing items while iterating breaks iteration, so defer attribute removal + $attributesToRemove = []; /** @var DOMAttr $attribute */ foreach ( $node->attributes as $attribute ) { if ( str_starts_with( $attribute->name, 'v-on:' ) ) { - $node->removeAttribute( $attribute->name ); + $attributesToRemove[] = $attribute; } } + foreach ( $attributesToRemove as $attribute ) { + $node->removeAttributeNode( $attribute ); + } } /** @@ -110,7 +115,7 @@ private function replaceMustacheVariables( DOMNode $node, array $data ) { private function handleAttributeBinding( DOMElement $node, array $data ) { /** @var DOMAttr $attribute */ foreach ( iterator_to_array( $node->attributes ) as $attribute ) { - if ( !preg_match( '/^:[\w-]+$/', $attribute->name ) ) { + if ( !str_starts_with( $attribute->name, ':' ) ) { continue; } diff --git a/src/JsParsing/BasicJsExpressionParser.php b/src/JsParsing/BasicJsExpressionParser.php index 752f83c..0068d53 100644 --- a/src/JsParsing/BasicJsExpressionParser.php +++ b/src/JsParsing/BasicJsExpressionParser.php @@ -19,9 +19,9 @@ public function __construct( array $methods ) { */ public function parse( $expression ) { $expression = $this->normalizeExpression( $expression ); - if ( strncmp( $expression, '!', 1 ) === 0 ) { + if ( str_starts_with( $expression, '!' ) ) { return new NegationOperator( $this->parse( substr( $expression, 1 ) ) ); - } elseif ( strncmp( $expression, "'", 1 ) === 0 ) { + } elseif ( str_starts_with( $expression, "'" ) && str_ends_with( $expression, "'" ) ) { return new StringLiteral( substr( $expression, 1, -1 ) ); } elseif ( preg_match( '/^(\w+)\((.*)\)$/', $expression, $matches ) ) { $methodName = $matches[1]; diff --git a/tests/php/TemplatingTest.php b/tests/php/TemplatingTest.php index 8afa74e..e8f1108 100644 --- a/tests/php/TemplatingTest.php +++ b/tests/php/TemplatingTest.php @@ -63,6 +63,12 @@ public function testTemplateHasOnClickHandlerInGrandChildNode_RemoveHandlerFormO $this->assertSame( '

', $result ); } + public function testTemplateHasMultipleEventHandlers_RemoveAll(): void { + $result = $this->createAndRender( '

', [] ); + + $this->assertSame( '

', $result ); + } + public function testTemplateWithSingleMustacheVariable_ReplacesVariableWithGivenValue() { $result = $this->createAndRender( '

{{value}}

', [ 'value' => 'some value' ] );