This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
polycalc.js
202 lines (164 loc) · 5.46 KB
/
polycalc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Redistribution and use in source and binary forms are permitted provided
that the above copyright notice and this paragraph are duplicated in all
such forms and that any documentation, advertising materials, and other
materials related to such distribution and use acknowledge that the
software was developed by the . The name of the may not be used to
endorse or promote products derived from this software without specific
prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND
WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE.
*/
if(!jQuery || !CSSParser) {
alert("PolyCalc requires jQuery and JSCSSP to function. Disabling.");
} else {
if(typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function(str){
return this.indexOf(str) == 0;
};
}
$(document).ready(function() {
window.PolyCalc = new function() {
this.abort = false;
this.initiate = function() {
var parser = new CSSParser();
var styleSheets = $("style");
styleSheets.each(function() {
parseStyleSheet(parser, $(this).html());
});
styleSheets = $("link[rel='stylesheet']");
styleSheets.each(function() {
$.get($(this).attr("href"), function(data){
parseStyleSheet(parser, data);
});
});
// Do not use inline styles if you are building for Internet Explorer!
$("*").each(function() { // $("[style*='calc(']"); fails for Chrome
if($(this).attr("style") === undefined)
return;
if($(this).attr("style").indexOf("calc(") != -1)
parseInline(parser, $(this));
});
}
var parseStyleSheet = function(parser, source) {
var styleSheet = parser.parse(source, false, false);
var selectors = styleSheet.cssRules;
for(var i = 0; i < selectors.length; ++i) {
var selector = selectors[i];
parseSelector(selector, false);
}
}
var parseInline = function(parser, element) {
var source = "* { " + element.attr("style") + " }";
var style = parser.parse(source, false, false);
var properties = style.cssRules[0].declarations;
for(var i = 0; i < properties.length; ++i) {
var property = properties[i];
parseProperty(element, property, true);
}
}
var parseSelector = function(selector) {
var properties = selector.declarations;
for(var i = 0; i < properties.length; ++i) {
var property = properties[i];
parseProperty(selector, property, false);
}
}
var parseProperty = function(selector, property, elementKnown) {
var values = property.values;
for(var i = 0; i < values.length; ++i) {
var value = values[i];
if(!elementKnown)
var selectorValue = selector.selectorText();
var propertyValue = property.property;
var valueValue = value.value;
if(valueValue.indexOf("calc(") !== -1) {
if(elementKnown) {
elements = selector;
} else {
elements = $(selectorValue);
}
var update = function() {
elements.each(function() {
var newValue = parseExpression(propertyValue, valueValue, $(this)) + "px";
$(this).css(propertyValue, newValue);
});
}
$(window).resize(update);
update();
}
}
}
var parseExpression = function(propertyValue, expression, element) {
var newExpression = "";
expression = expression.match(/^calc\((.+)\)$/)[1];
var value = -1;
for(var i = 0; i < expression.length; ++i) {
var substr = expression.substring(i);
var regex = substr.match(/^[\d.]+/);
if(regex !== null) {
value = parseFloat(regex[0], 10);
i += regex[0].length - 1;
continue;
}
regex = substr.match(/^([A-Za-z]+|%)/);
if(regex !== null) {
value = convertUnit(regex[1], "px", value, propertyValue, element);
if(value !== -1)
newExpression += value;
i += regex[1].length - 1;
value = -1;
continue;
}
var char = expression.charAt(i);
if(char == '+' || char == '-' || char == '*' || char == '/' || char == '(' || char == ')') {
newExpression += char;
value = -1;
}
}
return eval(newExpression);
}
var convertUnit = function(from, to, value, propertyValue, element) {
switch(to) {
case "px": {
switch(from) {
case "px":
return value;
case "%":
value *= 0.01;
value *= parseInt(element.parent().css(propertyValue), 10);
return value;
case "em":
value *= parseInt(element.parent().css("font-size"), 10);
return value;
case "rem":
value *= parseInt($("body").css("font-size"), 10);
return value;
case "in":
value *= 96;
return value;
case "pt":
value *= 4/3;
return value;
case "pc":
value *= 16;
return value;
case "mm":
value *= 9.6;
value /= 2.54
return value;
case "cm":
value *= 96;
value /= 2.54
return value;
}
break;
}
}
return -1;
}
};
PolyCalc.initiate();
});
}