#10394 closed bug (fixed)
jQuery.cssHooks.opacity throws exception on non-IE browsers
Reported by: | poulbak | Owned by: | poulbak |
---|---|---|---|
Priority: | low | Milestone: | 1.8 |
Component: | css | Version: | 1.6.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Whenever a browser returns 'false' for '$.support.opacity' the cssHooks.opacity code will be called. However, for non-IE browsers this will throw an exception on line 6522:
style.removeAttribute( "filter" );
because only IE has this method. I don't know how many browsers this concerns, but I think it would be best if no browsers throw exceptions. Honestly I don't understand the code cssHooks.opacity, but a simple way to prevent the error would of course be to wrap the call in a test like this:
if ( style.removeAttribute ) { style.removeAttribute( "filter" ); }
Change History (13)
comment:1 Changed 12 years ago by
Component: | unfiled → css |
---|---|
Owner: | set to poulbak |
Priority: | undecided → low |
Status: | new → pending |
comment:2 Changed 12 years ago by
Resolution: | → invalid |
---|---|
Status: | pending → closed |
Because we get so many tickets, we often need to return them to the initial reporter for more information. If that person does not reply within 14 days, the ticket will automatically be closed, and that has happened in this case. If you still are interested in pursuing this issue, feel free to add a comment with the requested information and we will be happy to reopen the ticket if it is still valid. Thanks!
comment:3 Changed 12 years ago by
This is happening when running our Jasmine tests (in HtmlUnit). The exception message is "Cannot find function removeAttribute in object [object CSSStyleDeclaration]"
comment:5 Changed 11 years ago by
The problem still exists in 1.7.1.
I've created a script that exposes a problem (requires jruby). Archive containing script and necessary jar files is available at: http://natcam.pl/media/htmlunit_jquery.tar.gz (start with ./run.sh)
Problem is caused by assumption (in jQuery code) that if a browser does not support 'opacity' then it supports style.removeAttribute. It is not true eg. for HTMLUnit browser. It is a result of these calls:
- jQuery checks for opacity support:
opacity: /^0.55/.test( a.style.opacity ),
which results in:
jQuery.support.opacity == false
- jQuery sets cssHook if browser doesn't support opacity:
if ( !jQuery.support.opacity ) { jQuery.cssHooks.opacity = { (...) style.removeAttribute( "filter" ); (...) } }
comment:7 Changed 11 years ago by
It does not matter whether you are supporting HtmlUnit browser or not. This way of writing code is dirty.
comment:8 Changed 11 years ago by
removeProperty is the W3C defined method. It's apparently not supported by IE <= 9. IE provides equivalent-ish functionality with removeAttribute.
So it seems like forward-looking code might look something like:
function removeStyleProperty(style,prop) { if (style.removeProperty) { style.removeProperty(prop); removeStyleProperty = function(style,prop) {style.removeProperty(prop);} } else if (style.removeAttribute) { style.removeAttribute(prop); removeStyleProperty = function(style,prop) {style.removeAttribute(prop);} } }
comment:9 Changed 11 years ago by
Please re-open this ticket, it's bitting me in jsdom, working with nodejs.
comment:10 Changed 11 years ago by
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Fixed this in https://github.com/jquery/jquery/commit/07e50933c4293818c5b36d368368656844e4df88 - just typoed the bug number... :(
comment:11 Changed 11 years ago by
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:12 Changed 11 years ago by
Milestone: | None → 1.8 |
---|
comment:14 Changed 11 years ago by
For anyone who found this page, but was stuck using 1.6.4 with HTMLUnit, I've found a workaround is to set a custom WebClient ScriptPreProcessor which does the following:
public String preProcess(HtmlPage htmlPage, String sourceCode, String sourceName, int lineNumber, HtmlElement htmlElement) { if(sourceName.contains("jquery-1.6.4")) { int startCodeIndex = sourceCode.indexOf("style.removeAttribute("); StringBuffer replacementCode = new StringBuffer(); replacementCode.append(sourceCode.substring(0, startCodeIndex )); replacementCode.append("try{"); int endCodeIndex = sourceCode.indexOf(";", startCodeIndex ) + 1; replacementCode.append(sourceCode.substring(startCodeIndex , endCodeIndex)); replacementCode.append("}catch(e){}"); replacementCode.append(sourceCode.substring(endCodeIndex)); return replacementCode.toString(); } return sourceCode; }
WARNING: Obviously this is not performing the required opacity behaviour, so do not use it if you require this. However it stops the test throwing an exception, or thousands of them if, like me, you are using it in a fadeout timer.
Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket.
Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/ Open the link and click to "Fork" (in the top menu) to get started.