3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('dom-style-ie', function(Y) {
10 var HAS_LAYOUT = 'hasLayout',
17 BORDER_WIDTH = 'borderWidth',
18 BORDER_TOP_WIDTH = 'borderTopWidth',
19 BORDER_RIGHT_WIDTH = 'borderRightWidth',
20 BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
21 BORDER_LEFT_WIDTH = 'borderLeftWidth',
24 TRANSPARENT = 'transparent',
26 GET_COMPUTED_STYLE = 'getComputedStyle',
27 UNDEFINED = undefined,
28 documentElement = Y.config.doc.documentElement,
30 testFeature = Y.Features.test,
31 addFeature = Y.Features.add,
33 // TODO: unit-less lineHeight (e.g. 1.22)
34 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,
36 isIE8 = (Y.UA.ie >= 8),
38 _getStyleObj = function(node) {
39 return node.currentStyle || node.style;
45 get: function(el, property) {
50 current = _getStyleObj(el)[property];
52 if (property === OPACITY && Y.DOM.CUSTOM_STYLES[OPACITY]) {
53 value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el);
54 } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
56 } else if (Y.DOM.IE.COMPUTED[property]) { // use compute function
57 value = Y.DOM.IE.COMPUTED[property](el, property);
58 } else if (re_unit.test(current)) { // convert to pixel
59 value = ComputedStyle.getPixel(el, property) + PX;
69 width: ['Left', 'Right'],
70 height: ['Top', 'Bottom'],
75 getOffset: function(el, prop) {
76 var current = _getStyleObj(el)[prop], // value of "width", "top", etc.
77 capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
78 offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
79 pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
80 sizeOffsets = ComputedStyle.sizeOffsets[prop],
81 mode = el.ownerDocument.compatMode,
84 // IE pixelWidth incorrect for percent
85 // manually compute by subtracting padding and border from offset size
86 // NOTE: clientWidth/Height (size minus border) is 0 when current === AUTO so offsetHeight is used
87 // reverting to auto from auto causes position stacking issues (old impl)
88 if (current === AUTO || current.indexOf('%') > -1) {
89 value = el['offset' + capped];
91 if (mode !== 'BackCompat') {
93 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]);
94 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1);
98 value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]);
99 value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1);
103 } else { // use style.pixelWidth, etc. to convert to pixels
104 // need to map style.width to currentStyle (no currentStyle.pixelWidth)
105 if (!el.style[pixel] && !el.style[prop]) {
106 el.style[prop] = current;
108 value = el.style[pixel];
115 thin: (isIE8) ? '1px' : '2px',
116 medium: (isIE8) ? '3px': '4px',
117 thick: (isIE8) ? '5px' : '6px'
120 getBorderWidth: function(el, property, omitUnit) {
121 var unit = omitUnit ? '' : PX,
122 current = el.currentStyle[property];
124 if (current.indexOf(PX) < 0) { // look up keywords if a border exists
125 if (ComputedStyle.borderMap[current] &&
126 el.currentStyle.borderStyle !== 'none') {
127 current = ComputedStyle.borderMap[current];
128 } else { // otherwise no border (default is "medium")
132 return (omitUnit) ? parseFloat(current) : current;
135 getPixel: function(node, att) {
136 // use pixelRight to convert to px
138 style = _getStyleObj(node),
139 styleRight = style.right,
140 current = style[att];
142 node.style.right = current;
143 val = node.style.pixelRight;
144 node.style.right = styleRight; // revert
149 getMargin: function(node, att) {
151 style = _getStyleObj(node);
153 if (style[att] == AUTO) {
156 val = ComputedStyle.getPixel(node, att);
161 getVisibility: function(node, att) {
163 while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
164 node = node.parentNode;
166 return (current) ? current[att] : VISIBLE;
169 getColor: function(node, att) {
170 var current = _getStyleObj(node)[att];
172 if (!current || current === TRANSPARENT) {
173 Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) {
174 current = _getStyleObj(parent)[att];
175 if (current && current !== TRANSPARENT) {
182 return Y.Color.toRGB(current);
185 getBorderColor: function(node, att) {
186 var current = _getStyleObj(node),
187 val = current[att] || current.color;
188 return Y.Color.toRGB(Y.Color.toHex(val));
192 //fontSize: getPixelFont,
195 addFeature('style', 'computedStyle', {
197 return 'getComputedStyle' in Y.config.win;
201 addFeature('style', 'opacity', {
203 return 'opacity' in documentElement.style;
207 addFeature('style', 'filter', {
209 return 'filters' in documentElement;
213 // use alpha filter for IE opacity
214 if (!testFeature('style', 'opacity') && testFeature('style', 'filter')) {
215 Y.DOM.CUSTOM_STYLES[OPACITY] = {
216 get: function(node) {
218 try { // will error if no DXImageTransform
219 val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
222 try { // make sure its in the document
223 val = node[FILTERS]('alpha')[OPACITY];
225 Y.log('getStyle: IE opacity filter not found; returning 1', 'warn', 'dom-style');
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 === '') {
267 Y.log('invalid style value for height: ' + val, 'warn', 'dom-style');
272 Y.DOM.CUSTOM_STYLES.width = {
273 set: function(node, val, style) {
274 var floatVal = parseFloat(val);
275 if (floatVal >= 0 || val === 'auto' || val === '') {
278 Y.log('invalid style value for width: ' + val, 'warn', 'dom-style');
284 if (!testFeature('style', 'computedStyle')) {
285 // TODO: top, right, bottom, left
286 IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
288 IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor;
290 IEComputed[BORDER_WIDTH] = IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
291 IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
292 ComputedStyle.getBorderWidth;
294 IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
295 IEComputed.marginLeft = ComputedStyle.getMargin;
297 IEComputed.visibility = ComputedStyle.getVisibility;
298 IEComputed.borderColor = IEComputed.borderTopColor =
299 IEComputed.borderRightColor = IEComputed.borderBottomColor =
300 IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
302 Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get;
304 Y.namespace('DOM.IE');
305 Y.DOM.IE.COMPUTED = IEComputed;
306 Y.DOM.IE.ComputedStyle = ComputedStyle;
312 }, '3.5.0' ,{requires:['dom-style']});