2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
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',
28 DOCUMENT = Y.config.doc,
29 UNDEFINED = undefined,
40 * Sets a style property for a given element.
42 * @param {HTMLElement} An HTMLElement to apply the style to.
43 * @param {String} att The style property to set.
44 * @param {String|Number} val The value.
46 setStyle: function(node, att, val, style) {
47 style = style || node.style;
48 var CUSTOM_STYLES = Y.DOM.CUSTOM_STYLES;
52 val = ''; // normalize for unsetting
54 if (att in CUSTOM_STYLES) {
55 if (CUSTOM_STYLES[att].set) {
56 CUSTOM_STYLES[att].set(node, val, style);
57 return; // NOTE: return
58 } else if (typeof CUSTOM_STYLES[att] === 'string') {
59 att = CUSTOM_STYLES[att];
67 * Returns the current style value for the given property.
69 * @param {HTMLElement} An HTMLElement to get the style from.
70 * @param {String} att The style property to get.
72 getStyle: function(node, att) {
73 var style = node[STYLE],
74 CUSTOM_STYLES = Y.DOM.CUSTOM_STYLES,
78 if (att in CUSTOM_STYLES) {
79 if (CUSTOM_STYLES[att].get) {
80 return CUSTOM_STYLES[att].get(node, att, style); // NOTE: return
81 } else if (typeof CUSTOM_STYLES[att] === 'string') {
82 att = CUSTOM_STYLES[att];
86 if (val === '') { // TODO: is empty string sufficient?
87 val = Y.DOM[GET_COMPUTED_STYLE](node, att);
95 * Sets multiple style properties.
97 * @param {HTMLElement} node An HTMLElement to apply the styles to.
98 * @param {Object} hash An object literal of property:value pairs.
100 setStyles: function(node, hash) {
101 var style = node.style;
102 Y.each(hash, function(v, n) {
103 Y.DOM.setStyle(node, n, v, style);
108 * Returns the computed style for the given node.
109 * @method getComputedStyle
110 * @param {HTMLElement} An HTMLElement to get the style from.
111 * @param {String} att The style property to get.
112 * @return {String} The computed value of the style property.
114 getComputedStyle: function(node, att) {
116 doc = node[OWNER_DOCUMENT];
119 val = doc[DEFAULT_VIEW][GET_COMPUTED_STYLE](node, null)[att];
125 // normalize reserved word float alternatives ("cssFloat" or "styleFloat")
126 if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][CSS_FLOAT] !== UNDEFINED) {
127 Y.DOM.CUSTOM_STYLES[FLOAT] = CSS_FLOAT;
128 } else if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][STYLE_FLOAT] !== UNDEFINED) {
129 Y.DOM.CUSTOM_STYLES[FLOAT] = STYLE_FLOAT;
132 // fix opera computedStyle default color unit (convert to rgb)
134 Y.DOM[GET_COMPUTED_STYLE] = function(node, att) {
135 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
136 val = view[GET_COMPUTED_STYLE](node, '')[att];
138 if (re_color.test(att)) {
139 val = Y.Color.toRGB(val);
147 // safari converts transparent to rgba(), others use "transparent"
149 Y.DOM[GET_COMPUTED_STYLE] = function(node, att) {
150 var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
151 val = view[GET_COMPUTED_STYLE](node, '')[att];
153 if (val === 'rgba(0, 0, 0, 0)') {
163 var PARSE_INT = parseInt,
186 re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
187 re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
188 re_hex3: /([0-9A-F])/gi,
190 toRGB: function(val) {
191 if (!Y.Color.re_RGB.test(val)) {
192 val = Y.Color.toHex(val);
195 if(Y.Color.re_hex.exec(val)) {
197 PARSE_INT(RE.$1, 16),
198 PARSE_INT(RE.$2, 16),
205 toHex: function(val) {
206 val = Y.Color.KEYWORDS[val] || val;
207 if (Y.Color.re_RGB.exec(val)) {
209 Number(RE.$1).toString(16),
210 Number(RE.$2).toString(16),
211 Number(RE.$3).toString(16)
214 for (var i = 0; i < val.length; i++) {
215 if (val[i].length < 2) {
216 val[i] = val[i].replace(Y.Color.re_hex3, '$1$1');
220 val = '#' + val.join('');
223 if (val.length < 6) {
224 val = val.replace(Y.Color.re_hex3, '$1$1');
227 if (val !== 'transparent' && val.indexOf('#') < 0) {
231 return val.toLowerCase();
237 var HAS_LAYOUT = 'hasLayout',
244 BORDER_WIDTH = 'borderWidth',
245 BORDER_TOP_WIDTH = 'borderTopWidth',
246 BORDER_RIGHT_WIDTH = 'borderRightWidth',
247 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
248 BORDER_LEFT_WIDTH = 'borderLeftWidth',
251 TRANSPARENT = 'transparent',
253 GET_COMPUTED_STYLE = 'getComputedStyle',
254 UNDEFINED = undefined,
255 documentElement = document.documentElement,
257 // TODO: unit-less lineHeight (e.g. 1.22)
258 re_unit = /^(\d[.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz|%){1}?/i,
260 _getStyleObj = function(node) {
261 return node.currentStyle || node.style;
267 get: function(el, property) {
272 current = _getStyleObj(el)[property];
274 if (property === OPACITY && Y.DOM.CUSTOM_STYLES[OPACITY]) {
275 value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el);
276 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
278 } else if (Y.DOM.IE.COMPUTED[property]) { // use compute function
279 value = Y.DOM.IE.COMPUTED[property](el, property);
280 } else if (re_unit.test(current)) { // convert to pixel
281 value = ComputedStyle.getPixel(el, property) + PX;
291 width: ['Left', 'Right'],
292 height: ['Top', 'Bottom'],
297 getOffset: function(el, prop) {
298 var current = _getStyleObj(el)[prop], // value of "width", "top", etc.
299 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
300 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
301 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
302 sizeOffsets = ComputedStyle.sizeOffsets[prop],
305 // IE pixelWidth incorrect for percent
306 // manually compute by subtracting padding and border from offset size
307 // NOTE: clientWidth/Height (size minus border) is 0 when current === AUTO so offsetHeight is used
308 // reverting to auto from auto causes position stacking issues (old impl)
309 if (current === AUTO || current.indexOf('%') > -1) {
310 value = el['offset' + capped];
312 if (sizeOffsets[0]) {
313 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]);
314 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1);
317 if (sizeOffsets[1]) {
318 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]);
319 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1);
322 } else { // use style.pixelWidth, etc. to convert to pixels
323 // need to map style.width to currentStyle (no currentStyle.pixelWidth)
324 if (!el.style[pixel] && !el.style[prop]) {
325 el.style[prop] = current;
327 value = el.style[pixel];
339 getBorderWidth: function(el, property, omitUnit) {
340 var unit = omitUnit ? '' : PX,
341 current = el.currentStyle[property];
343 if (current.indexOf(PX) < 0) { // look up keywords
344 if (ComputedStyle.borderMap[current]) {
345 current = ComputedStyle.borderMap[current];
349 return (omitUnit) ? parseFloat(current) : current;
352 getPixel: function(node, att) {
353 // use pixelRight to convert to px
355 style = _getStyleObj(node),
356 styleRight = style.right,
357 current = style[att];
359 node.style.right = current;
360 val = node.style.pixelRight;
361 node.style.right = styleRight; // revert
366 getMargin: function(node, att) {
368 style = _getStyleObj(node);
370 if (style[att] == AUTO) {
373 val = ComputedStyle.getPixel(node, att);
378 getVisibility: function(node, att) {
380 while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
381 node = node.parentNode;
383 return (current) ? current[att] : VISIBLE;
386 getColor: function(node, att) {
387 var current = _getStyleObj(node)[att];
389 if (!current || current === TRANSPARENT) {
390 Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) {
391 current = _getStyleObj(parent)[att];
392 if (current && current !== TRANSPARENT) {
399 return Y.Color.toRGB(current);
402 getBorderColor: function(node, att) {
403 var current = _getStyleObj(node),
404 val = current[att] || current.color;
405 return Y.Color.toRGB(Y.Color.toHex(val));
409 //fontSize: getPixelFont,
412 // use alpha filter for IE opacity
414 if (documentElement.style[OPACITY] === UNDEFINED &&
415 documentElement[FILTERS]) {
416 Y.DOM.CUSTOM_STYLES[OPACITY] = {
417 get: function(node) {
419 try { // will error if no DXImageTransform
420 val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
423 try { // make sure its in the document
424 val = node[FILTERS]('alpha')[OPACITY];
431 set: function(node, val, style) {
435 if (val === '') { // normalize inline style behavior
436 styleObj = _getStyleObj(node);
437 current = (OPACITY in styleObj) ? styleObj[OPACITY] : 1; // revert to original opacity
441 if (typeof style[FILTER] == 'string') { // in case not appended
442 style[FILTER] = 'alpha(' + OPACITY + '=' + val * 100 + ')';
444 if (!node.currentStyle || !node.currentStyle[HAS_LAYOUT]) {
445 style.zoom = 1; // needs layout
455 document.createElement('div').style.height = '-1px';
456 } catch(e) { // IE throws error on invalid style set; trap common cases
457 Y.DOM.CUSTOM_STYLES.height = {
458 set: function(node, val, style) {
459 var floatVal = parseFloat(val);
460 if (isNaN(floatVal) || floatVal >= 0) {
467 Y.DOM.CUSTOM_STYLES.width = {
468 set: function(node, val, style) {
469 var floatVal = parseFloat(val);
470 if (isNaN(floatVal) || floatVal >= 0) {
478 // TODO: top, right, bottom, left
479 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
481 IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor;
483 IEComputed[BORDER_WIDTH] = IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
484 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
485 ComputedStyle.getBorderWidth;
487 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
488 IEComputed.marginLeft = ComputedStyle.getMargin;
490 IEComputed.visibility = ComputedStyle.getVisibility;
491 IEComputed.borderColor = IEComputed.borderTopColor =
492 IEComputed.borderRightColor = IEComputed.borderBottomColor =
493 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
495 if (!Y.config.win[GET_COMPUTED_STYLE]) {
496 Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get;
499 Y.namespace('DOM.IE');
500 Y.DOM.IE.COMPUTED = IEComputed;
501 Y.DOM.IE.ComputedStyle = ComputedStyle;
506 }, '3.0.0' ,{requires:['dom-base']});