getComputedStyle, where is my LI?

G

Garrett Smith

Opera 10.1 has a buggy implementation of getComputedStyle top.

I've noticed now that getting the top value for an LI, in normal flow,
returns what would be an in-flow offset y coordinate.

Not surprising. I noticed a similar bug years ago in Opera.
<URL:
http://dhtmlkitchen.com/?category=/...tyle-Returns-Margin-for-Unset-Top-Left-Values

(messy URI and latency, probably due to my configuration of Blojsom);

In the following example, the "top" value for each LI should be "auto"
or 0, based on the definition of absolute value for top in CSS 2[1].
Definitions of "absolute value" changed from CSS 2 to CSS 2.1.

Result:
Firefox: "0px"
Chrome 2: "auto"
Opera 10.1: "133px"

What the ****?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>getComputedStyle(top) Opera Bug</title>
<style type="text/css">
li {
/* Remove 'position: relative;' for result "auto" in Gecko */
position: relative;
}
</style>
</head>
<body>
<ul>
<li><div>item 0</div></li>
<li><div>item 1</div></li>
<li><div>item 2</div></li>
<li><div>item 3</div></li>
<li><div>item 4</div></li>
<li><div>item 5</div></li>
<li><div>item 6</div></li>
<li id="i7"><div>item 7</div></li>
<li><div>item 8</div></li>
<li><div>item 9</div></li>
</ul>
<script type="text/javascript">
onload = function(){
var i7 = document.getElementById("i7");
var top = document.defaultView.getComputedStyle(i7,"").top;
var m = document.getElementById("m");
m.innerHTML = top;
};
</script>
<pre id="m">-</pre>
</body>
</html>

After over 10 years of having fickle, limited APIs to read styles, the
best efforts of browser vendors and CSS WG API designers have left us
with an API that is unreliable, complicated by design, non-interoperable
(doesn't work in IE and works differently in various browsers and
verions).

Did the browser vendors and CSS WG put forth the effort to cooperate to
design this? If so, do the implementation bugs indicate a problem in the
API design itself?

Definitions of "absolute value" are complex and vary between CSS
versions. That would seem to complicate implementation.

If the API design is such a problem, then it should be acknowledged. The
problem should not be denied (whether out of ignorance, laziness,
unwillingness, fear, etc).

As I understand CSS2.1, the following:

li {
position: relative;
}

should get the result "0px".

The CSS2 spec states:

| The 'top' and 'bottom' properties move relatively positioned
| element(s) up or down without changing their size. 'top' moves the
| boxes down, and 'bottom' moves them up. Since boxes are not split or
| stretched as a result of 'top' or 'bottom', the computed values are
| always: top = -bottom. If both are 'auto', their computed values are
| both '0'. If one of them is 'auto', it becomes the negative of the
| other. If neither is 'auto', 'bottom' is ignored (i.e., the computed
| value of 'bottom' will be minus the value of 'top').

The default value is "auto", and when both top and bottom are 'auto',
their computed values are both '0'.

Fortunately, the solution is to use position: relative and top: 0. That
gets me "0px" in all three browsers.

(meanwhile Apple, aka "those scumbags in Cupertino" are patenting CSS
animations and Touch events, marketing iPhone, iPod, etc).

[1]<URL: http://www.w3.org/TR/CSS2/visuren.html#position-props >
[2]<URL: http://www.w3.org/TR/CSS2/visuren.html#relative-positioning >
 
D

David Mark

Opera 10.1 has a buggy implementation of getComputedStyle top.

I've noticed now that getting the top value for an LI, in normal flow,
returns what would be an in-flow offset y coordinate.

Not surprising. I noticed a similar bug years ago in Opera.
<URL:http://dhtmlkitchen.com/?category=/Browsers/&date=2007/11/08/&entry=O...



(messy URI and latency, probably due to my configuration of Blojsom);

In the following example, the "top" value for each LI should be "auto"
or 0, based on the definition of absolute value for top in CSS 2[1].
Definitions of "absolute value" changed from CSS 2 to CSS 2.1.

Result:
   Firefox:    "0px"
   Chrome 2:   "auto"
   Opera 10.1: "133px"

What the ****?

Two words: who cares? If you wanted to - for example - move the list
item down two pixels, you would add two to whatever is there. In the
case of "auto" you would assume 0. GIGO.

If instead you are interested in the offset position, use offsetLeft/
Top, preferably within the confines of a positioned parent element.
 
G

Garrett Smith

David said:
Opera 10.1 has a buggy implementation of getComputedStyle top.
[...]

In the following example, the "top" value for each LI should be "auto"
or 0, based on the definition of absolute value for top in CSS 2[1].
Definitions of "absolute value" changed from CSS 2 to CSS 2.1.

Result:
Firefox: "0px"
Chrome 2: "auto"
Opera 10.1: "133px"

What the ****?

Two words: who cares?

The latest versions of the most popular, and mature browsers not
following 10yr old standards? Yeah, I care about that.

