Skip to main content

Bug Tracker

Side navigation

#3743 closed bug (duplicate)

Opened December 23, 2008 08:25PM UTC

Closed November 17, 2010 05:38AM UTC

Last modified March 14, 2012 10:46PM UTC

Unable to select elements with ID or class containing backslash (even when escaped)

Reported by: patridge Owned by: john
Priority: minor Milestone: 1.3
Component: selector Version: 1.2.6
Keywords: Cc: patridge
Blocked by: Blocking:
Description

When trying to select an id/class that contains a backslash "\\", escaping doesn't seem to work. While "\\" and the characters that are escapable (".", "$", "[", "]", "/", etc.) aren't really valid in these attributes according to HTML 4.0, this is the only character that appears to be impossible to find using a selector. Any number of repeated "\\" characters appear to resolve to nothing when used as a selector. If this character is intended to be reserved in an id/class, mentioning so in the selector documentation and FAQ would resolve the issue.

Here is the markup in question:

<div id="test\\123"></div>

or

<div class="test\\123"></div>

Here are some selectors that fail to find either of these divs (but all of them would find <div id="test123"></div> or <div class="test123"></div>, if it were present in the markup):

$("#test123") // => 0 items (0 expected)
$("#test\\\\123") // js-escaped as "#test\\123", selector-escaped as "#test123" => 0 items (0 expected)
$("#test\\\\\\123") // js-escaped as "#test\\123", selector-escaped as "#test123" => 0 items (0 expected)
$("#test\\\\\\\\123") // ideal documentation case: js-escaped as "#test\\\\123", should be selector-escaped as "#test\\123" => 0 items (1 expected: fail)
$(".test\\\\\\\\123") // => 0 items (1 expected: fail) [others for class left out intentionally]

Any number of "\\" in the selector appear to simply resolve to doing a select without any "\\" characters (unless there is a different character to actually escape at the end, such as a "."):

$("#test\\\\\\\\\\\\\\\\\\\\\\\\123") // js-escaped as "#test\\\\\\\\\\\\123", should be selector-escaped as "#test\\\\\\123" => won't find <div id="test\\\\\\123"></div> but will find <div id="test123"></div> just like the other test cases
Attachments (0)
Change History (7)

Changed December 25, 2008 08:52PM UTC by flesler comment:1

cc: → patridge
owner: → john

Changed January 04, 2009 09:02PM UTC by balazs.endre comment:2

It's working if you use an attribute selector: [id=test\\\\123] so I guess it's because the quickExpr that should match a single id: ^#([\\w-]+)$.

Maybe the same stuff should be used instead of [\\w-], just like in other regular expressions:

(?:[\\w\\u0128-\\uFFFF\\*_-]|\\\\.)

Changed January 11, 2009 02:53PM UTC by balazs.endresz comment:3

[id=test\\\\123] won't work any more either because of #3803.

Changed February 07, 2009 02:47PM UTC by dmethvin comment:4

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

http://www.w3.org/TR/html401/types.html#type-name

Changed July 10, 2009 07:03PM UTC by patridge comment:5

The W3 specs (see dmethvin's comment) rule out the need to explicitly support backslashes in IDs but it doesn't help with class attributes, although there is probably another standards doc out there that forbids them for class names (I only did a cursory search on those same w3.org pages).

Changed November 17, 2010 05:38AM UTC by snover comment:6

resolution: → duplicate
status: newclosed

The use of qSA in most cases makes this bug irrelevant except for IE6/7, where it is documented at #6428.

Changed November 17, 2010 05:38AM UTC by snover comment:7

Duplicate of #6428.