- From: Garrett Smith <dhtmlkitchen@gmail.com>
- Date: Tue, 22 Sep 2009 09:27:42 -0700
- To: www-style <www-style@w3.org>
Travis Leithead recently brought up the issue of reading styles again and it certainly is a pain point. There really is no good API for reading a style value. Scripts frequently have to deal with this task and use a combination of the "standard" method and the "IE/currentStyle" approach to (try to) get a style value consistent across browsers and often make use of IE's |runtimeStyle| property to do conversion of whatever currentStyle returns to px. A solution that is based on two different APIs does not work very well. The standard document.defaultView.getComputedStyle and IE currentStyle both have problems of their own. * document.defaultView.getComputedStyle * Too generic. The "absolute value" description is not nailed down. * Not flexible enough: A program that wants the unit in px or em or rgb or hex, etc, does not have that option. * Too verbose. The defaultView is unrelated to the Element (the DOM spec says: "a computed style is related to an Element node"). Verbosity is not such a problem with operations that don't occur frequently throughout the code. Reading styles is a common task. * currentStyle. * Not flexible enough. A program gets the value set in the stylesheet. If the stylesheet has EM or auto or %, then that value is returned. * simple and directly on the element. New Proposal: var height = x.getStyleAs("height", "px"); height = parseInt(height, 10); For color:- var color = x.getStyleAs("color", "rgba"); if(!color) { // failed. color = x.getStyleAs("color", "rgb"); } This addresses the problem of reading a style value. The primitiveValue is defined in the relevant CSS specification for the property. A corresponding method to set a value would not seem to be necessary, as any value could be set with: x.style.color = "papayawhip"; Garrett
Received on Tuesday, 22 September 2009 16:28:23 UTC