Skip to main content

Bug Tracker

Side navigation

#4455 closed bug (wontfix)

Opened March 30, 2009 10:42PM UTC

Closed April 01, 2009 04:24PM UTC

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:
Blocked by: Blocking:
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.

Attachments (0)
Change History (5)

Changed March 30, 2009 10:46PM UTC by dmethvin comment:1

resolution: → invalid
status: newclosed

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

Changed March 30, 2009 11:02PM UTC by micah comment:2

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).

Changed March 30, 2009 11:21PM UTC by micah comment:3

resolution: invalid
status: closedreopened

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.

Changed April 01, 2009 03:20AM UTC by dmethvin comment:4

So are you proposing that all invalid selector strings be quietly turned into an empty set?

Changed April 01, 2009 04:24PM UTC by john comment:5

resolution: → wontfix
status: reopenedclosed

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).