Bug Tracker

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#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 Timmy Willison

Component: unfiledcss
Owner: set to poulbak
Priority: undecidedlow
Status: newpending

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.

comment:2 Changed 12 years ago by trac-o-bot

Resolution: invalid
Status: pendingclosed

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 jfaris

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 pigletto@…

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:

  1. jQuery checks for opacity support:

opacity: /^0.55/.test( a.style.opacity ),

which results in:

jQuery.support.opacity == false
  1. jQuery sets cssHook if browser doesn't support opacity:
if ( !jQuery.support.opacity ) {
	jQuery.cssHooks.opacity = {
            (...)
            style.removeAttribute( "filter" );
            (...)
        }
}

comment:6 Changed 11 years ago by dmethvin

HtmlUnit is not on our list of supported browsers.

comment:7 Changed 11 years ago by anonymous

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 howard@…

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 anonymous

Please re-open this ticket, it's bitting me in jsdom, working with nodejs.

comment:10 Changed 11 years ago by gnarf

Resolution: invalid
Status: closedreopened

comment:11 Changed 11 years ago by gnarf

Resolution: fixed
Status: reopenedclosed

comment:12 Changed 11 years ago by gnarf

Milestone: None1.8

comment:13 Changed 11 years ago by Timmy Willison

#12178 is a duplicate of this ticket.

comment:14 Changed 11 years ago by anonymous

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.

Note: See TracTickets for help on using tickets.