In more than 10 years, there is not a way to read an element's style
value, cross browser.

It is a problem.

Given the rate of progress by the (useless individuals in the CSSWG),
there is no foreseeable change on the horizon.

*That* is a worse problem.

If you wanted to - for example - move the list
item down two pixels, you would add two to whatever is there. In the
case of "auto" you would assume 0. GIGO.

Is the answer to the question: How can I position an element. Who asked?

I would not assume "0" for auto, because I read the specification
(indeed I cited it on this very thread) and I understand that such
assumption would be incorrect. "auto" is not 0.
If instead you are interested in the offset position, use offsetLeft/
Top, preferably within the confines of a positioned parent element.
OffsetLeft and offsetTop are not standard. That approach works for UL,
but not other elements, such as TD (I recall problems with Safari 2).

Oddly, what Opera returns for the computed style string + "px" for the
value that you have described as "the offset position from the
positioned parent element."
 
J

Jorge

(...)
(meanwhile Apple, aka "those scumbags in Cupertino"

No news about the scumbags in Redmond (WA, USA), Espoo (FIN) or
Waterloo (CA), right ?

No, because as they're still inventing it, they've got nothing to
patent yet... LOL

Never mind, see below.

Anyway, what does this have to do with anything in the post above ?
are patenting CSS animations (...)

About Apple's (great !) CSS animations/transforms :

Webkit.org:
http://webkit.org/blog/324/css-animation-2/
<quote>
We’re documenting these enhancements on webkit.org and are proposing
the specifications to standards bodies.
</quote>

w3.org:
http://www.w3.org/TR/css3-animations/
<quote>
This document was produced by a group operating under the 5 February
2004 W3C Patent Policy
</quote>

http://www.w3.org/Consortium/Patent-Policy-20040205/
<quote>
The W3C Patent Policy governs the handling of patents in the process
of producing Web standards. The goal of this policy is to assure that
Recommendations produced under this policy can be implemented on a
Royalty-Free (RF) basis.
</quote>

http://www.w3.org/Consortium/Patent-Policy-20040205/#def-RF
<quote>
With respect to a Recommendation developed under this policy, a W3C
Royalty-Free license shall mean a non-assignable, non-sublicensable
license to make, have made, use, sell, have sold, offer to sell,
import, and distribute and dispose of implementations of the
Recommendation that:
1. shall be available to all, worldwide, whether or not they are W3C
Members;
(...)
and Touch events (...)

When you say, as in:
http://lists.w3.org/Archives/Public/www-dom/2009OctDec/0029.html
<quote>
No, fortunately that API is patented so it won't be usable (and hence,
will not be used as a public standard).
</quote>

You should know that something, even if patented by a 3rd party, can
still become a w3 standard.

See Maciej's response to Olli's "I was a bit afraid of that Apple
might have done something evil like this":
http://lists.w3.org/Archives/Public/www-dom/2009OctDec/0034.html
<quote>
I note that Apple is a member of the Web Apps Working Group and thus
subject to the W3C Patent Policy with respect to Web Apps WG
deliverables. I can't comment on whether Apple would exclude any
patent claims relating to this area, if they turn out to be
essential
claims.
</quote>

The point is: patented (to protect their intellectual property) or
not, the intellectual property of Apple's APIs/inventions is theirs
anyway.

What matters is if Apple *wants* their invention to become a w3
standard. If they don't, they are in their own right (and it's not
"something evil like this"). If they do, they know pretty well what
are the steps to take.

There are many reasons for Apple to prefer their API to become a w3
standard... at the right time (which is probably: "not yet" :)

Had the w3c proposed a useful, well designed touch/gestures API
already, Apple wouldn't have had to invent it themselves, and we would
not be discussing this. But these design by commitee things don't
deliver so much nor so fast nor of course in anticipation to future
needs. They might be lucky if Apple gifts them this API too -as the
CSS animations/transforms- otherwise they're going to have to design
their own.

The <canvas> is patented too -if I got it right- in the "Unified
interest layer for user interface" US 2006/0015818...
 
V

VK

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>getComputedStyle(top) Opera Bug</title>
</head>
<body>
<ul>
<li><div>item 0</div></li>
<li><div>item 1</div></li>
<li><div>item 2</div></li>
<li><div>item 3</div></li>
<li><div>item 4</div></li>
<li><div>item 5</div></li>
<li><div>item 6</div></li>
<li id="i7"><span>item 7</span></li>
<li><div>item 8</div></li>
<li><div>item 9</div></li>
</ul>
<script type="text/javascript">
window.onload = function(){

$('m').innerHTML = (window.getComputedStyle)?
window.getComputedStyle($('i7'),null).getPropertyValue('top')
:
$('i7').currentStyle.top;

function $(id) {
return document.getElementById(id);
}
}
</script>
<pre id="m">-</pre>
</body>
</html>


IE8 auto
Fx 3.5.4 auto
Chrome 3.0 auto
Safari 4.0 auto
Opera 10.0 149px (or other px value)

