2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('dom-style', function (Y, NAME) {
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',
36 TRANSFORMORIGIN = 'transformOrigin',
45 re_unit = /width|height|top|left|right|bottom|margin|padding/i;
47 Y.Array.each(VENDOR_TRANSFORM, function(val) {
48 if (val in DOCUMENT[DOCUMENT_ELEMENT].style) {
50 TRANSFORMORIGIN = val + "Origin";
62 * Sets a style property for a given element.
64 * @param {HTMLElement} An HTMLElement to apply the style to.
65 * @param {String} att The style property to set.
66 * @param {String|Number} val The value.
68 setStyle: function(node, att, val, style) {
69 style = style || node.style;
70 var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES;
73 if (val === null || val === '') { // normalize unsetting
75 } else if (!isNaN(new Number(val)) && re_unit.test(att)) { // number values may need a unit
76 val += Y_DOM.DEFAULT_UNIT;
79 if (att in CUSTOM_STYLES) {
80 if (CUSTOM_STYLES[att].set) {
81 CUSTOM_STYLES[att].set(node, val, style);
82 return; // NOTE: return
83 } else if (typeof CUSTOM_STYLES[att] === 'string') {
84 att = CUSTOM_STYLES[att];
86 } else if (att === '') { // unset inline styles
95 * Returns the current style value for the given property.
97 * @param {HTMLElement} An HTMLElement to get the style from.
98 * @param {String} att The style property to get.
100 getStyle: function(node, att, style) {
101 style = style || node.style;
102 var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES,
106 if (att in CUSTOM_STYLES) {
107 if (CUSTOM_STYLES[att].get) {
108 return CUSTOM_STYLES[att].get(node, att, style); // NOTE: return
109 } else if (typeof CUSTOM_STYLES[att] === 'string') {
110 att = CUSTOM_STYLES[att];
114 if (val === '') { // TODO: is empty string sufficient?
115 val = Y_DOM[GET_COMPUTED_STYLE](node, att);
123 * Sets multiple style properties.
125 * @param {HTMLElement} node An HTMLElement to apply the styles to.
126 * @param {Object} hash An object literal of property:value pairs.
128 setStyles: function(node, hash) {
129 var style = node.style;
130 Y.each(hash, function(v, n) {
131 Y_DOM.setStyle(node, n, v, style);
136 * Returns the computed style for the given node.
137 * @method getComputedStyle
138 * @param {HTMLElement} An HTMLElement to get the style from.
139 * @param {String} att The style property to get.
140 * @return {String} The computed value of the style property.
142 getComputedStyle: function(node, att) {
144 doc = node[OWNER_DOCUMENT],
147 if (node[STYLE] && doc[DEFAULT_VIEW] && doc[DEFAULT_VIEW][GET_COMPUTED_STYLE]) {
148 computed = doc[DEFAULT_VIEW][GET_COMPUTED_STYLE](node, null);
149 if (computed) { // FF may be null in some cases (ticket #2530548)
157 // normalize reserved word float alternatives ("cssFloat" or "styleFloat")
158 if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][CSS_FLOAT] !== UNDEFINED) {
159 Y_DOM.CUSTOM_STYLES[FLOAT] = CSS_FLOAT;
160 } else if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][STYLE_FLOAT] !== UNDEFINED) {
161 Y_DOM.CUSTOM_STYLES[FLOAT] = STYLE_FLOAT;
164 // fix opera computedStyle default color unit (convert to rgb)
166 Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
167 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
168 val = view[GET_COMPUTED_STYLE](node, '')[att];
170 if (re_color.test(att)) {
171 val = Y.Color.toRGB(val);
179 // safari converts transparent to rgba(), others use "transparent"
181 Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
182 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
183 val = view[GET_COMPUTED_STYLE](node, '')[att];
185 if (val === 'rgba(0, 0, 0, 0)') {
194 Y.DOM._getAttrOffset = function(node, attr) {
195 var val = Y.DOM[GET_COMPUTED_STYLE](node, attr),
196 offsetParent = node.offsetParent,
201 if (val === 'auto') {
202 position = Y.DOM.getStyle(node, 'position');
203 if (position === 'static' || position === 'relative') {
205 } else if (offsetParent && offsetParent[GET_BOUNDING_CLIENT_RECT]) {
206 parentOffset = offsetParent[GET_BOUNDING_CLIENT_RECT]()[attr];
207 offset = node[GET_BOUNDING_CLIENT_RECT]()[attr];
208 if (attr === 'left' || attr === 'top') {
209 val = offset - parentOffset;
211 val = parentOffset - node[GET_BOUNDING_CLIENT_RECT]()[attr];
219 Y.DOM._getOffset = function(node) {
224 pos = Y_DOM.getStyle(node, 'position');
226 parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'left'), 10),
227 parseInt(Y_DOM[GET_COMPUTED_STYLE](node, 'top'), 10)
230 if ( isNaN(xy[0]) ) { // in case of 'auto'
231 xy[0] = parseInt(Y_DOM.getStyle(node, 'left'), 10); // try inline
232 if ( isNaN(xy[0]) ) { // default to offset value
233 xy[0] = (pos === 'relative') ? 0 : node.offsetLeft || 0;
237 if ( isNaN(xy[1]) ) { // in case of 'auto'
238 xy[1] = parseInt(Y_DOM.getStyle(node, 'top'), 10); // try inline
239 if ( isNaN(xy[1]) ) { // default to offset value
240 xy[1] = (pos === 'relative') ? 0 : node.offsetTop || 0;
249 Y_DOM.CUSTOM_STYLES.transform = {
250 set: function(node, val, style) {
251 style[TRANSFORM] = val;
254 get: function(node, style) {
255 return Y_DOM[GET_COMPUTED_STYLE](node, TRANSFORM);
259 Y_DOM.CUSTOM_STYLES.transformOrigin = {
260 set: function(node, val, style) {
261 style[TRANSFORMORIGIN] = val;
264 get: function(node, style) {
265 return Y_DOM[GET_COMPUTED_STYLE](node, TRANSFORMORIGIN);
273 }, '3.13.0', {"requires": ["dom-base", "color-base"]});