On Jan 20, 10:08 am, Scott Sauyet <scott.sau...@gmail.com> wrote:
> On Jan 20, 7:50 am, David Mark <dmark.cins...@gmail.com> wrote:
>
> > On Jan 19, 11:07 am, Scott Sauyet <scott.sau...@gmail.com> wrote:
> >> On Jan 19, 10:28 am, Dr_KralNOS...@nyc.rr.com wrote:
> >>> Determining default style properties
>
> >> getComputedStyle
> >> currentStyle
>
> >> There are plenty of examples (of varying degrees of correctness and
> >> efficiency!) online describing how you might combine these two into
> >> your own function.
>
> > More like there are ten tons of bad examples. Where's one good
> > article (or example?) It's non-trivial to write a solid
> > getComputedStyle wrapper, even when limited to dealing with a handful
> > of styles (and apps should be designed with that in mind).
>
> This is true. If anything, it's an understatement.
Agreed.
>
> A few things you will probably have to keep in mind:
>
> - "opacity" is expressed in some places as a decimal, others as a
> percentage, and it is handled very differently in IE than in most
> other browsers.
Yes, and among older non-IE browsers. But this is not specific to
computing styles (just inherent to style in general).
>
> - "float" will require special handling (search for "cssFloat" and
> "styleFloat").
Yes.
>
> - inheritable properties may require you to walk up the DOM to
> find them defined.
Right, among other calculations (some impossible) that would be
required convert a "cascaded" style to proper computed value. It's
one of those things that is half-assed in the "standard" libraries.
Some make no distinction between them at all (and no comments or
documentation to indicate the shortcomings).
>
> - You have to decide how to handle children of elements with
> "display: none" that themselves have some other display value.
There's nothing to decide. See the CSS specs.
>
> - You'll have to deal with the difference in name formats between
> JS and CSS: "backgroundColor" vs "background-color".
Perhaps. It makes for a friendlier interface for beginners used to
reading CSS, but not JS. I've never bothered with it at all.
>
> And that is probably just the tip of the iceberg.
Pretty much. The computing of styles to compensate for IE's
shortcomings is a major can of worms. That's why it is best to
identify what you need out of a wrapper like this before you design
it. If, for example, left and top will always be in pixels, you know
you can get left and top reliably in cross-browser fashion (for
browsers that support getComputedStyle or IE). Set the inline styles
to pixel-based values and you can support the browsers that predate
getComputedStyle. Context and simplicity are everything with this
stuff, which is why outlandish, complex, half-assed error-prone
nightmare scripts (that must be swapped out in their entirety every
six months) are the opposite of a good idea. Yes, there are a lot of
people out there who have yet to have this epiphany. It's coming.
>
> > So if your design dictates a comprehensive cross-browser
> > getComputedStyle wrapper, change the design as it's a hard way to go.
>
> If on the other hand, you have only a limited number of properties you
> need to support, it may not be too bad.
>
Exactly.