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-ie', function(Y) {
11 var HAS_LAYOUT = 'hasLayout',
18 BORDER_WIDTH = 'borderWidth',
19 BORDER_TOP_WIDTH = 'borderTopWidth',
20 BORDER_RIGHT_WIDTH = 'borderRightWidth',
21 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
22 BORDER_LEFT_WIDTH = 'borderLeftWidth',
25 TRANSPARENT = 'transparent',
27 GET_COMPUTED_STYLE = 'getComputedStyle',
28 UNDEFINED = undefined,
29 documentElement = Y.config.doc.documentElement,
31 testFeature = Y.Features.test,
32 addFeature = Y.Features.add,
34 // TODO: unit-less lineHeight (e.g. 1.22)
35 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,
37 isIE8 = (Y.UA.ie >= 8),
39 _getStyleObj = function(node) {
40 return node.currentStyle || node.style;
46 get: function(el, property) {
51 current = _getStyleObj(el)[property];
53 if (property === OPACITY && Y.DOM.CUSTOM_STYLES[OPACITY]) {
54 value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el);
55 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
57 } else if (Y.DOM.IE.COMPUTED[property]) { // use compute function
58 value = Y.DOM.IE.COMPUTED[property](el, property);
59 } else if (re_unit.test(current)) { // convert to pixel
60 value = ComputedStyle.getPixel(el, property) + PX;
70 width: ['Left', 'Right'],
71 height: ['Top', 'Bottom'],
76 getOffset: function(el, prop) {
77 var current = _getStyleObj(el)[prop], // value of "width", "top", etc.
78 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
79 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
80 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
81 sizeOffsets = ComputedStyle.sizeOffsets[prop],
82 mode = el.ownerDocument.compatMode,
85 // IE pixelWidth incorrect for percent
86 // manually compute by subtracting padding and border from offset size
87 // NOTE: clientWidth/Height (size minus border) is 0 when current === AUTO so offsetHeight is used
88 // reverting to auto from auto causes position stacking issues (old impl)
89 if (current === AUTO || current.indexOf('%') > -1) {
90 value = el['offset' + capped];
92 if (mode !== 'BackCompat') {
94 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]);
95 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1);
99 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]);
100 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1);
104 } else { // use style.pixelWidth, etc. to convert to pixels
105 // need to map style.width to currentStyle (no currentStyle.pixelWidth)
106 if (!el.style[pixel] && !el.style[prop]) {
107 el.style[prop] = current;
109 value = el.style[pixel];
116 thin: (isIE8) ? '1px' : '2px',
117 medium: (isIE8) ? '3px': '4px',
118 thick: (isIE8) ? '5px' : '6px'
121 getBorderWidth: function(el, property, omitUnit) {
122 var unit = omitUnit ? '' : PX,
123 current = el.currentStyle[property];
125 if (current.indexOf(PX) < 0) { // look up keywords if a border exists
126 if (ComputedStyle.borderMap[current] &&
127 el.currentStyle.borderStyle !== 'none') {
128 current = ComputedStyle.borderMap[current];
129 } else { // otherwise no border (default is "medium")
133 return (omitUnit) ? parseFloat(current) : current;
136 getPixel: function(node, att) {
137 // use pixelRight to convert to px
139 style = _getStyleObj(node),
140 styleRight = style.right,
141 current = style[att];
143 node.style.right = current;
144 val = node.style.pixelRight;
145 node.style.right = styleRight; // revert
150 getMargin: function(node, att) {
152 style = _getStyleObj(node);
154 if (style[att] == AUTO) {
157 val = ComputedStyle.getPixel(node, att);
162 getVisibility: function(node, att) {
164 while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
165 node = node.parentNode;
167 return (current) ? current[att] : VISIBLE;
170 getColor: function(node, att) {
171 var current = _getStyleObj(node)[att];
173 if (!current || current === TRANSPARENT) {
174 Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) {
175 current = _getStyleObj(parent)[att];
176 if (current && current !== TRANSPARENT) {
183 return Y.Color.toRGB(current);
186 getBorderColor: function(node, att) {
187 var current = _getStyleObj(node),
188 val = current[att] || current.color;
189 return Y.Color.toRGB(Y.Color.toHex(val));
193 //fontSize: getPixelFont,
196 addFeature('style', 'computedStyle', {
198 return 'getComputedStyle' in Y.config.win;
202 addFeature('style', 'opacity', {
204 return 'opacity' in documentElement.style;
208 addFeature('style', 'filter', {
210 return 'filters' in documentElement;
214 // use alpha filter for IE opacity
215 if (!testFeature('style', 'opacity') && testFeature('style', 'filter')) {
216 Y.DOM.CUSTOM_STYLES[OPACITY] = {
217 get: function(node) {
219 try { // will error if no DXImageTransform
220 val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
223 try { // make sure its in the document
224 val = node[FILTERS]('alpha')[OPACITY];
231 set: function(node, val, style) {
233 styleObj = _getStyleObj(node),
234 currentFilter = styleObj[FILTER];
236 style = style || node.style;
237 if (val === '') { // normalize inline style behavior
238 current = (OPACITY in styleObj) ? styleObj[OPACITY] : 1; // revert to original opacity
242 if (typeof currentFilter == 'string') { // in case not appended
243 style[FILTER] = currentFilter.replace(/alpha([^)]*\))/gi, '') +
244 ((val < 1) ? 'alpha(' + OPACITY + '=' + val * 100 + ')' : '');
246 if (!style[FILTER]) {
247 style.removeAttribute(FILTER);
250 if (!styleObj[HAS_LAYOUT]) {
251 style.zoom = 1; // needs layout
259 Y.config.doc.createElement('div').style.height = '-1px';
260 } catch(e) { // IE throws error on invalid style set; trap common cases
261 Y.DOM.CUSTOM_STYLES.height = {
262 set: function(node, val, style) {
263 var floatVal = parseFloat(val);
264 if (floatVal >= 0 || val === 'auto' || val === '') {
271 Y.DOM.CUSTOM_STYLES.width = {
272 set: function(node, val, style) {
273 var floatVal = parseFloat(val);
274 if (floatVal >= 0 || val === 'auto' || val === '') {
282 if (!testFeature('style', 'computedStyle')) {
283 // TODO: top, right, bottom, left
284 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
286 IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor;
288 IEComputed[BORDER_WIDTH] = IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
289 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
290 ComputedStyle.getBorderWidth;
292 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
293 IEComputed.marginLeft = ComputedStyle.getMargin;
295 IEComputed.visibility = ComputedStyle.getVisibility;
296 IEComputed.borderColor = IEComputed.borderTopColor =
297 IEComputed.borderRightColor = IEComputed.borderBottomColor =
298 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
300 Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get;
302 Y.namespace('DOM.IE');
303 Y.DOM.IE.COMPUTED = IEComputed;
304 Y.DOM.IE.ComputedStyle = ComputedStyle;
310 }, '3.3.0' ,{requires:['dom-style']});