Bug Tracker

Opened 11 years ago

Closed 11 years ago

#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:
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.

Change History (5)

comment:1 Changed 11 years ago by dmethvin

Resolution: invalid
Status: newclosed

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

comment:2 Changed 11 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 11 years ago by micah

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.

comment:4 Changed 11 years ago by dmethvin

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

comment:5 Changed 11 years ago by john

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

Note: See TracTickets for help on using tickets.