Skip to content

Instantly share code, notes, and snippets.

@tommck
Created July 11, 2013 13:15
Show Gist options
  • Save tommck/5975355 to your computer and use it in GitHub Desktop.
Save tommck/5975355 to your computer and use it in GitHub Desktop.
jQuery exception logic flow examples... the "assert" function is being called in a couple places. Most of them are to detect Opera and/or other odd browsers. I highlighted the error lines with "/***************** ERROR ON NEXT LINE ****************/" It seems to me that they should already know if they're running a browser that requires this fea…
// jquery 2.0.2 line 1192+
function assert( fn ) {
var div = document.createElement("div");
try {
return !!fn( div );
} catch (e) {
return false;
} finally {
// Remove from its parent by default
if ( div.parentNode ) {
div.parentNode.removeChild( div );
}
// release memory in IE
div = null;
}
}
// jQuery 2.0.2 line 1549+
assert(function( div ) {
// Support: Opera 10-12/IE8
// ^= $= *= and empty values
// Should not select anything
// Support: Windows 8 Native Apps
// The type attribute is restricted during .innerHTML assignment
var input = doc.createElement("input");
input.setAttribute( "type", "hidden" );
div.appendChild( input ).setAttribute( "t", "" );
if ( div.querySelectorAll("[t^='']").length ) {
rbuggyQSA.push( "[*^$]=" + whitespace + "*(?:''|\"\")" );
}
// FF 3.5 - :enabled/:disabled and hidden elements (hidden elements are still enabled)
// IE8 throws error here and will not see later tests
if ( !div.querySelectorAll(":enabled").length ) {
rbuggyQSA.push( ":enabled", ":disabled" );
}
// Opera 10-11 does not throw on post-comma invalid pseudos
/***************** ERROR ON NEXT LINE ****************/
// SCRIPT5022: Exception was thrown at line 1571, column 4 in ms-appx://c33865be-41fa-45ec-b8f3-4d25e60ad0f3/Scripts/jquery-2.0.2.js 0x800a139e - JavaScript runtime error: SyntaxError
div.querySelectorAll("*,:x");
rbuggyQSA.push(",.*:");
});
// jquery 2.0.2 line 1581+
assert(function( div ) {
// Check to see if it's possible to do matchesSelector
// on a disconnected node (IE 9)
support.disconnectedMatch = matches.call( div, "div" );
// This should fail with an exception
// Gecko does not error, returns false instead
/***************** ERROR ON NEXT LINE ****************/
//SCRIPT5022: Exception was thrown at line 1588, column 4 in ms-appx://c33865be-41fa-45ec-b8f3-4d25e60ad0f3/Scripts/jquery-2.0.2.js 0x800a139e - JavaScript runtime error: SyntaxError
matches.call( div, "[s!='']:x" );
rbuggyMatches.push( "!=", pseudos );
});
@cromwellryan
Copy link

I don't see anything wrong here (in this case). Assert should fail with false if the block false or an exception is thrown. They also need to assure a debug statement like assert doesn't have side-affects like adding things to the DOM.

@tommck
Copy link
Author

tommck commented Jul 11, 2013

Well, the problem is that using a "break on throw" option in a debugger is REALLY useful.. jQuery is the only thing that does this when the script file is included. It makes us disable the "break on throw" and, thus makes debugging much harder.

@iegik
Copy link

iegik commented Feb 22, 2017

Don't break on caught exceptions. We caught it because we expected it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment