dom-ie-style.js revision 4f15171c562757e8d3e2b8882e04d6b5a2291732
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major(function(Y) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Majorvar CLIENT_TOP = 'clientTop',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major CLIENT_LEFT = 'clientLeft',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major HAS_LAYOUT = 'hasLayout',
c5aaf8bd167dc5fc4175bf9fc2d5b8067b913880Neil Madden PX = 'px',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major FILTER = 'filter',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major FILTERS = 'filters',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major OPACITY = 'opacity',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major AUTO = 'auto',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major BORDER_TOP_WIDTH = 'borderTopWidth',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major BORDER_RIGHT_WIDTH = 'borderRightWidth',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major BORDER_LEFT_WIDTH = 'borderLeftWidth',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major WIDTH = 'width',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major HEIGHT = 'height',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major TRANSPARENT = 'transparent',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major VISIBLE = 'visible',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major GET_COMPUTED_STYLE = 'getComputedStyle',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major UNDEFINED = undefined,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major documentElement = document.documentElement,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // TODO: unit-less lineHeight (e.g. 1.22)
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major re_size = /^width|height$/,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major 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,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
1f48f8236de7de97be1c6b9d06bef50b379c8801jenkins _getStyleObj = function(node) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return node.currentStyle || node.style;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major ComputedStyle = {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major CUSTOM_STYLES: {},
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
5d4589530d1353fdd627ab216a1cdcbcaf6b705ejenkins get: function(el, property) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var value = '',
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major current;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (el) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major current = _getStyleObj(el)[property];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (property === OPACITY) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = current;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else if (Y.DOM.IE.COMPUTED[property]) { // use compute function
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = Y.DOM.IE.COMPUTED[property](el, property);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else if (re_unit.test(current)) { // convert to pixel
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = ComputedStyle.getPixel(el, property) + PX;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = current;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return value;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getOffset: function(el, prop) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var current = _getStyleObj(el)[prop], // value of "width", "top", etc.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major actual,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = '';
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (current === AUTO) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major actual = el[offset]; // offsetHeight/Top etc.
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (actual === UNDEFINED) { // likely "right" or "bottom"
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = 0;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = actual;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (re_size.test(prop)) { // account for box model diff
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major el.style[prop] = actual;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (el[offset] > actual) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // the difference is padding + border (works in Standards & Quirks modes)
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = actual - (el[offset] - actual);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major el.style[prop] = AUTO; // revert to auto
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else { // convert units to px
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (current.indexOf('%') > -1) { // IE pixelWidth incorrect for percent; manually compute
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major current = el.clientWidth - // offsetWidth - borderWidth
a62d691262d0b1a55b3fc79eba377ac7bd1c0629jeff.schenk ComputedStyle.getPixel(el, 'paddingRight') -
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major ComputedStyle.getPixel(el, 'paddingLeft');
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (!el.style[pixel] && !el.style[prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major el.style[prop] = current; // no style.pixelWidth if no style.width
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = el.style[pixel];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return value + PX;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getBorderWidth: function(el, property) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // clientTop/Left = borderWidth
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var value = null;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (!el.currentStyle || !el.currentStyle[HAS_LAYOUT]) { // TODO: unset layout?
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major el.style.zoom = 1; // need layout to measure client
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major switch(property) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major case BORDER_TOP_WIDTH:
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = el[CLIENT_TOP];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major break;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major case BORDER_BOTTOM_WIDTH:
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major break;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major case BORDER_LEFT_WIDTH:
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = el[CLIENT_LEFT];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major break;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major case BORDER_RIGHT_WIDTH:
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major break;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return value + PX;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getPixel: function(node, att) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major // use pixelRight to convert to px
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var val = null,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major style = _getStyleObj(node),
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major styleRight = style.right,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major current = style[att];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major node.style.right = current;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major val = node.style.pixelRight;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major node.style.right = styleRight; // revert
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return val;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getMargin: function(node, att) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var val,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major style = _getStyleObj(node);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (style[att] == AUTO) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major val = 0;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major } else {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major val = ComputedStyle.getPixel(node, att);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return val + PX;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getVisibility: function(node, att) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var current;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major node = node.parentNode;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return (current) ? current[att] : VISIBLE;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major getColor: function(node, att) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major var current = _getStyleObj(node)[att];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (!current || current === TRANSPARENT) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major current = _getStyleObj(parent)[att];
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major if (current && current !== TRANSPARENT) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major node = parent;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return true;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major });
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return Y.Color.toRGB(current);
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
87e1cbcd02820f55e1816ee4efe9e9127be22a11James Phillpotts getBorderColor: function(node, att) {
87e1cbcd02820f55e1816ee4efe9e9127be22a11James Phillpotts var current = _getStyleObj(node),
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major val = current[att] || current.color;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major return Y.Color.toRGB(Y.Color.toHex(val));
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major }
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major //fontSize: getPixelFont,
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major IEComputed = {};
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major// use alpha filter for IE opacity
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Majorif (documentElement.style[OPACITY] === UNDEFINED &&
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major documentElement[FILTERS]) {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major Y.DOM.CUSTOM_STYLES[OPACITY] = {
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major get: function(node) {
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts var val = 100;
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts try { // will error if no DXImageTransform
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts } catch(e) {
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts try { // make sure its in the document
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts val = node[FILTERS]('alpha')[OPACITY];
8b6198b1170e3128c5df83267597f0058d07c70dJames Phillpotts } catch(err) {
921c9e8e47accb592cc0dcda4f6973aabc1935ceNeil Madden Y.log('getStyle: IE opacity filter not found; returning 1', 'warn', 'dom-style');
921c9e8e47accb592cc0dcda4f6973aabc1935ceNeil Madden }
921c9e8e47accb592cc0dcda4f6973aabc1935ceNeil Madden }
921c9e8e47accb592cc0dcda4f6973aabc1935ceNeil Madden return val / 100;
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major },
e8721886dbfd32e88cc7077cbee4b6bb1b44b443Peter Major
set: function(node, val, style) {
var current,
styleObj;
if (val === '') { // normalize inline style behavior
styleObj = _getStyleObj(node);
current = (OPACITY in styleObj) ? styleObj[OPACITY] : 1; // revert to original opacity
val = current;
}
if (typeof style[FILTER] == 'string') { // in case not appended
style[FILTER] = 'alpha(' + OPACITY + '=' + val * 100 + ')';
if (!node.currentStyle || !node.currentStyle[HAS_LAYOUT]) {
style.zoom = 1; // needs layout
}
}
}
};
}
try {
document.createElement('div').style.height = '-1px';
} catch(e) { // IE throws error on invalid style set; trap common cases
Y.DOM.CUSTOM_STYLES.height = {
set: function(node, val, style) {
if (parseInt(val, 10) >= 0) {
style.height = val;
} else {
Y.log('invalid style value for height: ' + val, 'warn', 'dom-style');
}
}
};
Y.DOM.CUSTOM_STYLES.width = {
set: function(node, val, style) {
if (parseInt(val, 10) >= 0) {
style.width = val;
} else {
Y.log('invalid style value for width: ' + val, 'warn', 'dom-style');
}
}
};
}
// TODO: top, right, bottom, left
IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
IEComputed.color = IEComputed.backgroundColor = ComputedStyle.getColor;
IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
ComputedStyle.getBorderWidth;
IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
IEComputed.marginLeft = ComputedStyle.getMargin;
IEComputed.visibility = ComputedStyle.getVisibility;
IEComputed.borderColor = IEComputed.borderTopColor =
IEComputed.borderRightColor = IEComputed.borderBottomColor =
IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
if (!Y.config.win[GET_COMPUTED_STYLE]) {
Y.DOM[GET_COMPUTED_STYLE] = ComputedStyle.get;
}
Y.namespace('DOM.IE');
Y.DOM.IE.COMPUTED = IEComputed;
Y.DOM.IE.ComputedStyle = ComputedStyle;
})(Y);