From 1815929422e43c0d21c6332efe9397dd07ef50d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20H=C3=BCsges?= Date: Mon, 20 Jul 2020 09:07:51 +0200 Subject: [PATCH] Fix for encoded attributes --- src/Compiler.php | 25 ++++++++++++++++--- src/Models/Replacements.php | 5 +++- .../bindings-with-encoded-attributes.twig | 12 +++++++++ .../bindings-with-encoded-attributes.vue | 14 +++++++++++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/vue-bind/bindings-with-encoded-attributes.twig create mode 100644 tests/fixtures/vue-bind/bindings-with-encoded-attributes.vue diff --git a/src/Compiler.php b/src/Compiler.php index f71e3e5..3b4fc69 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -406,10 +406,27 @@ private function handleAttributeBinding(DOMElement $node) Replacements::getSanitizedConstant('DOUBLE_CURLY_CLOSE'); } - $node->setAttribute( - $name === 'src' ? Replacements::getSanitizedConstant('SRC_ATTRIBUTE_NAME') : $name, - $this->implodeAttributeValue($name, $dynamicValues, $staticValues) - ); + /** @see https://gitlab.gnome.org/GNOME/libxml2/-/blob/LIBXML2.6.32/HTMLtree.c#L657 */ + switch ($name) { + case 'href': + $name = Replacements::getSanitizedConstant('ATTRIBUTE_NAME_HREF'); + break; + case 'action': + $name = Replacements::getSanitizedConstant('ATTRIBUTE_NAME_ACTION'); + break; + case 'src': + $name = Replacements::getSanitizedConstant('ATTRIBUTE_NAME_SRC'); + break; + case 'name': + if ($node->tagName === 'a') { + $name = Replacements::getSanitizedConstant('ATTRIBUTE_NAME_A_NAME'); + } + break; + default: + break; + } + + $node->setAttribute($name, $this->implodeAttributeValue($name, $dynamicValues, $staticValues)); } } diff --git a/src/Models/Replacements.php b/src/Models/Replacements.php index 01f8a7b..dc4afbe 100644 --- a/src/Models/Replacements.php +++ b/src/Models/Replacements.php @@ -12,7 +12,10 @@ abstract class Replacements extends BasicEnum public const SMALLER = '<'; public const AMPERSAND = '&'; public const PIPE = '|'; - public const SRC_ATTRIBUTE_NAME = 'src'; + public const ATTRIBUTE_NAME_HREF = 'href'; + public const ATTRIBUTE_NAME_ACTION = 'action'; + public const ATTRIBUTE_NAME_SRC = 'src'; + public const ATTRIBUTE_NAME_A_NAME = 'name'; /** * Removes all instances of replacements from target diff --git a/tests/fixtures/vue-bind/bindings-with-encoded-attributes.twig b/tests/fixtures/vue-bind/bindings-with-encoded-attributes.twig new file mode 100644 index 0000000..9c675f7 --- /dev/null +++ b/tests/fixtures/vue-bind/bindings-with-encoded-attributes.twig @@ -0,0 +1,12 @@ +
+ + {{isBool ? varA.alt : varB.alt}} + +
+ + +
+ +
diff --git a/tests/fixtures/vue-bind/bindings-with-encoded-attributes.vue b/tests/fixtures/vue-bind/bindings-with-encoded-attributes.vue new file mode 100644 index 0000000..3d58419 --- /dev/null +++ b/tests/fixtures/vue-bind/bindings-with-encoded-attributes.vue @@ -0,0 +1,14 @@ +