-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
363 lines (321 loc) · 10.7 KB
/
Copy pathtest.html
File metadata and controls
363 lines (321 loc) · 10.7 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AutoSizes Test</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
margin-bottom: 10px;
}
.subtitle {
color: #666;
margin-bottom: 30px;
}
.test-section {
margin-bottom: 60px;
padding: 20px;
background: #f5f5f5;
border-radius: 8px;
}
.test-section h2 {
margin-top: 0;
color: #333;
}
.image-container {
background: white;
padding: 20px;
border-radius: 4px;
margin-bottom: 20px;
}
img {
max-width: 100%;
height: auto;
display: block;
border: 2px solid #ddd;
}
/* Demo: fade in after autosized */
img.autosizes:not(.autosized) {
opacity: 0.3;
}
img.autosized {
opacity: 1;
transition: opacity 0.3s;
border-color: #4caf50;
}
.info {
margin-top: 10px;
padding: 10px;
background: #e3f2fd;
border-radius: 4px;
font-size: 14px;
font-family: 'Courier New', monospace;
}
.info strong {
color: #1976d2;
}
.half-width {
width: 50%;
}
.quarter-width {
width: 25%;
}
.controls {
position: sticky;
top: 0;
background: white;
padding: 15px;
border: 2px solid #333;
border-radius: 8px;
margin-bottom: 20px;
z-index: 100;
}
button {
padding: 10px 20px;
margin-right: 10px;
background: #1976d2;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
button:hover {
background: #1565c0;
}
.log {
background: #263238;
color: #aed581;
padding: 15px;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 13px;
max-height: 200px;
overflow-y: auto;
margin-top: 20px;
}
.log-entry {
margin-bottom: 5px;
}
.log-entry.event {
color: #81d4fa;
}
</style>
</head>
<body>
<h1>AutoSizes Test Page</h1>
<p class="subtitle">Testing automatic sizes calculation for responsive images with sizes="auto"</p>
<div class="controls">
<button onclick="updateAllSizes()">Recalculate All</button>
<button onclick="toggleWidth()">Toggle Container Width</button>
<button onclick="clearLog()">Clear Log</button>
</div>
<div id="log" class="log"></div>
<!-- Test 1: Basic image with sizes="auto" -->
<div class="test-section">
<h2>Test 1: Basic Image with sizes="auto"</h2>
<div class="image-container" id="container1">
<img
class="autosizes"
sizes="auto"
srcset="https://via.placeholder.com/300x200?text=300w 300w,
https://via.placeholder.com/600x400?text=600w 600w,
https://via.placeholder.com/900x600?text=900w 900w,
https://via.placeholder.com/1200x800?text=1200w 1200w"
src="https://via.placeholder.com/600x400?text=fallback"
alt="Test image"
/>
<div class="info">
<strong>sizes:</strong> <span id="sizes1">Not set</span><br>
<strong>Current width:</strong> <span id="width1">-</span>px<br>
<strong>Has autosized class:</strong> <span id="class1">No</span>
</div>
</div>
</div>
<!-- Test 2: Half width image -->
<div class="test-section">
<h2>Test 2: Half Width Container</h2>
<div class="image-container half-width" id="container2">
<img
class="autosizes"
sizes="auto"
srcset="https://via.placeholder.com/300x200?text=300w 300w,
https://via.placeholder.com/600x400?text=600w 600w,
https://via.placeholder.com/900x600?text=900w 900w"
src="https://via.placeholder.com/600x400?text=fallback"
alt="Half width test"
/>
<div class="info">
<strong>sizes:</strong> <span id="sizes2">Not set</span><br>
<strong>Current width:</strong> <span id="width2">-</span>px
</div>
</div>
</div>
<!-- Test 3: Picture element -->
<div class="test-section">
<h2>Test 3: Picture Element (correct HTML spec usage)</h2>
<div class="image-container" id="container3">
<picture>
<source
media="(min-width: 768px)"
srcset="https://via.placeholder.com/800x600?text=Desktop+800w 800w,
https://via.placeholder.com/1200x900?text=Desktop+1200w 1200w"
/>
<source
srcset="https://via.placeholder.com/400x300?text=Mobile+400w 400w,
https://via.placeholder.com/600x450?text=Mobile+600w 600w"
/>
<img
class="autosizes"
sizes="auto"
srcset="https://via.placeholder.com/400x300?text=400w 400w,
https://via.placeholder.com/600x450?text=600w 600w"
src="https://via.placeholder.com/400x300?text=fallback"
alt="Picture element test"
/>
</picture>
<div class="info">
<strong>img sizes:</strong> <span id="sizes3img">Not set</span><br>
<strong>source elements:</strong> No sizes attribute (per HTML spec)<br>
<strong>Current width:</strong> <span id="width3">-</span>px
</div>
</div>
</div>
<!-- Test 4: Quarter width container -->
<div class="test-section">
<h2>Test 4: Quarter Width Container</h2>
<div class="image-container quarter-width" id="container4">
<img
class="autosizes"
sizes="auto"
srcset="https://via.placeholder.com/300x200?text=300w 300w,
https://via.placeholder.com/600x400?text=600w 600w"
src="https://via.placeholder.com/300x200?text=fallback"
alt="Quarter width test"
/>
<div class="info">
<strong>sizes:</strong> <span id="sizes4img">Not set</span><br>
<strong>Current width:</strong> <span id="width4">-</span>px
</div>
</div>
</div>
<!-- Test 5: data-sizes attribute -->
<div class="test-section">
<h2>Test 5: Using data-sizes attribute</h2>
<p>Set config.sizesAttr = 'data-sizes' to test prefix support</p>
<div class="image-container half-width" id="container5">
<img
class="autosizes"
data-sizes="auto"
srcset="https://via.placeholder.com/300x200?text=300w 300w,
https://via.placeholder.com/600x400?text=600w 600w"
src="https://via.placeholder.com/300x200?text=fallback"
alt="data-sizes test"
id="dataSizesImg"
/>
<div class="info">
<strong>data-sizes:</strong> <span id="sizes5data">Not set</span><br>
<strong>sizes:</strong> <span id="sizes5">Not set</span><br>
<strong>Note:</strong> Both should be set when using prefix
</div>
</div>
</div>
<script type="module">
// Import autosizes library
import autoSizes from './index.js';
// Make autoSizes available globally for console debugging
window.autoSizes = autoSizes;
// Log to page
const logContainer = document.getElementById('log');
function log(message, type = 'info') {
const entry = document.createElement('div');
entry.className = `log-entry ${type}`;
entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
logContainer.appendChild(entry);
logContainer.scrollTop = logContainer.scrollHeight;
console.log(message);
}
// Clear log
window.clearLog = function() {
logContainer.innerHTML = '';
};
// Listen to beforeSizesUpdate event
document.addEventListener('beforeSizesUpdate', (event) => {
const elem = event.target;
const width = event.detail.width;
log(`beforeSizesUpdate: ${elem.tagName} width=${width}px`, 'event');
});
// Listen to afterSizesUpdate event
document.addEventListener('afterSizesUpdate', (event) => {
const elem = event.target;
const sizes = event.detail.sizes;
log(`afterSizesUpdate: ${elem.tagName} sizes="${sizes}"`, 'event');
});
// Update display info
function updateInfo() {
// Test 1
const img1 = document.querySelector('#container1 img');
document.getElementById('sizes1').textContent = img1.getAttribute('sizes') || 'auto';
document.getElementById('width1').textContent = img1.offsetWidth;
document.getElementById('class1').textContent = img1.classList.contains('autosized') ? 'Yes' : 'No';
// Test 2
const img2 = document.querySelector('#container2 img');
document.getElementById('sizes2').textContent = img2.getAttribute('sizes') || 'auto';
document.getElementById('width2').textContent = img2.offsetWidth;
// Test 3
const img3 = document.querySelector('#container3 img');
document.getElementById('sizes3img').textContent = img3.getAttribute('sizes') || 'auto';
document.getElementById('width3').textContent = img3.offsetWidth;
// Test 4
const img4 = document.querySelector('#container4 img');
document.getElementById('sizes4img').textContent = img4.getAttribute('sizes') || 'auto';
document.getElementById('width4').textContent = img4.offsetWidth;
// Test 5
const img5 = document.getElementById('dataSizesImg');
document.getElementById('sizes5data').textContent = img5.getAttribute('data-sizes') || 'Not set';
document.getElementById('sizes5').textContent = img5.getAttribute('sizes') || 'Not set';
}
// Update info on load and resize
window.addEventListener('load', () => {
log('Page loaded, autosizes initialized');
updateInfo();
});
window.addEventListener('resize', () => {
setTimeout(updateInfo, 150); // Wait for debounce
});
// Manual update button
window.updateAllSizes = function() {
log('Manually updating all sizes...');
autoSizes.updateAll();
setTimeout(updateInfo, 50);
};
// Toggle container width
let isFullWidth = true;
window.toggleWidth = function() {
const container1 = document.getElementById('container1');
if (isFullWidth) {
container1.style.width = '50%';
log('Container width set to 50%');
} else {
container1.style.width = '100%';
log('Container width set to 100%');
}
isFullWidth = !isFullWidth;
setTimeout(() => {
autoSizes.updateAll();
setTimeout(updateInfo, 50);
}, 50);
};
// Initial info update after a brief delay
setTimeout(updateInfo, 200);
log('Test page initialized. Resize window to see sizes recalculate.');
log('Note: Images with sizes="auto" will be updated automatically.');
</script>
</body>
</html>