[CSSOM] GetComputedStyle : choose the unit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[CSSOM] GetComputedStyle : choose the unit

Innovimax SARL
Dear,

For the moment in http://dev.w3.org/csswg/cssom/

We can find

[[
interface ViewCSS {
  CSSStyleDeclaration getComputedStyle(in Element elt);
  CSSStyleDeclaration getComputedStyle(in Element elt, in DOMString pseudoElt);
};
The getComputedStyle method must return a live CSSStyleDeclaration object that contains the computed style declaration block for elt, when the pseudoElt argument is null, the empty string or omitted. Otherwise, for the pseudo-element pseudoElt of elt. [CSS]
]]

And then
[[
interface CSSStyleDeclaration {
           attribute DOMString cssText;
  DOMString getPropertyValue(in DOMString property);
  DOMString getPropertyPriority(in DOMString property);
  DOMString removeProperty(in DOMString property);
  void setProperty(in DOMString property, in DOMString value);
  void setProperty(in DOMString property, in DOMString value, in DOMString priority);
  readonly attribute unsigned long length;
  DOMString item(in unsigned long index);
  readonly attribute CSSRule parentRule;
};
]]

So it means, if I want to have access to the width of an element
getComputedStyle(el, "").getPropertyValue("width");

I propose to add a new parameter to getPropertyValue so that you can choose the unit

something like

getComputedStyle(el, "").getPropertyValue("width", "px");

And even to get information with respect to relative value

getComputedStyle(el, "").getPropertyValue("width", "em");


Regards,

Mohamed ZERGAOUI

--
Innovimax SARL
Consulting, Training & XML Development
9, impasse des Orteaux
75020 Paris
Tel : +33 9 52 475787
Fax : +33 1 4356 1746
http://www.innovimax.fr
RCS Paris 488.018.631
SARL au capital de 10.000 €
Reply | Threaded
Open this post in threaded view
|

Re: [CSSOM] GetComputedStyle : choose the unit

Garrett Smith
On Mon, Mar 23, 2009 at 9:07 AM, Innovimax SARL <[hidden email]> wrote:
> Dear,

[...]

> I propose to add a new parameter to getPropertyValue so that you can choose
> the unit
>
> something like
>
> getComputedStyle(el, "").getPropertyValue("width", "px");
>
> And even to get information with respect to relative value
>
> getComputedStyle(el, "").getPropertyValue("width", "em");
>
>
Why do you need this?

Garrett

Reply | Threaded
Open this post in threaded view
|

RE: [CSSOM] GetComputedStyle : choose the unit

Ludger Buenger
In reply to this post by Innovimax SARL

 

This is already possible in a slightly different way, so please read the fine standard!

 

Try this:

 

CSSPrimitiveValue widthValue = ((CSSPrimitiveValue)getComputedStyle(element).getPropertyCSSValue(“width”));

 

int widthInEM = widthValue.getFloatValue(CSSPrimitiveValue.CSS_EM);

or

int widthInPX = widthValue.getFloatValue(CSSPrimitiveValue.CSS_PX);

 

For convenience, I omitted instance, type null and other checks…

 

 

Ludger

 

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Innovimax SARL
Sent: Monday, March 23, 2009 6:08 PM
To: [hidden email]
Subject: [CSSOM] GetComputedStyle : choose the unit

 

Dear,

For the moment in http://dev.w3.org/csswg/cssom/

We can find

[[
interface ViewCSS {
  CSSStyleDeclaration getComputedStyle(in Element elt);
  CSSStyleDeclaration getComputedStyle(in Element elt, in DOMString pseudoElt);
};
The getComputedStyle method must return a live CSSStyleDeclaration object that contains the computed style declaration block for elt, when the pseudoElt argument is null, the empty string or omitted. Otherwise, for the pseudo-element pseudoElt of elt. [CSS]
]]

