Bug Tracker

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#7590 closed enhancement (wontfix)

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:


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).

Change History (5)

comment:2 Changed 11 years ago by snover

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).

comment:3 Changed 11 years ago by danbeam

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.

comment:4 Changed 11 years ago by dmethvin

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.

comment:5 Changed 11 years ago by danbeam

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.

Note: See TracTickets for help on using tickets.