Opera is right as usual with CSS topics, others are using a "lazy
implementation".

http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-OverrideAndComputed
Return Value:
CSSStyleDeclaration
The computed style.
The CSSStyleDeclaration is read-only and contains only absolute
values.

http://www.w3.org/TR/1998/REC-CSS2-19980512/cascade.html#computed-value
For absolute values, no computation is needed to find the computed
value.

If in CSS2.1 or CSS3 or anywhere later some W3C dork(s) decided that
"auto" is an absolute value that doesn't need any further computation
then the only suggestion would be to stop smoking that grass as it
definitely affects them. ;)
 
G

Garrett Smith

Jorge said:
No news about the scumbags in Redmond (WA, USA), Espoo (FIN) or
Waterloo (CA), right ?

Microsoft, and who else?
See Maciej's response to Olli's "I was a bit afraid of that Apple
might have done something evil like this":
http://lists.w3.org/Archives/Public/www-dom/2009OctDec/0034.html
<quote>
I note that Apple is a member of the Web Apps Working Group and thus
subject to the W3C Patent Policy with respect to Web Apps WG
deliverables. I can't comment on whether Apple would exclude any
patent claims relating to this area, if they turn out to be
essential
claims.
</quote>

Hopefully not. It means every program that uses mousemove must write
duplicate code for "touchmove", and uses the changedTouches[0], etc.

Programs that use mousemove/touchmove require more than double effort to
unit test.

Simulating touch events takes some 18 parameter variables, three of
those being TouchList objects, each requiring a "Touch" object. It is
actually more work to unit test code for iPhone, so to say that the
effort required to unit test code for iPhone is doubled would be
misleading. The effort is more than double.

Nokia is vying to get some of the touch screen consumer money.

As a (paying) member, these companies get some influence over what
gets standardized. What is disturbing is that many of the
representatives have very little knowledge of browsers, the relevant
standards, or RIA scripting experience. In particular, the
representatives from Microsoft and Nokia.

Meanwhile, developers are stuck with complicated, divergent,
non-interoperable strategies to answer the question of:

"What is the style of my element?"

in the latest versions of the latest browsers are produced by members of
the w3c, (including Apple). They fail to fulfill the requirement, in
even the most common, simple cases.

That is why I am picking on Apple.

A test suite is long overdue. Is there anyone on the w3c who is capable
of writing a test suite? Certainly the funding is there.

http://www.w3.org/Consortium/membership
 
D

David Mark

David said:
Opera 10.1 has a buggy implementation of getComputedStyle top.
[...]
In the following example, the "top" value for each LI should be "auto"
or 0, based on the definition of absolute value for top in CSS 2[1].
Definitions of "absolute value" changed from CSS 2 to CSS 2.1.
Result:
   Firefox:    "0px"
   Chrome 2:   "auto"
   Opera 10.1: "133px"
What the ****?
Two words: who cares?  

The latest versions of the most popular, and mature browsers not
following 10yr old standards? Yeah, I care about that.

Just like you asserted people should care about IE6 patching the
memory leaks. You miss the point that you wouldn't need to care if
you would just avoid these problems at the design stage.
In more than 10 years, there is not a way to read an element's style
value, cross browser.

It is a problem.

Given the rate of progress by the (useless individuals in the CSSWG),
there is no foreseeable change on the horizon.

*That* is a worse problem.

If you wanted to - for example - move the list


Is the answer to the question: How can I position an element. Who asked?

I would not assume "0" for auto, because I read the specification
(indeed I cited it on this very thread) and I understand that such
assumption would be incorrect. "auto" is not 0.

Doesn't matter. I wouldn't bother memorizing such useless trivia as
"auto" is not a computed style at all. If you run into walls like
this, change direction.
OffsetLeft and offsetTop are not standard. That approach works for UL,
but not other elements, such as TD (I recall problems with Safari 2).

Works great within a positioned parent. Doesn't matter if they are
real standards (yet). If you try to measure all the way to the root
of the document, you are going to end up with a lot of code.
Oddly, what Opera returns for the computed style string + "px" for the
value that you have described as "the offset position from the
positioned parent element."

Again, who cares what they say (or do?) The computed left/top of a
relatively positioned element is not something you should rely on.
That's always been the case, driven home by Opera more than once.
 
D

David Mark

[...]
Oddly, what Opera returns for the computed style string + "px" for the
value that you have described as "the offset position from the
positioned parent element."

Nothing odd about that at all. Opera has always fumbled computed top/
left and nobody in their right mind would use those to determine the
offset position of an element. Hell, IE doesn't even handle computed
styles (as discussed ad nauseum). You'll get lots of "auto" results
(among other things) out of it and will have to jump through hoops to
do the required computations. It's not a good strategy, end of story.

And yes, you could assume 0 for auto in your example as there is no
bottom position specified. You should also know that assigning both
top and bottom (or right and left) positions is problematic in older
browsers (some won't know what to make of it at all). Always best to
stick with what you know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top