Skip to main content

Bug Tracker

Side navigation

#10394 closed bug (fixed)

Opened September 30, 2011 11:20PM UTC

Closed August 06, 2012 04:12PM UTC

Last modified August 15, 2012 10:35AM UTC

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" );
}
Attachments (0)
Change History (13)

Changed October 02, 2011 08:09PM UTC by timmywil comment:1

component: unfiledcss
owner: → 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.

Changed October 17, 2011 07:48AM UTC by trac-o-bot comment:2

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!

Changed November 29, 2011 11:03PM UTC by jfaris comment:3

This is happening when running our Jasmine tests (in HtmlUnit). The exception message is "Cannot find function removeAttribute in object [object CSSStyleDeclaration]"

Changed January 11, 2012 07:25AM UTC by pigletto@gmail.com comment:4

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

2. jQuery sets cssHook if browser doesn't support opacity:

if ( !jQuery.support.opacity ) {
	jQuery.cssHooks.opacity = {
            (...)
            style.removeAttribute( "filter" );
            (...)
        }
}

Changed January 11, 2012 01:59PM UTC by dmethvin comment:5

HtmlUnit is not on our list of supported browsers.

Changed April 26, 2012 02:23PM UTC by anonymous comment:6

It does not matter whether you are supporting HtmlUnit browser or not. This way of writing code is dirty.

Changed May 02, 2012 09:29PM UTC by howard@optify.net comment:7

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);}
    }
  }

Changed August 02, 2012 05:23PM UTC by anonymous comment:8

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

Changed August 06, 2012 04:11PM UTC by gnarf comment:9

resolution: invalid
status: closedreopened

Changed August 06, 2012 04:12PM UTC by gnarf comment:10

resolution: → fixed
status: reopenedclosed

Changed August 06, 2012 04:12PM UTC by gnarf comment:11

milestone: None1.8

Changed August 06, 2012 06:23PM UTC by timmywil comment:12

#12178 is a duplicate of this ticket.

Changed August 15, 2012 10:35AM UTC by anonymous comment:13

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.