From 8ac69b70047e583571ab5c12e4ea16f0f89d8ba9 Mon Sep 17 00:00:00 2001 From: Mikael Grankvist Date: Wed, 25 Nov 2020 15:01:04 +0200 Subject: [PATCH] fix: Correct regexp to only match when digit first use find instead of matches as matches never matched. Fixes #9494 --- .../com/vaadin/flow/internal/CustomElementNameValidator.java | 4 ++-- .../vaadin/flow/internal/CustomElementNameValidatorTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/CustomElementNameValidator.java b/flow-server/src/main/java/com/vaadin/flow/internal/CustomElementNameValidator.java index de3ba042ded..93423077eb0 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/CustomElementNameValidator.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/CustomElementNameValidator.java @@ -29,7 +29,7 @@ * @since 1.0 */ public final class CustomElementNameValidator { - private static final Pattern STARTS_WITH_A_DIGIT = Pattern.compile("\\d.*"); + private static final Pattern STARTS_WITH_A_DIGIT = Pattern.compile("^\\d.*"); private static final Set RESERVED_NAMES = Stream .of("annotation-xml", "color-profile", "font-face", "font-face-src", @@ -58,7 +58,7 @@ public static boolean isCustomElementName(String name) { private static boolean checkHtmlTagRules(String name) { return !name.isEmpty() && name.equals(name.toLowerCase(Locale.ENGLISH)) && !name.startsWith("-") - && !STARTS_WITH_A_DIGIT.matcher(name).matches(); + && !STARTS_WITH_A_DIGIT.matcher(name).find(); } private static boolean checkWebComponentRules(String name) { diff --git a/flow-server/src/test/java/com/vaadin/flow/internal/CustomElementNameValidatorTest.java b/flow-server/src/test/java/com/vaadin/flow/internal/CustomElementNameValidatorTest.java index f1df77414c0..2fd5b88f19e 100644 --- a/flow-server/src/test/java/com/vaadin/flow/internal/CustomElementNameValidatorTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/internal/CustomElementNameValidatorTest.java @@ -30,7 +30,7 @@ public class CustomElementNameValidatorTest { @Test public void testInvalidElementNames() { Stream.of("", "foo", "annotation-xml", "0-foo", "-foo", "foo-$", - "foo-/", "FOO-BAR", "foo/", "øl-unicorn", "foo-💩") + "foo-/", "FOO-BAR", "foo/", "øl-unicorn", "foo-💩", "5th-element") .forEach(name -> Assert.assertFalse(String.format( "Name %s is valid even though it should not be", name), CustomElementNameValidator.isCustomElementName(name))); @@ -38,7 +38,7 @@ public void testInvalidElementNames() { @Test public void testValidNamesWithoutErrorOrWarning() { - Stream.of("foo-bar", "custom-element", "date-field", "dos-box") + Stream.of("foo-bar", "custom-element", "date-field", "dos-box", "home-4-good") .forEach(name -> Assert.assertTrue(String.format( "Name %s is not valid even though it should be", name), CustomElementNameValidator.isCustomElementName(name)));