dom-style-debug.js revision 6469c0fd34f5d65f53cc4c8097c46e732a0819ae
YUI.add('dom-style', function(Y) {
(function(Y) {
/**
* Add style management functionality to DOM.
* @module dom
* @submodule dom-style
* @for DOM
*/
var DOCUMENT_ELEMENT = 'documentElement',
DEFAULT_VIEW = 'defaultView',
OWNER_DOCUMENT = 'ownerDocument',
STYLE = 'style',
FLOAT = 'float',
CSS_FLOAT = 'cssFloat',
STYLE_FLOAT = 'styleFloat',
TRANSPARENT = 'transparent',
GET_COMPUTED_STYLE = 'getComputedStyle',
DOCUMENT = Y.config.doc,
UNDEFINED = undefined,
Y_DOM = Y.DOM,
re_color = /color$/i,
re_unit = /width|height|top|left|right|bottom|margin|padding/i;
Y.mix(Y_DOM, {
DEFAULT_UNIT: 'px',
CUSTOM_STYLES: {
},
/**
* Sets a style property for a given element.
* @method setStyle
* @param {HTMLElement} An HTMLElement to apply the style to.
* @param {String} att The style property to set.
* @param {String|Number} val The value.
*/
setStyle: function(node, att, val, style) {
style = style || node.style;
var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES,
current;
if (style) {
if (val === null) { // normalize unsetting
val = '';
// allow increment/decrement prefix operator
} else if (/^\+|-/.test(val)) { // TODO: perf test vs charAt
current = parseFloat(Y_DOM.getStyle(node, att, style));
if (!current) { // in case of 'auto'
current = 0;
}
val = current + parseFloat(val);
}
// number values may need a unit
if (!isNaN(new Number(val)) && re_unit.test(att)) {
val += Y_DOM.DEFAULT_UNIT;
}
if (att in CUSTOM_STYLES) {
if (CUSTOM_STYLES[att].set) {
CUSTOM_STYLES[att].set(node, val, style);
return; // NOTE: return
} else if (typeof CUSTOM_STYLES[att] === 'string') {
att = CUSTOM_STYLES[att];
}
}
style[att] = val;
}
},
/**
* Returns the current style value for the given property.
* @method getStyle
* @param {HTMLElement} An HTMLElement to get the style from.
* @param {String} att The style property to get.
*/
getStyle: function(node, att, style) {
style = style || node.style;
var CUSTOM_STYLES = Y_DOM.CUSTOM_STYLES,
val = '';
if (style) {
if (att in CUSTOM_STYLES) {
if (CUSTOM_STYLES[att].get) {
return CUSTOM_STYLES[att].get(node, att, style); // NOTE: return
} else if (typeof CUSTOM_STYLES[att] === 'string') {
att = CUSTOM_STYLES[att];
}
}
val = style[att];
if (val === '') { // TODO: is empty string sufficient?
val = Y_DOM[GET_COMPUTED_STYLE](node, att);
}
}
return val;
},
/**
* Sets multiple style properties.
* @method setStyles
* @param {HTMLElement} node An HTMLElement to apply the styles to.
* @param {Object} hash An object literal of property:value pairs.
*/
setStyles: function(node, hash) {
var style = node.style;
Y.each(hash, function(v, n) {
Y_DOM.setStyle(node, n, v, style);
}, Y_DOM);
},
/**
* Returns the computed style for the given node.
* @method getComputedStyle
* @param {HTMLElement} An HTMLElement to get the style from.
* @param {String} att The style property to get.
* @return {String} The computed value of the style property.
*/
getComputedStyle: function(node, att) {
var val = '',
doc = node[OWNER_DOCUMENT];
if (node[STYLE]) {
val = doc[DEFAULT_VIEW][GET_COMPUTED_STYLE](node, null)[att];
}
return val;
}
});
// normalize reserved word float alternatives ("cssFloat" or "styleFloat")
if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][CSS_FLOAT] !== UNDEFINED) {
Y_DOM.CUSTOM_STYLES[FLOAT] = CSS_FLOAT;
} else if (DOCUMENT[DOCUMENT_ELEMENT][STYLE][STYLE_FLOAT] !== UNDEFINED) {
Y_DOM.CUSTOM_STYLES[FLOAT] = STYLE_FLOAT;
}
// fix opera computedStyle default color unit (convert to rgb)
if (Y.UA.opera) {
Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
val = view[GET_COMPUTED_STYLE](node, '')[att];
if (re_color.test(att)) {
val = Y.Color.toRGB(val);
}
return val;
};
}
// safari converts transparent to rgba(), others use "transparent"
if (Y.UA.webkit) {
Y_DOM[GET_COMPUTED_STYLE] = function(node, att) {
var view = node[OWNER_DOCUMENT][DEFAULT_VIEW],
val = view[GET_COMPUTED_STYLE](node, '')[att];
if (val === 'rgba(0, 0, 0, 0)') {
val = TRANSPARENT;
}
return val;
};
}
})(Y);
(function(Y) {
var PARSE_INT = parseInt,
RE = RegExp;
Y.Color = {
KEYWORDS: {
black: '000',
silver: 'c0c0c0',
gray: '808080',
white: 'fff',
maroon: '800000',
red: 'f00',
purple: '800080',
fuchsia: 'f0f',
green: '008000',
lime: '0f0',
olive: '808000',
yellow: 'ff0',
navy: '000080',
blue: '00f',
teal: '008080',
aqua: '0ff'
},
re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
re_hex3: /([0-9A-F])/gi,
toRGB: function(val) {
if (!Y.Color.re_RGB.test(val)) {
val = Y.Color.toHex(val);
}
if(Y.Color.re_hex.exec(val)) {
val = 'rgb(' + [
PARSE_INT(RE.$1, 16),
PARSE_INT(RE.$2, 16),
PARSE_INT(RE.$3, 16)
].join(', ') + ')';
}
return val;
},
toHex: function(val) {
val = Y.Color.KEYWORDS[val] || val;
if (Y.Color.re_RGB.exec(val)) {
val = [
Number(RE.$1).toString(16),
Number(RE.$2).toString(16),
Number(RE.$3).toString(16)
];
for (var i = 0; i < val.length; i++) {
if (val[i].length < 2) {
val[i] = '0' + val[i];
}
}
val = val.join('');
}
if (val.length < 6) {
val = val.replace(Y.Color.re_hex3, '$1$1');
}
if (val !== 'transparent' && val.indexOf('#') < 0) {
val = '#' + val;
}
return val.toUpperCase();
}
};
})(Y);
(function(Y) {
var HAS_LAYOUT = 'hasLayout',
PX = 'px',
FILTER = 'filter',
FILTERS = 'filters',
OPACITY = 'opacity',
AUTO = 'auto',
BORDER_WIDTH = 'borderWidth',
BORDER_TOP_WIDTH = 'borderTopWidth',
BORDER_RIGHT_WIDTH = 'borderRightWidth',
BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
BORDER_LEFT_WIDTH = 'borderLeftWidth',
WIDTH = 'width',
HEIGHT = 'height',
TRANSPARENT = 'transparent',
VISIBLE = 'visible',
GET_COMPUTED_STYLE = 'getComputedStyle',
UNDEFINED = undefined,
documentElement = document.documentElement,
// TODO: unit-less lineHeight (e.g. 1.22)
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,
_getStyleObj = function(node) {
return node.currentStyle || node.style;
},
ComputedStyle = {
CUSTOM_STYLES: {},
get: function(el, property) {
var value = '',
current;
if (el) {
current = _getStyleObj(el)[property];
if (property === OPACITY && Y.DOM.CUSTOM_STYLES[OPACITY]) {
value = Y.DOM.CUSTOM_STYLES[OPACITY].get(el);
} else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
value = current;
} else if (Y.DOM.IE.COMPUTED[property]) { // use compute function
value = Y.DOM.IE.COMPUTED[property](el, property);
} else if (re_unit.test(current)) { // convert to pixel
value = ComputedStyle.getPixel(el, property) + PX;
} else {
value = current;
}
}
return value;
},
sizeOffsets: {
width: ['Left', 'Right'],
height: ['Top', 'Bottom'],
top: ['Top'],
bottom: ['Bottom']
},
getOffset: function(el, prop) {
var current = _getStyleObj(el)[prop], // value of "width", "top", etc.
capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
sizeOffsets = ComputedStyle.sizeOffsets[prop],
value = '';
// IE pixelWidth incorrect for percent
// manually compute by subtracting padding and border from offset size
// NOTE: clientWidth/Height (size minus border) is 0 when current === AUTO so offsetHeight is used
// reverting to auto from auto causes position stacking issues (old impl)
if (current === AUTO || current.indexOf('%') > -1) {
value = el['offset' + capped];
if (sizeOffsets[0]) {
value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[0]);
value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[0] + 'Width', 1);
}
if (sizeOffsets[1]) {
value -= ComputedStyle.getPixel(el, 'padding' + sizeOffsets[1]);
value -= ComputedStyle.getBorderWidth(el, 'border' + sizeOffsets[1] + 'Width', 1);
}
} else { // use style.pixelWidth, etc. to convert to pixels
// need to map style.width to currentStyle (no currentStyle.pixelWidth)
if (!el.style[pixel] && !el.style[prop]) {
el.style[prop] = current;
}
value = el.style[pixel];
}
return value + PX;
},
borderMap: {
thin: '2px',
medium: '4px',
thick: '6px'
},
getBorderWidth: function(el, property, omitUnit) {
var unit = omitUnit ? '' : PX,
current = el.currentStyle[property];
if (current.indexOf(PX) < 0) { // look up keywords
if (ComputedStyle.borderMap[current]) {
current = ComputedStyle.borderMap[current];
} else {
Y.log('borderWidth computing not implemented', 'warn', 'dom-ie-style');
}
}
return (omitUnit) ? parseFloat(current) : current;
},
getPixel: function(node, att) {
// use pixelRight to convert to px
var val = null,
style = _getStyleObj(node),
styleRight = style.right,
current = style[att];
node.style.right = current;
val = node.style.pixelRight;
node.style.right = styleRight; // revert
return val;
},
getMargin: function(node, att) {
var val,
style = _getStyleObj(node);
if (style[att] == AUTO) {
val = 0;
} else {
val = ComputedStyle.getPixel(node, att);
}
return val + PX;
},
getVisibility: function(node, att) {
var current;
while ( (current = node.currentStyle) && current[att] == 'inherit') { // NOTE: assignment in test
node = node.parentNode;
}
return (current) ? current[att] : VISIBLE;
},
getColor: function(node, att) {
var current = _getStyleObj(node)[att];
if (!current || current === TRANSPARENT) {
Y.DOM.elementByAxis(node, 'parentNode', null, function(parent) {
current = _getStyleObj(parent)[att];
if (current && current !== TRANSPARENT) {
node = parent;
return true;
}
});
}
return Y.Color.toRGB(current);
},
getBorderColor: function(node, att) {
var current = _getStyleObj(node),
val = current[att] || current.color;
return Y.Color.toRGB(Y.Color.toHex(val));
}
},
//fontSize: getPixelFont,
IEComputed = {};
// use alpha filter for IE opacity
try {
if (documentElement.style[OPACITY] === UNDEFINED &&
documentElement[FILTERS]) {
Y.DOM.CUSTOM_STYLES[OPACITY] = {
get: function(node) {
var val = 100;
try { // will error if no DXImageTransform
val = node[FILTERS]['DXImageTransform.Microsoft.Alpha'][OPACITY];
} catch(e) {
try { // make sure its in the document
val = node[FILTERS]('alpha')[OPACITY];
} catch(err) {
Y.log('getStyle: IE opacity filter not found; returning 1', 'warn', 'dom-style');
}
}
return val / 100;
},
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
}
}
}
};
}
} catch(e) {
Y.log('document.documentElement.filters error (activeX disabled)', 'warn', 'dom-style');
}
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) {
var floatVal = parseFloat(val);
if (isNaN(floatVal) || floatVal >= 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) {
var floatVal = parseFloat(val);
if (isNaN(floatVal) || floatVal >= 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_WIDTH] = 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);
Y.mix(Y.DOM, {
/**
* Sets the width of the element to the given size, regardless
* of box model, border, padding, etc.
* @method setWidth
* @param {HTMLElement} element The DOM element.
* @param {String|Int} size The pixel height to size to
*/
setWidth: function(node, size) {
Y.DOM._setSize(node, 'width', size);
},
/**
* Sets the height of the element to the given size, regardless
* of box model, border, padding, etc.
* @method setHeight
* @param {HTMLElement} element The DOM element.
* @param {String|Int} size The pixel height to size to
*/
setHeight: function(node, size) {
Y.DOM._setSize(node, 'height', size);
},
_getOffsetProp: function(node, prop) {
return 'offset' + prop.charAt(0).toUpperCase() + prop.substr(1);
},
_setSize: function(node, prop, val) {
var offset;
Y.DOM.setStyle(node, prop, val + 'px');
offset = node[Y.DOM._getOffsetProp(node, prop)];
val = val - (offset - val);
// TODO: handle size less than border/padding (add class?)
if (val < 0) {
val = 0; // no negative sizes
}
Y.DOM.setStyle(node, prop, val + 'px');
}
});
}, '@VERSION@' ,{requires:['dom-base']});