-
Notifications
You must be signed in to change notification settings - Fork 15
/
boxsizing.htc
executable file
·399 lines (360 loc) · 10.4 KB
/
boxsizing.htc
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* box-sizing Polyfill
*
* A polyfill for box-sizing: border-box for IE6 & IE7.
*
* JScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* See <http://www.gnu.org/licenses/lgpl-3.0.txt>
*
* @category JScript
* @package box-sizing-polyfill
* @author Christian Schepp Schaefer <[email protected]> <http://twitter.com/derSchepp>
* @copyright 2012 Christian Schepp Schaefer
* @license http://www.gnu.org/copyleft/lesser.html The GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0
* @link http://github.com/Schepp/box-sizing-polyfill
*
* PREFACE:
*
* This box-sizing polyfill is based on previous work done by Erik Arvidsson,
* which he published in 2002 on http://webfx.eae.net/dhtml/boxsizing/boxsizing.html.
*
* USAGE:
*
* Add the behavior/HTC after every `box-sizing: border-box;` that you assign:
*
* box-sizing: border-box;
* *behavior: url(/scripts/boxsizing.htc);`
*
* Prefix the `behavior` property with a star, like seen above, so it will only be seen by
* IE6 & IE7, not by IE8+ who already implement box-sizing.
*
* The URL to the HTC file must be relative to your HTML(!) document, not relative to your CSS.
* That's why I'd advise you to use absolute paths like in the example.
*
*/
<component lightWeight="true">
<attach event="onpropertychange" onevent="checkPropertyChange()" />
<attach event="ondetach" onevent="restore()" />
<attach event="onresize" for="window" onevent="update()" />
<script type="text/javascript">
//<![CDATA[
var viewportwidth = (typeof window.innerWidth != 'undefined' ? window.innerWidth : element.document.documentElement.clientWidth);
// Shortcut for the document object
var doc = element.document;
// Buffer for multiple resize events
var resizetimeout = null;
// Don't apply box-sizing to certain elements
var apply = false;
switch(element.nodeName){
case '#comment':
case 'HTML':
case 'HEAD':
case 'TITLE':
case 'SCRIPT':
case 'STYLE':
case 'LINK':
case 'META':
break;
default:
apply = true;
break;
}
/*
* update gets called during resize events, then waits until there are no further resize events, and finally triggers a recalculation
*/
function update(){
if(resizetimeout !== null){
window.clearTimeout(resizetimeout);
}
resizetimeout = window.setTimeout(function(){
restore();
init();
resizetimeout = null;
},100);
}
/*
* restore gets called when the behavior is being detached (see event binding at the top),
* resets everything like it was before applying the behavior
*/
function restore(){
if(apply){
element.runtimeStyle.removeAttribute("width");
element.runtimeStyle.removeAttribute("height");
}
}
/*
* init gets called once at the start and then never again,
* triggers box-sizing calculations and updates width and height
*/
function init(){
if(apply){
updateBorderBoxWidth();
updateBorderBoxHeight();
}
}
/*
* checkPropertyChange gets called as soon as an element property changes
* (see event binding at the top), it then checks if any property influencing its
* dimensions was changed and if yes recalculates width and height
*/
function checkPropertyChange(){
if(apply){
var pn = event.propertyName;
if(pn === "style.boxSizing" && element.style.boxSizing === ""){
element.style.removeAttribute("boxSizing");
element.runtimeStyle.removeAttribute("boxSizing");
element.runtimeStyle.removeAttribute("width");
element.runtimeStyle.removeAttribute("height");
}
switch (pn){
case "style.width":
case "style.borderLeftWidth":
case "style.borderLeftStyle":
case "style.borderRightWidth":
case "style.borderRightStyle":
case "style.paddingLeft":
case "style.paddingRight":
updateBorderBoxWidth();
break;
case "style.height":
case "style.borderTopWidth":
case "style.borderTopStyle":
case "style.borderBottomWidth":
case "style.borderBottomStyle":
case "style.paddingTop":
case "style.paddingBottom":
updateBorderBoxHeight();
break;
case "className":
case "style.boxSizing":
updateBorderBoxWidth();
updateBorderBoxHeight();
break;
}
}
}
/*
* Helper function, taken from Dean Edward's IE7 framework,
* added by Schepp on 12.06.2010.
* http://code.google.com/p/ie7-js/
*
* Allows us to convert from relative to pixel-values.
*/
function getPixelValue(value){
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
var style = element.style.left;
var runtimeStyle = element.runtimeStyle.left;
element.runtimeStyle.left = element.currentStyle.left;
element.style.left = value || 0;
value = parseInt(element.style.pixelLeft);
element.style.left = style;
element.runtimeStyle.left = runtimeStyle;
return value;
}
function getPixelWidth(object, value){
// For Pixel Values
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
// For Percentage Values
var PERCENT = /^[\d\.]+%$/i;
if (PERCENT.test(value)){
try{
parentWidth = getPixelWidth(object.parentElement,(object.parentElement.currentStyle.width != "auto" ? object.parentElement.currentStyle.width : "100%"));
value = (parseFloat(value) / 100) * parentWidth;
}
catch(e){
value = (parseFloat(value) / 100) * element.document.documentElement.clientWidth;
}
return parseInt(value);
}
// For EM Values
var style = object.style.left;
var runtimeStyle = object.runtimeStyle.left;
object.runtimeStyle.left = object.currentStyle.left;
object.style.left = value || 0;
value = parseInt(object.style.pixelLeft);
object.style.left = style;
object.runtimeStyle.left = runtimeStyle;
return value;
}
function getPixelHeight(object, value){
// For Pixel Values
var PIXEL = /^\d+(px)?$/i;
if (PIXEL.test(value)) return parseInt(value);
// For Percentage Values
var PERCENT = /^[\d\.]+%$/i;
if (PERCENT.test(value)){
try{
if(object.parentElement.currentStyle.height != "auto"){
switch(object.parentElement.nodeName){
default:
parentHeight = getPixelHeight(object.parentElement,object.parentElement.currentStyle.height);
if(parentHeight !== "auto"){
value = (parseFloat(value) / 100) * parentHeight;
}
else {
value = "auto";
}
break;
case 'HTML':
parentHeight = element.document.documentElement.clientHeight;
if(parentHeight !== "auto"){
value = (parseFloat(value) / 100) * parentHeight;
}
else {
value = "auto";
}
break;
}
if(value !== "auto") value = parseInt(value);
}
else {
value = "auto";
}
}
catch(e){
value = "auto";
}
return value;
}
// For EM Values
var style = object.style.left;
var runtimeStyle = object.runtimeStyle.left;
object.runtimeStyle.left = object.currentStyle.left;
object.style.left = value || 0;
value = parseInt(object.style.pixelLeft);
object.style.left = style;
object.runtimeStyle.left = runtimeStyle;
return value;
}
/*
* getBorderWidth & friends
* Border width getters
*/
function getBorderWidth(sSide){
if(element.currentStyle["border" + sSide + "Style"] == "none"){
return 0;
}
var n = getPixelValue(element.currentStyle["border" + sSide + "Width"]);
return n || 0;
}
function getBorderLeftWidth() { return getBorderWidth("Left"); }
function getBorderRightWidth() { return getBorderWidth("Right"); }
function getBorderTopWidth() { return getBorderWidth("Top"); }
function getBorderBottomWidth() { return getBorderWidth("Bottom"); }
/*
* getPadding & friends
* Padding width getters
*/
function getPadding(sSide) {
var n = getPixelValue(element.currentStyle["padding" + sSide]);
return n || 0;
}
function getPaddingLeft() { return getPadding("Left"); }
function getPaddingRight() { return getPadding("Right"); }
function getPaddingTop() { return getPadding("Top"); }
function getPaddingBottom() { return getPadding("Bottom"); }
/*
* getBoxSizing
* Get the box-sizing value for the current element
*/
function getBoxSizing(){
var s = element.style;
var cs = element.currentStyle
if(typeof s.boxSizing != "undefined" && s.boxSizing != ""){
return s.boxSizing;
}
if(typeof s["box-sizing"] != "undefined" && s["box-sizing"] != ""){
return s["box-sizing"];
}
if(typeof cs.boxSizing != "undefined" && cs.boxSizing != ""){
return cs.boxSizing;
}
if(typeof cs["box-sizing"] != "undefined" && cs["box-sizing"] != ""){
return cs["box-sizing"];
}
return getDocumentBoxSizing();
}
/*
* getDocumentBoxSizing
* Get the default document box sizing (check for quirks mode)
*/
function getDocumentBoxSizing(){
if(doc.compatMode === null || doc.compatMode === "BackCompat"){
return "border-box";
}
return "content-box"
}
/*
* setBorderBoxWidth & friends
* Width and height setters
*/
function setBorderBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n - getBorderLeftWidth() -
getPaddingLeft() - getPaddingRight() - getBorderRightWidth()) + "px";
}
function setBorderBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n - getBorderTopWidth() -
getPaddingTop() - getPaddingBottom() - getBorderBottomWidth()) + "px";
}
function setContentBoxWidth(n){
element.runtimeStyle.width = Math.max(0, n + getBorderLeftWidth() +
getPaddingLeft() + getPaddingRight() + getBorderRightWidth()) + "px";
}
function setContentBoxHeight(n){
element.runtimeStyle.height = Math.max(0, n + getBorderTopWidth() +
getPaddingTop() + getPaddingBottom() + getBorderBottomWidth()) + "px";
}
/*
* updateBorderBoxWidth & updateBorderBoxHeight
*
*/
function updateBorderBoxWidth() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csw = element.currentStyle.width;
if(csw != "auto"){
csw = getPixelWidth(element,csw);
if(getBoxSizing() == "border-box"){
setBorderBoxWidth(parseInt(csw));
}
else{
setContentBoxWidth(parseInt(csw));
}
}
}
function updateBorderBoxHeight() {
if(getDocumentBoxSizing() == getBoxSizing()){
return;
}
var csh = element.currentStyle.height;
if(csh != "auto"){
csh = getPixelHeight(element,csh);
if(csh !== "auto"){
if(getBoxSizing() == "border-box"){
setBorderBoxHeight(parseInt(csh));
}
else{
setContentBoxHeight(parseInt(csh));
}
}
}
}
// Run the calculations
init();
//]]>
</script>
</component>