forked from louisremi/background-size-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgroundsize.htc
272 lines (217 loc) · 6.8 KB
/
backgroundsize.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
<public:component lightWeight="true">
<public:attach event="onpropertychange" onevent="handlePropertychange()" />
<public:attach event="ondetach" onevent="restore()" />
<public:attach event="onresize" for="window" onevent="handleResize()" />
<script type="text/javascript">
var rsrc = /url\(["']?(.*?)["']?\)/,
positions = {
top: 0,
left: 0,
bottom: 1,
right: 1,
center: .5
},
doc = element.document;
init();
// remove the background-image and emulate it with a wrapped <img/>
function init() {
var wrapper = doc.createElement( "div" ),
img = doc.createElement( "img" ),
expando,
pos;
wrapper.style.position = "absolute";
wrapper.style.zIndex = -1;
wrapper.style.top = 0;
wrapper.style.right = 0;
wrapper.style.left = 0;
wrapper.style.bottom = 0;
wrapper.style.overflow = "hidden";
img.alt = "";
img.style.position = "absolute";
img.style.width = img.style.width = "auto";
wrapper.appendChild( img );
element.insertBefore( wrapper, element.firstChild );
pos = [
element.currentStyle.backgroundPositionX,
element.currentStyle.backgroundPositionY
];
// save useful data for quick access
element.bgsExpando = expando = {
wrapper: wrapper,
img: img,
backgroundSize: element.currentStyle['background-size'],
// Only keywords or percentage values are supported
backgroundPositionX: positions[ pos[0] ] || parseFloat( pos[0] ) / 100,
backgroundPositionY: positions[ pos[1] ] || parseFloat( pos[1] ) / 100
};
// This is the part where we mess with the existing DOM
// to make sure that the background image is correctly zIndexed
if ( element.currentStyle.zIndex == "auto" ) {
element.style.zIndex = 0;
}
if ( element.currentStyle.position == "static" ) {
element.style.position = "relative";
}
if ( refreshDisplay( element, expando ) ) {
refreshDimensions( element, expando );
refreshBackgroundImage( element, expando, function() {
updateBackground( element, expando );
});
}
}
function refreshDisplay( element, expando ) {
var display = element.currentStyle.display;
if ( display != expando.display ) {
expando.display = display;
expando.somethingChanged = true;
}
return display != "none";
}
function refreshDimensions( element, expando ) {
var innerWidth = element.offsetWidth
- ( parseFloat( element.currentStyle.borderLeftWidth ) || 0 )
- ( parseFloat( element.currentStyle.borderRightWidth ) || 0 ),
innerHeight = element.offsetHeight
- ( parseFloat( element.currentStyle.borderTopWidth ) || 0 )
- ( parseFloat( element.currentStyle.borderBottomWidth ) || 0 );
if ( innerWidth != expando.innerWidth || innerHeight != expando.innerHeight ) {
expando.innerWidth = innerWidth;
expando.innerHeight = innerHeight;
expando.somethingChanged = true;
}
}
function refreshBackgroundImage( element, expando, callback ) {
var img = expando.img,
src = ( rsrc.exec( element.currentStyle.backgroundImage ) || [] )[1];
if ( src && src != expando.backgroundSrc ) {
expando.backgroundSrc = src;
expando.somethingChanged = true;
img.onload = function() {
var width = img.width,
height = img.height;
// ignore onload on the proxy image
if ( width == 1 && height == 1 ) { return; }
expando.imgWidth = width;
expando.imgHeight = height;
expando.constrain = false;
callback();
img.style.visibility = "visible";
img.onload = null;
};
img.style.visibility = "hidden";
img.src = expando.backgroundSrc;
// force onload execution
if ( img.readyState || img.complete ) {
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
img.src = expando.backgroundSrc;
}
expando.ignoreNextPropertyChange = true;
element.style.backgroundImage = "none";
} else {
// the callback should be exacuted in all cases
callback();
}
}
function updateBackground( element, expando ) {
if ( !expando.somethingChanged ) { return; }
var img = expando.img,
elemRatio = expando.innerWidth / expando.innerHeight,
imgRatio = expando.imgWidth / expando.imgHeight,
prevConstrain = expando.constrain,
curConstrain,
delta;
if ( expando.backgroundSize == "contain" ) {
if ( imgRatio > elemRatio ) {
expando.constrain = curConstrain = "width";
delta = Math.floor(
( expando.innerHeight - expando.innerWidth / imgRatio )
* expando.backgroundPositionY
);
img.style.top = delta + "px";
// when switching from height to width constraint,
// make sure to release constraint on height and reset left
if ( curConstrain != prevConstrain ) {
img.style.width = "100%";
img.style.height = "auto";
img.style.left = 0;
}
// elemRatio > imgRatio
} else {
expando.constrain = curConstrain = "height";
delta = Math.floor(
( expando.innerWidth - expando.innerHeight * imgRatio )
* expando.backgroundPositionX
);
img.style.left = delta + "px";
if ( curConstrain != prevConstrain ) {
img.style.width = "auto";
img.style.height = "100%";
img.style.top = 0;
}
}
} else if ( expando.backgroundSize == "cover" ) {
if ( imgRatio > elemRatio ) {
expando.constrain = curConstrain = "height";
delta = Math.floor(
( expando.innerHeight * imgRatio - expando.innerWidth )
* expando.backgroundPositionX
);
img.style.left = -delta + "px";
// when switching from height to width constraint,
// make sure to release constraint on height and reset left
if ( curConstrain != prevConstrain ) {
img.style.width = "auto";
img.style.height = "100%";
img.style.top = 0;
}
// elemRatio > imgRatio
} else {
expando.constrain = curConstrain = "width";
delta = Math.floor(
( expando.innerWidth / imgRatio - expando.innerHeight )
* expando.backgroundPositionY
);
img.style.top = -delta + "px";
if ( curConstrain != prevConstrain ) {
img.style.width = "100%";
img.style.height = "auto";
img.style.left = 0;
}
}
}
expando.somethingChanged = false;
}
// handle different style changes
function handlePropertychange() {
var expando = element.bgsExpando;
// this prevents handling propertychange events caused by this script
// TODO: make it reliable
if ( expando.ignoreNextPropertyChange ) {
expando.ignoreNextPropertyChange = false;
return;
}
if ( refreshDisplay( element, expando ) ) {
refreshDimensions( element, expando );
refreshBackgroundImage( element, expando, function() {
updateBackground( element, expando );
});
}
}
function handleResize() {
// TODO: throttle resize events
var expando = element.bgsExpando;
if ( expando.display != "none" ) {
refreshDimensions( element, expando );
updateBackground( element, expando );
}
}
function restore() {
var expando = element.bgsExpando;
try {
element.style.backgroundImage = "url('" + expando.backgroundSrc + "')";
element.removeChild( expando.wrapper );
element.bgsExpando = null;
} catch(e) {}
}
</script>
</public:component>