Ticket #4455 (closed bug: wontfix)
Uncaught exception when selector begins with forward slash
| Reported by: | micah | Owned by: | john |
|---|---|---|---|
| Priority: | major | Milestone: | 1.4 |
| Component: | selector | Version: | 1.3.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
I know, I know, a selector shouldn't begin with a forward slash anyways, but bear with me here.
If I do this:
$('/foo/bar');
I expect an empty instance of jQuery, since XPath is no longer supported. Instead it throws:
Syntax error, unrecognized expression: /foo/bar
This issue arose because I sometimes use $() to check if a var contains a DOM fragment or not, so when passing a URL in I inadvertently discovered that jQuery now pukes on XPath instead of gracefully ignoring it.
Change History
comment:1 Changed 4 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
comment:2 Changed 4 years ago by micah
I agree that an invalid selector is a problem, but I think an exception is a bigger response than the problem merits. IMO an exception should be thrown for something that will screw up your script, not a selector that returns an empty set.
That's a stylistic difference though, not a bug, so I'll adapt my script to work around it.
Perhaps a useful utility would be Sizzle.validate, to check ahead of time whether you've got a selector that Sizzle will reject without having to try...catch your $() call (which isn't guaranteed to only throw this specific exception, otherwise it'd be fine).
comment:3 Changed 4 years ago by micah
- Status changed from closed to reopened
- Resolution invalid deleted
Whoops, forgot to reopen the ticket so it'll actually be read.
I should clarify too: It's fine for Sizzle to throw an exception, but it should be caught by jQuery and turned into an empty set.
comment:4 Changed 4 years ago by dmethvin
So are you proposing that all invalid selector strings be quietly turned into an empty set?
comment:5 Changed 4 years ago by john
- Status changed from reopened to closed
- Resolution set to wontfix
This is 100% intentional. There's a difference between a selector like $("foo") (which might return no matched elements) and $("/blah") (which has a syntax error - and for which we throw an exception).
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

If it gracefully ignored the problem and returned an empty object, you wouldn't know it was a problem.