phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

element.webkitMatchesSelector in phantomjs does not report ":focus" accurately

Open ryangreenberg opened this issue 14 years ago • 64 comments

[email protected] commented:

Which version of PhantomJS are you using? 1.4.1

What steps will reproduce the problem?

  1. Run the provided test script with the test HTML (below).

What is the expected output? What do you see instead? I expect :focus to be true after triggering a focus event on an element. Instead it is false.

Which operating system are you using? OS X 10.7.3

Did you use binary PhantomJS or did you compile it from source? Compiled.

Here is the output of my test script in Chrome, Safari, and PhantomJS:

Chrome

navigator.userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.54 Safari/535.19 $('#some-input').is(':focus'): true $input[0].webkitMatchesSelector(':focus'): true $input[0] == document.activeElement: true

Safari

navigator.userAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16 $('#some-input').is(':focus'): true $input[0].webkitMatchesSelector(':focus'): true $input[0] == document.activeElement: true

PhantomJS 1.4.1

navigator.userAgent: Mozilla/5.0 (Macintosh; PPC Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.4.1 Safari/534.34 $('#some-input').is(':focus'): false $input[0].webkitMatchesSelector(':focus'): false $input[0] == document.activeElement: true

Test HTML <!DOCTYPE html> <html> <head> <title>:focus check</title> <script src="http://code.jquery.com/jquery.js"></script> <script> $(function(){ console.log("navigator.userAgent: " + navigator.userAgent);

// Simple case
$input = $('#some-input');
$input.focus();
console.log(&quot;$('#some-input').is(':focus'): &quot; + $('#some-input').is(':focus'));

// Similar case
$input.focus();
console.log(&quot;$input[0].webkitMatchesSelector(':focus'): &quot; + $input[0].webkitMatchesSelector(':focus'));

// When a native matchesSelector isn't available, jQuery uses:
// 
//  focus: function( elem ) {
//     return elem === elem.ownerDocument.activeElement;
//  }
// 
// The input == document.activeElement, which suggests that the bug is in webkitMatchesSelector
console.log(&quot;$input[0] == document.activeElement: &quot; + ($input[0] == document.activeElement));

}); </script> </head> <body> <form> <input id="some-input"> </form> </body> </html>

Test script: var page = require('webpage').create(); page.onConsoleMessage = function(msg) { console.log(msg); };

page.open('file:///' + phantom.libraryPath + '/focus_test.html', function (status) { if (status !== 'success') { console.log('Could not open URL'); } phantom.exit(); });

Disclaimer: This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #427. :star2:   16 people had starred this issue at the time of migration.

ryangreenberg avatar Mar 14 '12 04:03 ryangreenberg

[email protected] commented:

I can confirm this is an issue in 1.4.1, also in 1.5.0 as well

I'm working around by using "elem === elem.ownerDocument.activeElement" instead, but it's not pretty.

Seems to work fine in Chrome 17 and Safari 5.1.5 - I'm not sure if these are using more recent versions of webkit though

spmason avatar Apr 03 '12 23:04 spmason

[email protected] commented:

Please fix :D

robotarmy avatar Apr 06 '12 04:04 robotarmy

[email protected] commented:

Please fix!

philfreo avatar Mar 09 '13 02:03 philfreo

[email protected] commented:

// Patch $el.is(':focus') until PhantomJS supports it properly. // https://code.google.com/p/phantomjs/issues/detail?id=427 var _jQuery_is = jQuery.fn.is; jQuery.fn.is = function(s) { if (s === ':focus') { return this.get(0) === document.activeElement; } return _jQuery_is.apply(this, arguments); };

This workaround may help some (put this in your test runner code)

philfreo avatar Mar 09 '13 03:03 philfreo

+1

realyze avatar May 03 '13 17:05 realyze

+1

caiges avatar May 10 '13 17:05 caiges

+1

tjvantoll avatar May 10 '13 17:05 tjvantoll

+1

TheCloudlessSky avatar May 13 '13 12:05 TheCloudlessSky

:+1:

camsong avatar May 13 '13 21:05 camsong

Since this bug specifically mentions jQuery, I'll note that jQuery added a workaround for this WebKit bug in jQuery core 1.8.2: http://bugs.jquery.com/ticket/12492

JamesMGreene avatar May 14 '13 05:05 JamesMGreene

Though this has been fixed in jQuery, I'm using Poltergeist (a PhantomJS driver for Capybara, which is a browser testing driver for Ruby) for a lot of integration testing. I'd like to be able to test whether an element has focus and it seems like the natural way to do that would be to use the :focus selector. It would be great if this was fixed.

I'd also guess the cause of this is the same as #11432.

Mr0grog avatar Jul 10 '13 22:07 Mr0grog

+1

ccidral avatar Aug 21 '13 16:08 ccidral

+1

Flamefork avatar Aug 28 '13 20:08 Flamefork

+1

ralfthewise avatar Sep 12 '13 00:09 ralfthewise

+1

roelrz avatar Oct 02 '13 01:10 roelrz

+1 If I want to get currently focused element, I have to mock implementation framework which is not a part of BDD principles. The same downside exists in HTMLUnit also.

vellotis avatar Oct 08 '13 14:10 vellotis

+1

vincentdieltiens avatar Oct 11 '13 12:10 vincentdieltiens

+1

SebiTimeWaster avatar Oct 31 '13 17:10 SebiTimeWaster

Please fix it!

lijunle avatar Jan 03 '14 19:01 lijunle

+1

coledot avatar Jan 13 '14 21:01 coledot