public

Fix for :focus pseudo selector when browser does not have focus

  • Download Gist
jquery.focus.test-fix.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
* JQuery tries to use native CSS selectors instead of the Sizzle selector
* engine for performance reasons.
*
* This causes problems when trying to test intefaces using the
* :focus pseudo selector as unless the web page and browser window
* has the focus, all elements are considered to be without focus.
* Checking for :focus in Selenium or Capybara tests therefore fail if
* using JQuery or Sizzle.
*
* Sizzle will however return true for a :focus element even if the
* window itself has lost focus if we force it not use the native selector functions
* This script forces Sizzle to use its own engine over native selectors.
*
* This file MUST be included before JQuery or Sizzle is loaded
*
* Refer to http://blog.mattheworiordan.com/post/9308775285 for more info
*
**/
 
/* Prevent use of native find selector */
document.querySelectorAll = false;
 
/* Prevent use of native matches selector */
document.documentElement.matchesSelector = false;
document.documentElement.mozMatchesSelector = false;
document.documentElement.webkitMatchesSelector = false;
document.documentElement.msMatchesSelector = false;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.