Skip to content

Commit a6dc498

Browse files
Enable keyfilter only for keydown, keyup, and keypress. (#613)
* Enable keyfilter only for keydown, keyup, and keypress. * Fixed a bug in restoring event names * add test * npm run format
1 parent 9381867 commit a6dc498

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/core/action_descriptor.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,21 @@ const descriptorPattern = /^(?:(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)
4444
export function parseActionDescriptorString(descriptorString: string): Partial<ActionDescriptor> {
4545
const source = descriptorString.trim()
4646
const matches = source.match(descriptorPattern) || []
47+
let eventName = matches[1]
48+
let keyFilter = matches[2]
49+
50+
if (keyFilter && !["keydown", "keyup", "keypress"].includes(eventName)) {
51+
eventName += `.${keyFilter}`
52+
keyFilter = ""
53+
}
54+
4755
return {
4856
eventTarget: parseEventTarget(matches[3]),
49-
eventName: matches[1],
57+
eventName,
5058
eventOptions: matches[6] ? parseEventOptions(matches[6]) : {},
5159
identifier: matches[4],
5260
methodName: matches[5],
53-
keyFilter: matches[2],
61+
keyFilter,
5462
}
5563
}
5664

src/tests/modules/core/action_keyboard_filter_tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default class ActionKeyboardFilterTests extends LogControllerTestCase {
2121
<button id="button7"></button>
2222
<button id="button8" data-action="keydown.a->a#log keydown.b->a#log2"></button>
2323
<button id="button9" data-action="keydown.shift+a->a#log keydown.a->a#log2 keydown.ctrl+shift+a->a#log3">
24+
<button id="button10" data-action="jquery.custom.event->a#log jquery.a->a#log2">
2425
</div>
2526
`
2627

@@ -182,4 +183,18 @@ export default class ActionKeyboardFilterTests extends LogControllerTestCase {
182183
await this.triggerKeyboardEvent(button, "keydown", { key: "A", ctrlKey: true, shiftKey: true })
183184
this.assertActions({ name: "log3", identifier: "a", eventType: "keydown", currentTarget: button })
184185
}
186+
187+
async "test ignore filter syntax when not a keyboard event"() {
188+
const button = this.findElement("#button10")
189+
await this.nextFrame
190+
await this.triggerEvent(button, "jquery.custom.event")
191+
this.assertActions({ name: "log", identifier: "a", eventType: "jquery.custom.event", currentTarget: button })
192+
}
193+
194+
async "test ignore filter syntax when not a keyboard event (case2)"() {
195+
const button = this.findElement("#button10")
196+
await this.nextFrame
197+
await this.triggerEvent(button, "jquery.a")
198+
this.assertActions({ name: "log2", identifier: "a", eventType: "jquery.a", currentTarget: button })
199+
}
185200
}

0 commit comments

Comments
 (0)