|
602
|
1 |
YUI.add('dom-style-ie', function (Y, NAME) { |
|
|
2 |
|
|
|
3 |
(function(Y) { |
|
|
4 |
var HAS_LAYOUT = 'hasLayout', |
|
|
5 |
PX = 'px', |
|
|
6 |
FILTER = 'filter', |
|
|
7 |
FILTERS = 'filters', |
|
|
8 |
OPACITY = 'opacity', |
|
|
9 |
AUTO = 'auto', |
|
|
10 |
|
|
|
11 |
BORDER_WIDTH = 'borderWidth', |
|
|
12 |
BORDER_TOP_WIDTH = 'borderTopWidth', |
|
|
13 |
BORDER_RIGHT_WIDTH = 'borderRightWidth', |
|
|
14 |
BORDER_BOTTOM_WIDTH = 'borderBottomWidth', |
|
|
15 |
BORDER_LEFT_WIDTH = 'borderLeftWidth', |
|
|
16 |
WIDTH = 'width', |
|
|
17 |
HEIGHT = 'height', |
|
|
18 |
TRANSPARENT = 'transparent', |
|
|
19 |
VISIBLE = 'visible', |
|
|
20 |
GET_COMPUTED_STYLE = 'getComputedStyle', |
|
|
21 |
UNDEFINED = undefined, |
|
|
22 |
documentElement = Y.config.doc.documentElement, |
|
|
23 |
|
|
|
24 |
testFeature = Y.Features.test, |
|
|
25 |
addFeature = Y.Features.add, |
|
|
26 |
|
|
|
27 |
// TODO: unit-less lineHeight (e.g. 1.22) |
|
|
28 |
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, |
|
|
29 |
|
|
|
30 |
isIE8 = (Y.UA.ie >= 8), |
|
|
31 |
|
|
|
32 |
_getStyleObj = function(node) { |
|
|
33 |
return node.currentStyle || node.style; |
|
|
34 |
}, |
|
|
35 |
|
|
|
36 |
ComputedStyle = { |
|
|
37 |
CUSTOM_STYLES: {}, |
|
|
38 |
|
|
|
39 |
get: function(el, property) { |
|
|
40 |
var value = '', |
|
|
41 |
current; |
|
|
42 |
|
|
|
43 |
if (el) { |
|
|
44 |
current = _getStyleObj(el)[property]; |
|
|
45 |
|
|
|
46 |
if (property === OPACITY && Y.DOM.CUSTOM_STYLES[OPACITY]) { |
|
|
47 |
value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el); |
|
|
48 |
} else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert |
|
|
49 |
value = current; |
|
|
50 |
} else if (Y.DOM.IE.COMPUTED[property]) { // use compute function |
|
|
51 |
value = Y.DOM.IE.COMPUTED[property](el, property); |
|
|
52 |
} else if (re_unit.test(current)) { // convert to pixel |
|
|
53 |
value = ComputedStyle.getPixel(el, property) + PX; |
|
|
54 |
} else { |
|
|
55 |
value = current; |
|
|
56 |
} |
|
|
57 |
} |
|
|
58 |
|
|
|
59 |
return value; |
|
|
60 |
}, |
|
|
61 |
|
|
|
62 |
sizeOffsets: { |
|
|
63 |
width: ['Left', 'Right'], |
|
|
64 |
height: ['Top', 'Bottom'], |
|
|
65 |
top: ['Top'], |
|
|
66 |
bottom: ['Bottom'] |
|
|
67 |
}, |
|
|
68 |
|
|
|
69 |
getOffset: function(el, prop) { |
|
|
70 |
var current = _getStyleObj(el)[prop], // value of "width", "top", etc. |
|
|
71 |
capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc. |
|
|
72 |
offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc. |
|
|
73 |
pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc. |
|
|
74 |
sizeOffsets = ComputedStyle.sizeOffsets[prop], |
|
|
75 |
mode = el.ownerDocument.compatMode, |
|
|
76 |
value = ''; |
|
|
77 |
|
|
|
78 |
// IE pixelWidth incorrect for percent |
|
|
79 |
// manually compute by subtracting padding and border from offset size |
|
|
80 |
// NOTE: clientWidth/Height (size minus border) is 0 when current === AUTO so offsetHeight is used |
|
|
81 |
// reverting to auto from auto causes position stacking issues (old impl) |
|
|
82 |
if (current === AUTO || current.indexOf('%') > -1) { |
|
|
83 |
value = el['offset' + capped]; |
|
|
84 |
|
|
|
85 |
if (mode !== 'BackCompat') { |
|
|
86 |
if (sizeOffsets[0]) { |
|
|
87 |
value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]); |
|
|
88 |
value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
if (sizeOffsets[1]) { |
|
|
92 |
value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]); |
|
|
93 |
value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1); |
|
|
94 |
} |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
} else { // use style.pixelWidth, etc. to convert to pixels |
|
|
98 |
// need to map style.width to currentStyle (no currentStyle.pixelWidth) |
|
|
99 |
if (!el.style[pixel] && !el.style[prop]) { |
|
|
100 |
el.style[prop] = current; |
|
|
101 |
} |
|
|
102 |
value = el.style[pixel]; |
|
|
103 |
|
|
|
104 |
} |
|
|
105 |
return value + PX; |
|
|
106 |
}, |
|
|
107 |
|
|
|
108 |
borderMap: { |
|
|
109 |
thin: (isIE8) ? '1px' : '2px', |
|
|
110 |
medium: (isIE8) ? '3px': '4px', |
|
|
111 |
thick: (isIE8) ? '5px' : '6px' |
|
|
112 |
}, |
|
|
113 |
|
|
|
114 |
getBorderWidth: function(el, property, omitUnit) { |
|
|
115 |
var unit = omitUnit ? '' : PX, |
|
|
116 |
current = el.currentStyle[property]; |
|
|
117 |
|
|
|
118 |
if (current.indexOf(PX) < 0) { // look up keywords if a border exists |
|
|
119 |
if (ComputedStyle.borderMap[current] && |
|
|
120 |
el.currentStyle.borderStyle !== 'none') { |
|
|
121 |
current = ComputedStyle.borderMap[current]; |
|
|
122 |
} else { // otherwise no border (default is "medium") |
|
|
123 |
current = 0; |
|
|
124 |
} |
|
|
125 |
} |
|
|
126 |
return (omitUnit) ? parseFloat(current) : current; |
|
|
127 |
}, |
|
|
128 |
|
|
|
129 |
getPixel: function(node, att) { |
|
|
130 |
// use pixelRight to convert to px |
|
|
131 |
var val = null, |
|
|
132 |
style = _getStyleObj(node), |
|
|
133 |
styleRight = style.right, |
|
|
134 |
current = style[att]; |
|
|
135 |
|
|
|
136 |
node.style.right = current; |
|
|
137 |
val = node.style.pixelRight; |
|
|
138 |
node.style.right = styleRight; // revert |
|
|
139 |
|
|
|
140 |
return val; |
|
|
141 |
}, |
|
|
142 |
|
|
|
143 |
getMargin: function(node, att) { |
|
|
144 |
var val, |
|
|
145 |
style = _getStyleObj(node); |
|
|
146 |
|
|
|
147 |
if (style[att] == AUTO) { |
|
|
148 |
val = 0; |
|
|
149 |
} else { |
|
|
150 |
val = ComputedStyle.getPixel(node, att); |
|
|
151 |
} |
|
|
152 |
return val + PX; |
|
|
153 |
}, |
|
|
154 |
|
|
|
155 |
getVisibility: function(node, att) { |
|
|
156 |
var current; |
|
|
157 |
while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test |
|
|
158 |
node = node.parentNode; |
|
|
159 |
} |
|
|
160 |
return (current) ? current[att] : VISIBLE; |
|
|
161 |
}, |
|
|
162 |
|
|
|
163 |
getColor: function(node, att) { |
|
|
164 |
var current = _getStyleObj(node)[att]; |
|
|
165 |
|
|
|
166 |
if (!current || current === TRANSPARENT) { |
|
|
167 |
Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) { |
|
|
168 |
current = _getStyleObj(parent)[att]; |
|
|
169 |
if (current && current !== TRANSPARENT) { |
|
|
170 |
node = parent; |
|
|
171 |
return true; |
|
|
172 |
} |
|
|
173 |
}); |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
return Y.Color.toRGB(current); |
|
|
177 |
}, |
|
|
178 |
|
|
|
179 |
getBorderColor: function(node, att) { |
|
|
180 |
var current = _getStyleObj(node), |
|
|
181 |
val = current[att] || current.color; |
|
|
182 |
return Y.Color.toRGB(Y.Color.toHex(val)); |
|
|
183 |
} |
|
|
184 |
}, |
|
|
185 |
|
|
|
186 |
//fontSize: getPixelFont, |
|
|
187 |
IEComputed = {}; |
|
|
188 |
|
|
|
189 |
addFeature('style', 'computedStyle', { |
|
|
190 |
test: function() { |
|
|
191 |
return 'getComputedStyle' in Y.config.win; |
|
|
192 |
} |
|
|
193 |
}); |
|
|
194 |
|
|
|
195 |
addFeature('style', 'opacity', { |
|
|
196 |
test: function() { |
|
|
197 |
return 'opacity' in documentElement.style; |
|
|
198 |
} |
|
|
199 |
}); |
|
|
200 |
|
|
|
201 |
addFeature('style', 'filter', { |
|
|
202 |
test: function() { |
|
|
203 |
return 'filters' in documentElement; |
|
|
204 |
} |
|
|
205 |
}); |
|
|
206 |
|
|
|
207 |
// use alpha filter for IE opacity |
|
|
208 |
if (!testFeature('style', 'opacity') && testFeature('style', 'filter')) { |
|
|
209 |
Y.DOM.CUSTOM_STYLES[OPACITY] = { |
|
|
210 |
get: function(node) { |
|
|
211 |
var val = 100; |
|
|
212 |
try { // will error if no DXImageTransform |
|
|
213 |
val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY]; |
|
|
214 |
|
|
|
215 |
} catch(e) { |
|
|
216 |
try { // make sure its in the document |
|
|
217 |
val = node[FILTERS]('alpha')[OPACITY]; |
|
|
218 |
} catch(err) { |
|
|
219 |
} |
|
|
220 |
} |
|
|
221 |
return val / 100; |
|
|
222 |
}, |
|
|
223 |
|
|
|
224 |
set: function(node, val, style) { |
|
|
225 |
var current, |
|
|
226 |
styleObj = _getStyleObj(node), |
|
|
227 |
currentFilter = styleObj[FILTER]; |
|
|
228 |
|
|
|
229 |
style = style || node.style; |
|
|
230 |
if (val === '') { // normalize inline style behavior |
|
|
231 |
current = (OPACITY in styleObj) ? styleObj[OPACITY] : 1; // revert to original opacity |
|
|
232 |
val = current; |
|
|
233 |
} |
|
|
234 |
|
|
|
235 |
if (typeof currentFilter == 'string') { // in case not appended |
|
|
236 |
style[FILTER] = currentFilter.replace(/alpha([^)]*\))/gi, '') + |
|
|
237 |
((val <= 1) ? 'alpha(' + OPACITY + '=' + val * 100 + ')' : ''); |
|
|
238 |
|
|
|
239 |
if (!style[FILTER]) { |
|
|
240 |
style.removeAttribute(FILTER); |
|
|
241 |
} |
|
|
242 |
|
|
|
243 |
if (!styleObj[HAS_LAYOUT]) { |
|
|
244 |
style.zoom = 1; // needs layout |
|
|
245 |
} |
|
|
246 |
} |
|
|
247 |
} |
|
|
248 |
}; |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
try { |
|
|
252 |
Y.config.doc.createElement('div').style.height = '-1px'; |
|
|
253 |
} catch(e) { // IE throws error on invalid style set; trap common cases |
|
|
254 |
Y.DOM.CUSTOM_STYLES.height = { |
|
|
255 |
set: function(node, val, style) { |
|
|
256 |
var floatVal = parseFloat(val); |
|
|
257 |
if (floatVal >= 0 || val === 'auto' || val === '') { |
|
|
258 |
style.height = val; |
|
|
259 |
} else { |
|
|
260 |
} |
|
|
261 |
} |
|
|
262 |
}; |
|
|
263 |
|
|
|
264 |
Y.DOM.CUSTOM_STYLES.width = { |
|
|
265 |
set: function(node, val, style) { |
|
|
266 |
var floatVal = parseFloat(val); |
|
|
267 |
if (floatVal >= 0 || val === 'auto' || val === '') { |
|
|
268 |
style.width = val; |
|
|
269 |
} else { |
|
|
270 |
} |
|
|
271 |
} |
|
|
272 |
}; |
|
|
273 |
} |
|
|
274 |
|
|
|
275 |
if (!testFeature('style', 'computedStyle')) { |
|
|
276 |
// TODO: top, right, bottom, left |
|
|
277 |
IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset; |
|
|
278 |
|
|
|
279 |
IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor; |
|
|
280 |
|
|
|
281 |
IEComputed[BORDER_WIDTH] = IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] = |
|
|
282 |
IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] = |
|
|
283 |
ComputedStyle.getBorderWidth; |
|
|
284 |
|
|
|
285 |
IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom = |
|
|
286 |
IEComputed.marginLeft = ComputedStyle.getMargin; |
|
|
287 |
|
|
|
288 |
IEComputed.visibility = ComputedStyle.getVisibility; |
|
|
289 |
IEComputed.borderColor = IEComputed.borderTopColor = |
|
|
290 |
IEComputed.borderRightColor = IEComputed.borderBottomColor = |
|
|
291 |
IEComputed.borderLeftColor = ComputedStyle.getBorderColor; |
|
|
292 |
|
|
|
293 |
Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get; |
|
|
294 |
|
|
|
295 |
Y.namespace('DOM.IE'); |
|
|
296 |
Y.DOM.IE.COMPUTED = IEComputed; |
|
|
297 |
Y.DOM.IE.ComputedStyle = ComputedStyle; |
|
|
298 |
} |
|
|
299 |
|
|
|
300 |
})(Y); |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
}, '@VERSION@', {"requires": ["dom-style"]}); |