Skip to content

Commit

Permalink
πŸ”” #175 #294 add srcset <picture> support
Browse files Browse the repository at this point in the history
- use currentSrc
- emit progress with <picture>
- βœ… add <picture> test
- βœ… test srcset
- add test css
- remove bower_components
  • Loading branch information
desandro committed Feb 9, 2022
1 parent b059c1f commit 1915f05
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 5 deletions.
9 changes: 6 additions & 3 deletions imagesloaded.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ ImagesLoaded.prototype.progress = function( image, elem, message ) {
}

if ( this.options.debug && console ) {
console.log( 'progress: ' + message, image, elem );
console.log( `progress: ${message}`, image, elem );
}
};

Expand Down Expand Up @@ -230,7 +230,7 @@ LoadingImage.prototype.check = function() {
// bind to image as well for Firefox. #191
this.img.addEventListener( 'load', this );
this.img.addEventListener( 'error', this );
this.proxyImage.src = this.img.src;
this.proxyImage.src = this.img.currentSrc || this.img.src;
};

LoadingImage.prototype.getIsImageComplete = function() {
Expand All @@ -241,7 +241,10 @@ LoadingImage.prototype.getIsImageComplete = function() {

LoadingImage.prototype.confirm = function( isLoaded, message ) {
this.isLoaded = isLoaded;
this.emitEvent( 'progress', [ this, this.img, message ] );
let { parentNode } = this.img;
// emit progress with parent <picture> or self <img>
let elem = parentNode.nodeName == 'PICTURE' ? parentNode : this.img;
this.emitEvent( 'progress', [ this, elem, message ] );
};

// ----- events ----- //
Expand Down
2 changes: 1 addition & 1 deletion sandbox/background/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>background</h1>

<div class="box blue"></div>

<script src="../../bower_components/ev-emitter/ev-emitter.js"></script>
<script src="../../node_modules/ev-emitter/ev-emitter.js"></script>
<script src="../../imagesloaded.js"></script>
<script>

Expand Down
19 changes: 19 additions & 0 deletions sandbox/picture.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>picture</title>

</head>
<body>

<picture>
<source srcset="https://picsum.photos/id/103/1200/900" media="(min-width: 1200px)">
<source srcset="https://picsum.photos/id/103/800/600" media="(min-width: 800px)">
<img src="https://picsum.photos/id/103/400/300">
</picture>

</body>
</html>
2 changes: 1 addition & 1 deletion sandbox/progress/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ <h1>progress</h1>
</div>
<div id="image-container"></div>

<script src="../../bower_components/ev-emitter/ev-emitter.js"></script>
<script src="../../node_modules/ev-emitter/ev-emitter.js"></script>
<script src="../../imagesloaded.js"></script>
<script src="progress.js"></script>

Expand Down
10 changes: 10 additions & 0 deletions test/css/tests.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
body {
font-family: sans-serif;
}

img {
display: inline-block;
max-width: 240px;
}

#qunit {
max-width: 600px;
right: 0;
left: auto;
}

/* ---- backgrounds ---- */

.bg-box {
Expand Down
64 changes: 64 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<script src="unit/jquery-fail.js"></script>
<script src="unit/non-element.js"></script>
<script src="unit/background.js"></script>
<script src="unit/picture.js"></script>
<script src="unit/srcset.js"></script>

</head>
<body>
Expand Down Expand Up @@ -100,5 +102,67 @@ <h2>background</h2>
</div>
</div>

<h2>picture</h2>

<div id="picture-list">
<picture>
<source srcset="https://picsum.photos/id/1036/1200/900" media="(min-width: 1200px)">
<source srcset="https://picsum.photos/id/1036/800/600" media="(min-width: 800px)">
<img src="https://picsum.photos/id/1036/400/300">
</picture>
<picture>
<source srcset="https://picsum.photos/id/103/1200/900" media="(min-width: 1200px)">
<source srcset="https://picsum.photos/id/103/800/600" media="(min-width: 800px)">
<img src="https://picsum.photos/id/103/400/300">
</picture>
<picture>
<source srcset="https://picsum.photos/id/1080/900/1200" media="(min-width: 1200px)">
<source srcset="https://picsum.photos/id/1080/600/800" media="(min-width: 800px)">
<img src="https://picsum.photos/id/1080/300/400">
</picture>
</div>

<h2>srcset</h2>

<div id="srcset">
<img
srcset="
https://picsum.photos/id/1074/1200/900 1200w,
https://picsum.photos/id/1074/800/600 800w,
https://picsum.photos/id/1074/400/300 400w
"
sizes="
(min-width: 1200px) 240px,
(min-width: 800px) 240px,
240px,
"
>
<img
srcset="
https://picsum.photos/id/159/900/1200 900w,
https://picsum.photos/id/159/600/800 600w,
https://picsum.photos/id/159/300/400 300w
"
sizes="
(min-width: 1200px) 240px,
(min-width: 800px) 240px,
240px,
"
>
<img
srcset="
https://picsum.photos/id/133/1200/900 1200w,
https://picsum.photos/id/133/800/600 800w,
https://picsum.photos/id/133/400/300 400w
"
sizes="
(min-width: 1200px) 240px,
(min-width: 800px) 240px,
240px,
"
>
</div>


</body>
</html>
17 changes: 17 additions & 0 deletions test/unit/picture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
QUnit.test( 'picture', function( assert ) {

let done = assert.async( 4 );

let imgLoad0 = imagesLoaded( '#picture-list', function() {
assert.ok( true, 'callback triggered on #picture-list' );
done();
} );
assert.equal( imgLoad0.images.length, 3, '3 images on #picture-list' );

imgLoad0.on( 'progress', function( instance, image, element ) {
assert.ok( element.nodeName == 'PICTURE', 'progress; element is picture' );
assert.ok( image.isLoaded, 'progress; image.isLoaded' );
done();
} );

} );
17 changes: 17 additions & 0 deletions test/unit/srcset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
QUnit.test( 'srcset', function( assert ) {

let done = assert.async( 4 );

let imgLoad0 = imagesLoaded( '#srcset', function() {
assert.ok( true, 'callback triggered on #srcset' );
done();
} );
assert.equal( imgLoad0.images.length, 3, '3 images on #srcset' );

imgLoad0.on( 'progress', function( instance, image, element ) {
assert.ok( element.nodeName == 'IMG', 'progress; element is img' );
assert.ok( image.isLoaded, 'progress; image.isLoaded' );
done();
} );

} );

0 comments on commit 1915f05

Please sign in to comment.