Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / dom-style-ie / dom-style-ie-debug.js
blobcc11b77c6fcc7147a941a18b55c651f406374734
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('dom-style-ie', function(Y) {
9 (function(Y) {
10 var HAS_LAYOUT = 'hasLayout',
11     PX = 'px',
12     FILTER = 'filter',
13     FILTERS = 'filters',
14     OPACITY = 'opacity',
15     AUTO = 'auto',
17     BORDER_WIDTH = 'borderWidth',
18     BORDER_TOP_WIDTH = 'borderTopWidth',
19     BORDER_RIGHT_WIDTH = 'borderRightWidth',
20     BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
21     BORDER_LEFT_WIDTH = 'borderLeftWidth',
22     WIDTH = 'width',
23     HEIGHT = 'height',
24     TRANSPARENT = 'transparent',
25     VISIBLE = 'visible',
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;
40     },
42     ComputedStyle = {
43         CUSTOM_STYLES: {},
45         get: function(el, property) {
46             var value = '',
47                 current;
49             if (el) {
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
55                     value = current;
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;
60                 } else {
61                     value = current;
62                 }
63             }
65             return value;
66         },
68         sizeOffsets: {
69             width: ['Left', 'Right'],
70             height: ['Top', 'Bottom'],
71             top: ['Top'],
72             bottom: ['Bottom']
73         },
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,
82                 value = '';
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') {
92                     if (sizeOffsets[0]) {
93                         value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]);
94                         value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1);
95                     }
97                     if (sizeOffsets[1]) {
98                         value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]);
99                         value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1);
100                     }
101                 }
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;
107                 }
108                 value = el.style[pixel];
109                 
110             }
111             return value + PX;
112         },
114         borderMap: {
115             thin: (isIE8) ? '1px' : '2px',
116             medium: (isIE8) ? '3px': '4px', 
117             thick: (isIE8) ? '5px' : '6px'
118         },
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")
129                     current = 0;
130                 }
131             }
132             return (omitUnit) ? parseFloat(current) : current;
133         },
135         getPixel: function(node, att) {
136             // use pixelRight to convert to px
137             var val = null,
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
146             return val;
147         },
149         getMargin: function(node, att) {
150             var val,
151                 style = _getStyleObj(node);
153             if (style[att] == AUTO) {
154                 val = 0;
155             } else {
156                 val = ComputedStyle.getPixel(node, att);
157             }
158             return val + PX;
159         },
161         getVisibility: function(node, att) {
162             var current;
163             while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
164                 node = node.parentNode;
165             }
166             return (current) ? current[att] : VISIBLE;
167         },
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) {
176                         node = parent;
177                         return true;
178                     }
179                 });
180             }
182             return Y.Color.toRGB(current);
183         },
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));
189         }
190     },
192     //fontSize: getPixelFont,
193     IEComputed = {};
195 addFeature('style', 'computedStyle', {
196     test: function() {
197         return 'getComputedStyle' in Y.config.win;
198     }
201 addFeature('style', 'opacity', {
202     test: function() {
203         return 'opacity' in documentElement.style;
204     }
207 addFeature('style', 'filter', {
208     test: function() {
209         return 'filters' in documentElement;
210     }
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) {
217             var val = 100;
218             try { // will error if no DXImageTransform
219                 val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
221             } catch(e) {
222                 try { // make sure its in the document
223                     val = node[FILTERS]('alpha')[OPACITY];
224                 } catch(err) {
225                     Y.log('getStyle: IE opacity filter not found; returning 1', 'warn', 'dom-style');
226                 }
227             }
228             return val / 100;
229         },
231         set: function(node, val, style) {
232             var current,
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
239                 val = current;
240             }
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);
248                 }
250                 if (!styleObj[HAS_LAYOUT]) {
251                     style.zoom = 1; // needs layout 
252                 }
253             }
254         }
255     };
258 try {
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 === '') {
265                 style.height = val;
266             } else {
267                 Y.log('invalid style value for height: ' + val, 'warn', 'dom-style');
268             }
269         }
270     };
272     Y.DOM.CUSTOM_STYLES.width = {
273         set: function(node, val, style) {
274             var floatVal = parseFloat(val);
275             if (floatVal >= 0 || val === 'auto' || val === '') {
276                 style.width = val;
277             } else {
278                 Y.log('invalid style value for width: ' + val, 'warn', 'dom-style');
279             }
280         }
281     };
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;
309 })(Y);
312 }, '3.5.0' ,{requires:['dom-style']});