Last Comment Bug 707691 - Retrieving percentage CSS values by javascript (computed style of left|right)
: Retrieving percentage CSS values by javascript (computed style of left|right)
Status: UNCONFIRMED
: testcase
Product: Core
Classification: Components
Component: DOM: CSS Object Model (show other bugs)
: Trunk
: All All
: -- normal (vote)
: ---
Assigned To: Nobody; OK to take it and work on it
:
Mentors:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-12-05 08:05 PST by Stefano Ceschi Berrini
Modified: 2011-12-06 12:59 PST (History)
3 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
testcase (329 bytes, text/html)
2011-12-05 18:08 PST, j.j.
no flags Details

Description Stefano Ceschi Berrini 2011-12-05 08:05:48 PST
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7

Steps to reproduce:

I have this scenario:

Markup: <div id="box">box</div>

Css : 

#box{
    width:200px;
    height:200px;
    left:10%;
    position:absolute;
    background:red;
}

and I'm trying to retrieve the left position by javascript in this way:

function getStyle(el,styleProp){ //from ppk's quirksmode
    var x = document.getElementById(el);
    if (x.currentStyle)
        var y = x.currentStyle[styleProp];
    else if (window.getComputedStyle)
        var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
    return y;
}

var left = getStyle('box','left');


Actual results:

I get the left value in px (that is obv not constant and depends on the actual window width)


Expected results:

I should get (like every other browser) '10%'

The running test case is here : http://jsfiddle.net/steweb/tUAKA/
Comment 1 j.j. 2011-12-05 18:08:20 PST
Created attachment 579181 [details]
testcase

> I should get (like every other browser) '10%'
Well, I get "102px" in Opera 11.5
Comment 2 Boris Zbarsky [:bz] 2011-12-05 18:19:31 PST
The spec for this is still in flux.  Once it looks like it may be stabilizing, we'll align to it wholesale.

Note that per spec the correct answer here used to be a pixel length, which is what we implement at the moment.  But as I said, the spec has been changing.
Comment 3 j.j. 2011-12-05 18:33:40 PST
The "resolved value" of "left" is the "computed value"
http://dev.w3.org/csswg/cssom/#resolved-value

The computed value of "left" is:
if specified as a ‘<percentage>’, the specified value;
http://dev.w3.org/csswg/css3-positioning/#box-offsets-trbl
Comment 4 Boris Zbarsky [:bz] 2011-12-05 18:39:52 PST
Yes, I know what the current CSSOM drafts say.  For this particular property it's different than what the combination of DOM2 CSS and CSS2 used to say, as comment 2 notes.
Comment 5 Stefano Ceschi Berrini 2011-12-06 02:32:40 PST
(In reply to j.j. from comment #1)
> Created attachment 579181 [details]
> testcase
> 
> > I should get (like every other browser) '10%'
> Well, I get "102px" in Opera 11.5

Sorry, I had to specify.. on every other browser on mac osx (opera 11.5 included => http://i39.tinypic.com/2z6847n.png) 

On linux (Opera 11.5) I get value in pixel.. it's weird.. and on firefox 7 (on winXP) I get the %..

This is happening because of the spec still in flux? So, there's no way atm to retrieve % value on firefox (without parsing the css stylesheet) ?
Comment 6 Boris Zbarsky [:bz] 2011-12-06 11:04:26 PST
> and on firefox 7 (on winXP) I get the %..

That's .... quite odd.  Shouldn't happen.

> So, there's no way atm to retrieve % value on firefox (without parsing the css
> stylesheet) ?

At the moment, no.
Comment 7 David Baron :dbaron: ⌚️UTC+2 (mostly busy through August 4; review requests must explain patch) 2011-12-06 11:22:54 PST
You should get a % if the element is display:none or inside something that's display:none.  Perhaps there's a timing issue causing that to be the case some of the time?
Comment 8 Stefano Ceschi Berrini 2011-12-06 12:51:32 PST
(In reply to David Baron [:dbaron] from comment #7)
> You should get a % if the element is display:none or inside something that's
> display:none.  Perhaps there's a timing issue causing that to be the case
> some of the time?

That works! Perfect, thanks.. btw, it's not clear why the behav is different depending on the OS (same browser/version).. Timinng issue, could it be something related to when the 'domready' is called ?
Comment 9 Stefano Ceschi Berrini 2011-12-06 12:59:11 PST
(In reply to Boris Zbarsky (:bz) from comment #6)
> > and on firefox 7 (on winXP) I get the %..
> 
> That's .... quite odd.  Shouldn't happen.
> 
Yea that's really odd.. it also happens on opera (11.5 mac os/ubuntu, different behav).

> > So, there's no way atm to retrieve % value on firefox (without parsing the css
> > stylesheet) ?
> 
> At the moment, no.

Like David said, setting display:none solves the problem

Note You need to log in before you can comment on or make changes to this bug.