Since v2.2.0: Cannot read property 'opener' of null when $.parseHTML and then get css style #2866

Closed
todorpr opened this Issue Jan 25, 2016 · 5 comments

Projects

None yet

4 participants

@todorpr
Contributor
todorpr commented Jan 25, 2016

I just upgraded to jQuery v2.2.0 and this problem appeared.

I could reproduce it in the following case:

  • parse some html using $.parseHTML
var el = $.parseHTML('<span style="font-family: Cuprum,sans-serif; font-size: 14px; color: #999999;">some text</span>');
  • on the already parsed html wrap it in jQuery container:
var span = $(el[0]);
  • now try to get css style on the jQuery element:
var style = span.css("font-size");
console.log(style);

Which results in:

Uncaught TypeError: Cannot read property 'opener' of null

jsBin: http://jsbin.com/ruyecuxogu/edit?html,js,console,output

@todorpr
Contributor
todorpr commented Jan 25, 2016

I found out that the problem comes from this function
https://github.com/jquery/jquery/blob/99e8ff1baa7ae341e94bb89c3e84570c7c3ad9ea/src/css/var/getStyles.js

if change:

if (!view.opener ) {
    view = window;
}

to:

if (!view || !view.opener ) {
    view = window;
}

the error disappears and getting css style is done properly.

@timmywil timmywil added this to the 1.12.1/2.2.1 milestone Jan 25, 2016
@timmywil timmywil added the CSS label Jan 25, 2016
@markelog
Member

Hm, it seems issue is relevant for the beta too

@markelog markelog self-assigned this Jan 25, 2016
@todorpr todorpr added a commit to todorpr/jquery that referenced this issue Jan 25, 2016
@todorpr todorpr Make sure elem.ownerDocument.defaultView is not null
Since v2.2.0: Cannot read property 'opener' of null when $.parseHTML and then get css style #2866
jquery#2866
318aa74
@gibson042
Member

This is caused by cfe468f, right?

document.implementation.createHTMLDocument( "" ).defaultView
// → null
@timmywil
Member

@gibson042 Must be, but are you implying that createHTMLDocument isn't worth it? Current solution is to default to the current window, which might even be alright when building append-able elements for iframes.

@gibson042
Member

I'm not implying that, just trying to establish the blast radius. The fix proposed in #2867 seems fine.

@timmywil timmywil pushed a commit that closed this issue Jan 27, 2016
@todorpr todorpr + Timmy Willison CSS: Make sure elem.ownerDocument.defaultView is not null
Fixes gh-2866
Close gh-2867
35c3148
@timmywil timmywil closed this in 35c3148 Jan 27, 2016
@timmywil timmywil pushed a commit that referenced this issue Jan 27, 2016
@todorpr todorpr + Timmy Willison CSS: Make sure elem.ownerDocument.defaultView is not null
Fixes gh-2866
Close gh-2867
1822945
@timmywil timmywil pushed a commit that referenced this issue Jan 27, 2016
@todorpr todorpr + Timmy Willison CSS: Make sure elem.ownerDocument.defaultView is not null
Fixes gh-2866
Close gh-2867
ab5992e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment