From 355b7cd272f5b347038216e574796a9eea78ca26 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Tue, 9 Aug 2022 16:12:55 +0200 Subject: [PATCH] Make Action Parameters attributes case-insensitive Even though this is not the "recommended way" to specify the data attributes for the params it's to match the existing behaviour of Actions, Controllers, Targets and Values. Resolves #561 --- src/core/action.ts | 2 +- .../action_params_case_insensitive_tests.ts | 62 +++++++++++++++++++ src/tests/modules/core/action_params_tests.ts | 8 +-- 3 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 src/tests/modules/core/action_params_case_insensitive_tests.ts diff --git a/src/core/action.ts b/src/core/action.ts index 63b59dde..00832f72 100644 --- a/src/core/action.ts +++ b/src/core/action.ts @@ -31,7 +31,7 @@ export class Action { get params() { const params:{ [key: string]: any } = {} - const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`) + const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`, "i") for (const { name, value } of Array.from(this.element.attributes)) { const match = name.match(pattern) diff --git a/src/tests/modules/core/action_params_case_insensitive_tests.ts b/src/tests/modules/core/action_params_case_insensitive_tests.ts new file mode 100644 index 00000000..764c4c39 --- /dev/null +++ b/src/tests/modules/core/action_params_case_insensitive_tests.ts @@ -0,0 +1,62 @@ +import ActionParamsTests from "./action_params_tests" + +export default class ActionParamsCaseInsensitiveTests extends ActionParamsTests { + identifier = ["CamelCase", "AnotherOne"] + fixtureHTML = ` +
+ +
+
+ ` + expectedParamsForCamelCase = { + id: 123, + multiWordExample: "/path", + payload: { + value: 1 + }, + active: true, + empty: "", + inactive: false + } + + async "test clicking on the element does return its params"() { + this.actionValue = "click->CamelCase#log" + await this.nextFrame + await this.triggerEvent(this.buttonElement, "click") + + this.assertActions( + { identifier: "CamelCase", params: this.expectedParamsForCamelCase }, + ) + } + + async "test global event return element params where the action is defined"() { + this.actionValue = "keydown@window->CamelCase#log" + await this.nextFrame + await this.triggerEvent("#outside", "keydown") + + this.assertActions( + { identifier: "CamelCase", params: this.expectedParamsForCamelCase }, + ) + } + + async "test passing params to namespaced controller"() { + this.actionValue = "click->CamelCase#log click->AnotherOne#log2" + await this.nextFrame + await this.triggerEvent(this.buttonElement, "click") + + this.assertActions( + { identifier: "CamelCase", params: this.expectedParamsForCamelCase }, + { identifier: "AnotherOne", params: { id: 234 } }, + ) + } +} diff --git a/src/tests/modules/core/action_params_tests.ts b/src/tests/modules/core/action_params_tests.ts index 982c03cc..eadb0a28 100644 --- a/src/tests/modules/core/action_params_tests.ts +++ b/src/tests/modules/core/action_params_tests.ts @@ -69,10 +69,10 @@ export default class ActionParamsTests extends LogControllerTestCase { { identifier: "c", params: this.expectedParamsForC }, ) - await this.buttonElement.setAttribute("data-c-id-param", "234") - await this.buttonElement.setAttribute("data-c-new-param", "new") - await this.buttonElement.removeAttribute("data-c-payload-param") - await this.triggerEvent(this.buttonElement, "click") + this.buttonElement.setAttribute("data-c-id-param", "234") + this.buttonElement.setAttribute("data-c-new-param", "new") + this.buttonElement.removeAttribute("data-c-payload-param") + this.triggerEvent(this.buttonElement, "click") this.assertActions( { identifier: "c", params: this.expectedParamsForC },