Permalink
Browse files

More pixel properties

  • Loading branch information...
1 parent 0f5b840 commit 0fc374847a1d3d1dc24ba4c96760d35f63fccfff @jonathantneal committed Apr 27, 2012
Showing with 30 additions and 13 deletions.
  1. +30 −13 getComputedStyle.js
View
@@ -1,15 +1,16 @@
// getComputedStyle
!('getComputedStyle' in this) && (this.getComputedStyle = (function () {
- function getPixelSize(element, sizeWithSuffix, rootSize) {
+ function getPixelSize(element, style, property, fontSize) {
var
+ sizeWithSuffix = style[property],
size = parseFloat(sizeWithSuffix),
- suffix = sizeWithSuffix.split(/\d/)[0];
+ suffix = sizeWithSuffix.split(/\d/)[0],
+ rootSize;
- if (rootSize == null) {
- rootSize = /em|%/.test(suffix) && element.parentElement ? getPixelSize(element.parentElement, element.parentElement.currentStyle.fontSize, null) : 16;
- }
+ fontSize = fontSize != null ? fontSize : /%|em/.test(suffix) && element.parentElement ? getPixelSize(element.parentElement, element.parentElement.currentStyle, 'fontSize', null) : 16;
+ rootSize = property == 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
- return (suffix == 'em') ? size * rootSize : (suffix == 'in') ? size * 96 : (suffix == 'pt') ? size * 96 / 72 : (suffix == '%') ? size / 100 * rootSize : size;
+ return (suffix == 'em') ? size * fontSize : (suffix == 'in') ? size * 96 : (suffix == 'pt') ? size * 96 / 72 : (suffix == '%') ? size / 100 * rootSize : size;
}
function setShortStyleProperty(style, property) {
@@ -26,17 +27,19 @@
: [style[t], style[r], style[b], style[l]]).join(' ');
}
- function getComputedStyle(element) {
+ function CSSStyleDeclaration(element) {
var
currentStyle = element.currentStyle,
- style = {},
- fontSize = getPixelSize(element, currentStyle.fontSize, null);
+ style = this,
+ fontSize = getPixelSize(element, currentStyle, 'fontSize', null);
- for (property in currentStyle) {
- style[property] = currentStyle[property];
+ console.log(['fontSize', fontSize]);
- if (/(margin|padding)(Top|Right|Bottom|Left)|border(Top|Right|Bottom|Left)Width/.test(property)) {
- style[property] = getPixelSize(element, style[property], fontSize) + 'px';
+ for (property in currentStyle) {
+ if (/width|height|margin.|padding.|border.+W/.test(property) && style[property] !== 'auto') {
+ style[property] = getPixelSize(element, currentStyle, property, fontSize) + 'px';
+ } else {
+ style[property] = currentStyle[property];
}
}
@@ -49,5 +52,19 @@
return style;
}
+ CSSStyleDeclaration.prototype = {
+ constructor: CSSStyleDeclaration,
+ getPropertyPriority: function () {},
+ getPropertyValue: function () {},
+ item: function () {},
+ removeProperty: function () {},
+ setProperty: function () {},
+ getPropertyCSSValue: function () {}
+ };
+
+ function getComputedStyle(element) {
+ return new CSSStyleDeclaration(element);
+ }
+
return getComputedStyle;
})(this));

0 comments on commit 0fc3748

Please sign in to comment.