Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#11458 closed enhancement (wontfix)

Event namespace should allow special chars like "+", "$", "/"

Reported by: millermedeiros Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:

Description

It could be fixed by escaping the namespace string before doing the split().sort().join()

// chars that should be escaped in RegExp constructor
var rEscapeRegExpChars = /[\\.+*?\^$\[\](){}\/'#]/g;

function escapeRegExp(str) {
    return str.replace(rEscapeRegExpChars,'\\$&');
}

// than later on the place where namespaces RegExp is created
namespaces = namespaces ? new RegExp("(^|\\.)" + escapeRegExp(namespaces).split("\\.").sort().join("\\.(?:.*\\.)?") + "(\\.|$)") : null;
//

I was trying to create a namespace to handle keys combinations like "keypress.ctrl+s" but the "+" breaks unbind() and off() since namespaces.test( handleObj.namespace ) will return false.

My solution for now was simply to avoid using chars that have special meaning inside RegExp (using "keypress.ctrl_s" instead).

Couldn't find any information about it in the documentation and it took a while to realize the the event wasn't being removed and that the problem was caused by the plus sign.

Change History (2)

comment:1 Changed 11 years ago by dmethvin

Resolution: wontfix
Status: newclosed

I don't think it's necessary to accept any special characters, and doing so may limit our ability to expand syntax later. Use an underscore instead.

comment:2 Changed 11 years ago by millermedeiros

ok, totally understand and main reason why I didn't even asked for a pull request.

already implemented it here: https://github.com/tzuryby/jquery.hotkeys/pull/20

good thing that now it's documented somewhere (here in the bug tracker) and explains why it works this way.

Note: See TracTickets for help on using tickets.