And then
[[
interface CSSStyleDeclaration {
           attribute DOMString cssText;
  DOMString getPropertyValue(in DOMString property);
  DOMString getPropertyPriority(in DOMString property);
  DOMString removeProperty(in DOMString property);
  void setProperty(in DOMString property, in DOMString value);
  void setProperty(in DOMString property, in DOMString value, in DOMString priority);
  readonly attribute unsigned long length;
  DOMString item(in unsigned long index);
  readonly attribute CSSRule parentRule;
};
]]

So it means, if I want to have access to the width of an element
getComputedStyle(el, "").getPropertyValue("width");

I propose to add a new parameter to getPropertyValue so that you can choose the unit

something like

getComputedStyle(el, "").getPropertyValue("width", "px");

And even to get information with respect to relative value

getComputedStyle(el, "").getPropertyValue("width", "em");


Regards,

Mohamed ZERGAOUI

--
Innovimax SARL
Consulting, Training & XML Development
9, impasse des Orteaux
75020 Paris
Tel : +33 9 52 475787
Fax : +33 1 4356 1746
http://www.innovimax.fr
RCS Paris 488.018.631
SARL au capital de 10.000 €

--
This message was scanned by ESVA and is believed to be clean.
Click here to report this message as spam.

Reply | Threaded
Open this post in threaded view
|

Re: [CSSOM] GetComputedStyle : choose the unit

Boris Zbarsky
Ludger Buenger wrote:
> CSSPrimitiveValue widthValue =
> ((CSSPrimitiveValue)getComputedStyle(element).getPropertyCSSValue(“width”));

Though note https://bugzilla.mozilla.org/show_bug.cgi?id=474655#c0

-Boris

Reply | Threaded
Open this post in threaded view
|

Re: [CSSOM] GetComputedStyle : choose the unit

Garrett Smith
In reply to this post by Ludger Buenger
On Mon, Mar 23, 2009 at 10:20 AM, Ludger Buenger
<[hidden email]> wrote:

>
>
> This is already possible in a slightly different way, so please read the
> fine standard!
>
>
>
> Try this:
>
>

In what? Not a browser, obviously.

If changed to:-

var prop = document.defaultView.getComputedStyle(document.body,
'').getPropertyCSSValue('width')
var value = prop.getFloatValue(CSSPrimitiveValue.CSS_PX);

It would work in some browsers. Replacing CSS_PX with CSS_EM should
result in error.

CSS_EMS might work in some browsers.

The API is designed in a way that pretty much requires try-catch because:-

| getFloatValue
|    This method is used to get a float value in a specified unit.
| If this CSS value doesn't contain a float value or can't be converted
| into the specified unit, a DOMException is raised.

It doesn't work reliably. What happens if an error is thrown?

> For convenience, I omitted instance, type null and other checks…
>

I am not saying you have a bad idea, but can you explain why you need
this to work?

>
>
> Ludger
>

[...]

Garrett

Reply | Threaded
Open this post in threaded view
|

Re: [CSSOM] GetComputedStyle : choose the unit

Innovimax SARL
In reply to this post by Boris Zbarsky
Thanks Boris for pointing this out

It doesn't surprise me since implementing it this way make a heavy burden on the implementation

That's why I was asking for a way to tell the implementation which unit I need so it can gracefully give an understandable error message if it cannot handle it, instead of doing some strange gymnastic with a lot of casting

Mohamed

2009/3/23 Boris Zbarsky <[hidden email]>
Ludger Buenger wrote:
CSSPrimitiveValue widthValue = ((CSSPrimitiveValue)getComputedStyle(element).getPropertyCSSValue(“width”));

Though note https://bugzilla.mozilla.org/show_bug.cgi?id=474655#c0

-Boris



--
Innovimax SARL
Consulting, Training & XML Development
9, impasse des Orteaux
75020 Paris
Tel : +33 9 52 475787
Fax : +33 1 4356 1746
http://www.innovimax.fr
RCS Paris 488.018.631
SARL au capital de 10.000 €