2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('dom-style', function(Y) {
12 * Add style management functionality to DOM.
14 * @submodule dom-style
18 var DOCUMENT_ELEMENT = 'documentElement',
19 DEFAULT_VIEW = 'defaultView',
20 OWNER_DOCUMENT = 'ownerDocument',
23 CSS_FLOAT = 'cssFloat',
24 STYLE_FLOAT = 'styleFloat',
25 TRANSPARENT = 'transparent',
26 GET_COMPUTED_STYLE = 'getComputedStyle',
27 GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
29 WINDOW = Y.config.win,
30 DOCUMENT = Y.config.doc,
31 UNDEFINED = undefined,
35 TRANSFORM = 'transform',
43 re_unit = /width|height|top|left|right|bottom|margin|padding/i;
45 Y.Array.each(VENDOR_TRANSFORM, function(val) {
46 if (val in DOCUMENT[DOCUMENT_ELEMENT].style) {
59 * Sets a style property for a given element.
61 * @param {HTMLElement} An HTMLElement to apply the style to.
62 * @param {String} att The style property to set.
63 * @param {String|Number} val The value.
65 setStyle: function(node, att, val, style) {
66 style = style || node.style;
67 var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES;
70 if (val === null || val === '') { // normalize unsetting
72 } else if (!isNaN(new Number(val)) && re_unit.test(att)) { // number values may need a unit
73 val += Y_DOM.DEFAULT_UNIT;
76 if (att in CUSTOM_STYLES) {
77 if (CUSTOM_STYLES[att].set) {
78 CUSTOM_STYLES[att].set(node, val, style);
79 return; // NOTE: return
80 } else if (typeof CUSTOM_STYLES[att] === 'string') {
81 att = CUSTOM_STYLES[att];
83 } else if (att === '') { // unset inline styles
92 * Returns the current style value for the given property.
94 * @param {HTMLElement} An HTMLElement to get the style from.
95 * @param {String} att The style property to get.
97 getStyle: function(node, att, style) {
98 style = style || node.style;
99 var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES,
103 if (att in CUSTOM_STYLES) {
104 if (CUSTOM_STYLES[att].get) {
105 return CUSTOM_STYLES[att].get(node, att, style); // NOTE: return
106 } else if (typeof CUSTOM_STYLES[att] === 'string') {
107 att = CUSTOM_STYLES[att];
111 if (val === '') { // TODO: is empty string sufficient?
112 val = Y_DOM[GET_COMPUTED_STYLE](node, att);
120 * Sets multiple style properties.
122 * @param {HTMLElement} node An HTMLElement to apply the styles to.
123 * @param {Object} hash An object literal of property:value pairs.
125 setStyles: function(node, hash) {
126 var style = node.style;
127 Y.each(hash, function(v, n) {
128 Y_DOM.setStyle(node, n, v, style);
133 * Returns the computed style for the given node.
134 * @method getComputedStyle
135 * @param {HTMLElement} An HTMLElement to get the style from.
136 * @param {String} att The style property to get.
137 * @return {String} The computed value of the style property.
139 getComputedStyle: function(node, att) {
141 doc = node[OWNER_DOCUMENT];
143 if (node[STYLE] && doc[DEFAULT_VIEW] && doc[DEFAULT_VIEW][GET_COMPUTED_STYLE]) {
144 val = doc[DEFAULT_VIEW][GET_COMPUTED_STYLE](node, null)[att];
150 // normalize reserved word float alternatives ("cssFloat" or "styleFloat")
151 if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][CSS_FLOAT] !== UNDEFINED) {
152 Y_DOM.CUSTOM_STYLES[FLOAT] = CSS_FLOAT;
153 } else if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][STYLE_FLOAT] !== UNDEFINED) {
154 Y_DOM.CUSTOM_STYLES[FLOAT] = STYLE_FLOAT;
157 // fix opera computedStyle default color unit (convert to rgb)
159 Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
160 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
161 val = view[GET_COMPUTED_STYLE](node, '')[att];
163 if (re_color.test(att)) {
164 val = Y.Color.toRGB(val);
172 // safari converts transparent to rgba(), others use "transparent"
174 Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
175 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
176 val = view[GET_COMPUTED_STYLE](node, '')[att];
178 if (val === 'rgba(0, 0, 0, 0)') {
187 Y.DOM._getAttrOffset = function(node, attr) {
188 var val = Y.DOM[GET_COMPUTED_STYLE](node, attr),
189 offsetParent = node.offsetParent,
194 if (val === 'auto') {
195 position = Y.DOM.getStyle(node, 'position');
196 if (position === 'static' || position === 'relative') {
198 } else if (offsetParent && offsetParent[GET_BOUNDING_CLIENT_RECT]) {
199 parentOffset = offsetParent[GET_BOUNDING_CLIENT_RECT]()[attr];
200 offset = node[GET_BOUNDING_CLIENT_RECT]()[attr];
201 if (attr === 'left' || attr === 'top') {
202 val = offset - parentOffset;
204 val = parentOffset - node[GET_BOUNDING_CLIENT_RECT]()[attr];
212 Y.DOM._getOffset = function(node) {
217 pos = Y_DOM.getStyle(node, 'position');
219 parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'left'), 10),
220 parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'top'), 10)
223 if ( isNaN(xy[0]) ) { // in case of 'auto'
224 xy[0] = parseInt(Y_DOM.getStyle(node, 'left'), 10); // try inline
225 if ( isNaN(xy[0]) ) { // default to offset value
226 xy[0] = (pos === 'relative') ? 0 : node.offsetLeft || 0;
230 if ( isNaN(xy[1]) ) { // in case of 'auto'
231 xy[1] = parseInt(Y_DOM.getStyle(node, 'top'), 10); // try inline
232 if ( isNaN(xy[1]) ) { // default to offset value
233 xy[1] = (pos === 'relative') ? 0 : node.offsetTop || 0;
242 Y_DOM.CUSTOM_STYLES.transform = {
243 set: function(node, val, style) {
244 style[TRANSFORM] = val;
247 get: function(node, style) {
248 return Y_DOM[GET_COMPUTED_STYLE](node, TRANSFORM);
255 var PARSE_INT = parseInt,
278 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
279 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
280 re_hex3: /([0-9A-F])/gi,
282 toRGB: function(val) {
283 if (!Y.Color.re_RGB.test(val)) {
284 val = Y.Color.toHex(val);
287 if(Y.Color.re_hex.exec(val)) {
289 PARSE_INT(RE.$1, 16),
290 PARSE_INT(RE.$2, 16),
297 toHex: function(val) {
298 val = Y.Color.KEYWORDS[val] || val;
299 if (Y.Color.re_RGB.exec(val)) {
301 Number(RE.$1).toString(16),
302 Number(RE.$2).toString(16),
303 Number(RE.$3).toString(16)
306 for (var i = 0; i < val.length; i++) {
307 if (val[i].length < 2) {
308 val[i] = '0' + val[i];
315 if (val.length < 6) {
316 val = val.replace(Y.Color.re_hex3, '$1$1');
319 if (val !== 'transparent' && val.indexOf('#') < 0) {
323 return val.toUpperCase();
330 }, '3.3.0' ,{requires:['dom-base']});