UNPKG

2.59 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.getCompStyle = getCompStyle;
5
6function getPixelSize(element, style, property, fontSize) {
7 var sizeWithSuffix = style[property],
8 size = parseFloat(sizeWithSuffix),
9 suffix = sizeWithSuffix.split(/\d/)[0],
10 rootSize;
11 fontSize = fontSize != null ? fontSize : /%|em/.test(suffix) && element && element.parentElement ? getPixelSize(element.parentElement, element.parentElement.currentStyle, "fontSize", null) : 16;
12 rootSize = property == "fontSize" ? fontSize : /width/i.test(property) ? element.clientWidth : element.clientHeight;
13 return suffix == "em" ? size * fontSize : suffix == "in" ? size * 96 : suffix == "pt" ? size * 96 / 72 : suffix == "%" ? size / 100 * rootSize : size;
14}
15
16function setShortStyleProperty(style, property) {
17 var borderSuffix = property == "border" ? "Width" : "",
18 t = property + "Top" + borderSuffix,
19 r = property + "Right" + borderSuffix,
20 b = property + "Bottom" + borderSuffix,
21 l = property + "Left" + borderSuffix;
22 style[property] = (style[t] == style[r] == style[b] == style[l] ? [style[t]] : style[t] == style[b] && style[l] == style[r] ? [style[t], style[r]] : style[l] == style[r] ? [style[t], style[r], style[b]] : [style[t], style[r], style[b], style[l]]).join(" ");
23}
24
25function CSSStyleDeclaration(element) {
26 var currentStyle = element.currentStyle,
27 style = this,
28 fontSize = getPixelSize(element, currentStyle, "fontSize", null);
29
30 for (property in currentStyle) {
31 if (/width|height|margin.|padding.|border.+W/.test(property) && style[property] !== "auto") {
32 style[property] = getPixelSize(element, currentStyle, property, fontSize) + "px";
33 } else if (property === "styleFloat") {
34 style["float"] = currentStyle[property];
35 } else {
36 style[property] = currentStyle[property];
37 }
38 }
39
40 setShortStyleProperty(style, "margin");
41 setShortStyleProperty(style, "padding");
42 setShortStyleProperty(style, "border");
43 style.fontSize = fontSize + "px";
44 return style;
45}
46
47CSSStyleDeclaration.prototype = {
48 constructor: CSSStyleDeclaration,
49 getPropertyPriority: function getPropertyPriority() {},
50 getPropertyValue: function getPropertyValue(prop) {
51 return this[prop] || "";
52 },
53 item: function item() {},
54 removeProperty: function removeProperty() {},
55 setProperty: function setProperty() {},
56 getPropertyCSSValue: function getPropertyCSSValue() {}
57};
58
59function getCompStyle(element) {
60 if (window.getComputedStyle) return window.getComputedStyle(element);
61 return new CSSStyleDeclaration(element);
62}
\No newline at end of file