|
| 1 | +/** |
| 2 | + * Adapted from: https://raw.githubusercontent.com/Financial-Times/polyfill-service |
| 3 | + */ |
| 4 | +function getComputedStylePixel(element, property, fontSize) { |
| 5 | + var |
| 6 | + // Internet Explorer sometimes struggles to read currentStyle until the element's document is accessed. |
| 7 | + value = element.document && element.currentStyle[property].match(/([\d\.]+)(%|cm|em|in|mm|pc|pt|)/) || [0, 0, ''], |
| 8 | + size = value[1], |
| 9 | + suffix = value[2], |
| 10 | + rootSize; |
| 11 | + |
| 12 | + fontSize = !fontSize ? fontSize : /%|em/.test(suffix) && element.parentElement ? getComputedStylePixel(element.parentElement, 'fontSize', null) : 16; |
| 13 | + rootSize = property === 'fontSize' ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight; |
| 14 | + |
| 15 | + return suffix === '%' ? size / 100 * rootSize : |
| 16 | + suffix === 'cm' ? size * 0.3937 * 96 : |
| 17 | + suffix === 'em' ? size * fontSize : |
| 18 | + suffix === 'in' ? size * 96 : |
| 19 | + suffix === 'mm' ? size * 0.3937 * 96 / 10 : |
| 20 | + suffix === 'pc' ? size * 12 * 96 / 72 : |
| 21 | + suffix === 'pt' ? size * 96 / 72 : |
| 22 | + size; |
| 23 | +} |
| 24 | + |
| 25 | +function setShortStyleProperty(style, property) { |
| 26 | + var |
| 27 | + borderSuffix = property === 'border' ? 'Width' : '', |
| 28 | + t = property + 'Top' + borderSuffix, |
| 29 | + r = property + 'Right' + borderSuffix, |
| 30 | + b = property + 'Bottom' + borderSuffix, |
| 31 | + l = property + 'Left' + borderSuffix; |
| 32 | + |
| 33 | + style[property] = (style[t] === style[r] && style[t] === style[b] && style[t] === style[l] ? [style[t]] : |
| 34 | + style[t] === style[b] && style[l] === style[r] ? [style[t], style[r]] : |
| 35 | + style[l] === style[r] ? [style[t], style[r], style[b]] : |
| 36 | + [style[t], style[r], style[b], style[l]]).join(' '); |
| 37 | +} |
| 38 | + |
| 39 | +// <CSSStyleDeclaration> |
| 40 | +function CSSStyleDeclaration(element) { |
| 41 | + var currentStyle = element.currentStyle, |
| 42 | + fontSize = getComputedStylePixel(element, 'fontSize'), |
| 43 | + unCamelCase = function(match) { |
| 44 | + return '-' + match.toLowerCase(); |
| 45 | + }, |
| 46 | + property; |
| 47 | + |
| 48 | + for (property in currentStyle) { |
| 49 | + Array.prototype.push.call(this, property === 'styleFloat' ? 'float' : property.replace(/[A-Z]/, unCamelCase)); |
| 50 | + |
| 51 | + if (property === 'width') { |
| 52 | + this[property] = element.offsetWidth + 'px'; |
| 53 | + } else if (property === 'height') { |
| 54 | + this[property] = element.offsetHeight + 'px'; |
| 55 | + } else if (property === 'styleFloat') { |
| 56 | + this.float = currentStyle[property]; |
| 57 | + } else if (/margin.|padding.|border.+W/.test(property) && this[property] !== 'auto') { |
| 58 | + this[property] = Math.round(getComputedStylePixel(element, property, fontSize)) + 'px'; |
| 59 | + } else if (/^outline/.test(property)) { |
| 60 | + // errors on checking outline |
| 61 | + try { |
| 62 | + this[property] = currentStyle[property]; |
| 63 | + } catch (error) { |
| 64 | + this.outlineColor = currentStyle.color; |
| 65 | + this.outlineStyle = this.outlineStyle || 'none'; |
| 66 | + this.outlineWidth = this.outlineWidth || '0px'; |
| 67 | + this.outline = [this.outlineColor, this.outlineWidth, this.outlineStyle].join(' '); |
| 68 | + } |
| 69 | + } else { |
| 70 | + this[property] = currentStyle[property]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + setShortStyleProperty(this, 'margin'); |
| 75 | + setShortStyleProperty(this, 'padding'); |
| 76 | + setShortStyleProperty(this, 'border'); |
| 77 | + |
| 78 | + this.fontSize = Math.round(fontSize) + 'px'; |
| 79 | +} |
| 80 | + |
| 81 | +CSSStyleDeclaration.prototype = { |
| 82 | + constructor: CSSStyleDeclaration, |
| 83 | + // <CSSStyleDeclaration>.getPropertyPriority |
| 84 | + getPropertyPriority: function() { |
| 85 | + throw new Error('NotSupportedError: DOM Exception 9'); |
| 86 | + }, |
| 87 | + // <CSSStyleDeclaration>.getPropertyValue |
| 88 | + getPropertyValue: function(property) { |
| 89 | + return this[property.replace(/-\w/g, function(match) { |
| 90 | + return match[1].toUpperCase(); |
| 91 | + })]; |
| 92 | + }, |
| 93 | + // <CSSStyleDeclaration>.item |
| 94 | + item: function(index) { |
| 95 | + return this[index]; |
| 96 | + }, |
| 97 | + // <CSSStyleDeclaration>.removeProperty |
| 98 | + removeProperty: function() { |
| 99 | + throw new Error('NoModificationAllowedError: DOM Exception 7'); |
| 100 | + }, |
| 101 | + // <CSSStyleDeclaration>.setProperty |
| 102 | + setProperty: function() { |
| 103 | + throw new Error('NoModificationAllowedError: DOM Exception 7'); |
| 104 | + }, |
| 105 | + // <CSSStyleDeclaration>.getPropertyCSSValue |
| 106 | + getPropertyCSSValue: function() { |
| 107 | + throw new Error('NotSupportedError: DOM Exception 9'); |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | + // <Global>.getComputedStyle |
| 112 | +exports.getComputedStyle = function getComputedStyle(element) { |
| 113 | + return new CSSStyleDeclaration(element); |
| 114 | +}; |
0 commit comments