3
Copyright 2011 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];
230
set: function(node, val, style) {
232
styleObj = _getStyleObj(node),
233
currentFilter = styleObj[FILTER];
235
style = style || node.style;
236
if (val === '') { // normalize inline style behavior
237
current = (OPACITY in styleObj) ? styleObj[OPACITY] : 1; // revert to original opacity
241
if (typeof currentFilter == 'string') { // in case not appended
242
style[FILTER] = currentFilter.replace(/alpha([^)]*\))/gi, '') +
243
((val < 1) ? 'alpha(' + OPACITY + '=' + val * 100 + ')' : '');
245
if (!style[FILTER]) {
246
style.removeAttribute(FILTER);
249
if (!styleObj[HAS_LAYOUT]) {
250
style.zoom = 1; // needs layout
258
Y.config.doc.createElement('div').style.height = '-1px';
259
} catch(e) { // IE throws error on invalid style set; trap common cases
260
Y.DOM.CUSTOM_STYLES.height = {
261
set: function(node, val, style) {
262
var floatVal = parseFloat(val);
263
if (floatVal >= 0 || val === 'auto' || val === '') {
270
Y.DOM.CUSTOM_STYLES.width = {
271
set: function(node, val, style) {
272
var floatVal = parseFloat(val);
273
if (floatVal >= 0 || val === 'auto' || val === '') {
281
if (!testFeature('style', 'computedStyle')) {
282
// TODO: top, right, bottom, left
283
IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
285
IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor;
287
IEComputed[BORDER_WIDTH] = IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
288
IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
289
ComputedStyle.getBorderWidth;
291
IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
292
IEComputed.marginLeft = ComputedStyle.getMargin;
294
IEComputed.visibility = ComputedStyle.getVisibility;
295
IEComputed.borderColor = IEComputed.borderTopColor =
296
IEComputed.borderRightColor = IEComputed.borderBottomColor =
297
IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
299
Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get;
301
Y.namespace('DOM.IE');
302
Y.DOM.IE.COMPUTED = IEComputed;
303
Y.DOM.IE.ComputedStyle = ComputedStyle;
309
}, '3.4.1' ,{requires:['dom-style']});