Skip to main content

Bug Tracker

Side navigation

#7590 closed enhancement (wontfix)

Opened November 22, 2010 09:28AM UTC

Closed November 22, 2010 03:53PM UTC

Last modified November 22, 2010 08:24PM UTC

jQuery.fn.css returns computed styles on the first DOM node or no-ops

Reported by: danbeam Owned by:
Priority: undecided Milestone: 1.5
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

Hello fellow jQuery developers,

While I was mucking around in jQuery's source (here: https://github.com/jquery/jquery/blob/master/src/css.js#L17), I noticed that calling jQuery.fn.css with no arguments wasn't one of the cases planned for in this function. I additionally checked the documentation (http://api.jquery.com/css/) and found no cases of calling jQuery.fn.css() without a propertyName or value/object to set styles of a node/set of nodes.

So, I thought it would be cool if jQuery.fn.css() would return all computed styles on the first HTMLElement in a set (if there is one), otherwise no-op, as calling something like $('body').css() seemed to be throwing errors anyways (TypeError: Cannot call method 'replace' of undefined).

Attachments (0)
Change History (5)

Changed November 22, 2010 09:53AM UTC by danbeam comment:1

Changed November 22, 2010 03:53PM UTC by snover comment:2

resolution: → wontfix
status: newclosed

This is basically the same request as #4461 but with a more specious use case. And, of course, calling a method in a manner contrary to the API documentation will always have undefined results (in this case, an error).

Changed November 22, 2010 05:51PM UTC by danbeam comment:3

I understand using jQuery in an undocumented way would have an uncertain outcome, but I think this case could easily be accounted for with a no-op or with my waiting pull request (https://github.com/jquery/jquery/pull/85).

It's obviously not up to me, but it seems responsible to do one or the other.

Changed November 22, 2010 07:15PM UTC by dmethvin comment:4

I think it's rare that you'd want to retrieve all computed styles on an element, and enhancement #7169 already asks for the ability to pass in a list of specific css properties to return.

Changed November 22, 2010 08:24PM UTC by danbeam comment:5

The use case might be relatively rare, but it will be non-existent if it's not in the API, ;).

I personally I wanted to copy the computed styles from one node to another (https://github.com/danbeam/ellipsis/blob/master/jquery/jquery.ellipsis.js#L71). I'm not sure how popular this might be, but it seemed legitimate enough for me to write this and test this patch on 10 browsers, haha (though it opens a can of worms when you uncover the computed style bugs underneath the surface, like http://tomcat76.open-theweb.net/pages/op-font-size-calc.html).

I'm pretty sure .getComputedStyle() works by flushing any pending repaints and returning a CSSDeclaration containing ALL the current styles on an HTMLElement (https://github.com/danbeam/jquery/blob/master/src/css.js#L278), which can be iterated over with .getPropertyValue() (https://github.com/danbeam/jquery/blob/master/src/css.js#L279). So, essentially you're getting all the styles any time you call $(set).css('single-property') anyways, so I just thought there might be a use for this given that it probably isn't terribly inefficient.

Perhaps if there's no implementation of the other bug you mentioned (which was my initial approach), I can do this instead.