Skip to content

Commit

Permalink
Enable keyfilter only for keydown, keyup, and keypress. (#613)
Browse files Browse the repository at this point in the history
* Enable keyfilter only for keydown, keyup, and keypress.

* Fixed a bug in restoring event names

* add test

* npm run format
  • Loading branch information
NakajimaTakuya committed Nov 29, 2022
1 parent 9381867 commit a6dc498
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/action_descriptor.ts
Expand Up @@ -44,13 +44,21 @@ const descriptorPattern = /^(?:(.+?)(?:\.(.+?))?(?:@(window|document))?->)?(.+?)
export function parseActionDescriptorString(descriptorString: string): Partial<ActionDescriptor> {
const source = descriptorString.trim()
const matches = source.match(descriptorPattern) || []
let eventName = matches[1]
let keyFilter = matches[2]

if (keyFilter && !["keydown", "keyup", "keypress"].includes(eventName)) {
eventName += `.${keyFilter}`
keyFilter = ""
}

return {
eventTarget: parseEventTarget(matches[3]),
eventName: matches[1],
eventName,
eventOptions: matches[6] ? parseEventOptions(matches[6]) : {},
identifier: matches[4],
methodName: matches[5],
keyFilter: matches[2],
keyFilter,
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/tests/modules/core/action_keyboard_filter_tests.ts
Expand Up @@ -21,6 +21,7 @@ export default class ActionKeyboardFilterTests extends LogControllerTestCase {
<button id="button7"></button>
<button id="button8" data-action="keydown.a->a#log keydown.b->a#log2"></button>
<button id="button9" data-action="keydown.shift+a->a#log keydown.a->a#log2 keydown.ctrl+shift+a->a#log3">
<button id="button10" data-action="jquery.custom.event->a#log jquery.a->a#log2">
</div>
`

Expand Down Expand Up @@ -182,4 +183,18 @@ export default class ActionKeyboardFilterTests extends LogControllerTestCase {
await this.triggerKeyboardEvent(button, "keydown", { key: "A", ctrlKey: true, shiftKey: true })
this.assertActions({ name: "log3", identifier: "a", eventType: "keydown", currentTarget: button })
}

async "test ignore filter syntax when not a keyboard event"() {
const button = this.findElement("#button10")
await this.nextFrame
await this.triggerEvent(button, "jquery.custom.event")
this.assertActions({ name: "log", identifier: "a", eventType: "jquery.custom.event", currentTarget: button })
}

async "test ignore filter syntax when not a keyboard event (case2)"() {
const button = this.findElement("#button10")
await this.nextFrame
await this.triggerEvent(button, "jquery.a")
this.assertActions({ name: "log2", identifier: "a", eventType: "jquery.a", currentTarget: button })
}
}

0 comments on commit a6dc498

Please sign in to comment.