Skip to content

Commit 3233f0f

Browse files
committed
fix: handle classIgnore for img with no class attribute
- default classes to [] even when checking for attributes - update test
1 parent e1248b4 commit 3233f0f

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = function (options) {
2020
return function posthtmlWebp (tree) {
2121
tree.match([{ tag: 'img' }, { tag: 'amp-img' }], function (imgNode) {
2222
if (imgNode.skip) return imgNode
23-
var classes = imgNode.attrs && imgNode.attrs.class && (imgNode.attrs.class.split(' ') || [])
23+
var classes = (imgNode.attrs && imgNode.attrs.class && imgNode.attrs.class.split(' ')) || []
2424
var extension = imgNode.attrs.src.split('.').pop()
2525
var isIgnoredByClass = options.classIgnore.filter(className => classes.includes(className)).length > 0
2626
var isIgnoredByExtension = options.extensionIgnore.filter(fileExtension => fileExtension === extension).length > 0

test/fixtures/ignore.expected.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html>
3-
<body>
4-
<img class="ignore-class" srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png">
5-
</body>
3+
<body>
4+
<img class="ignore-class" srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png">
5+
<picture><source type="image/webp" srcset="photo.png.webp"><img src="photo.png"></picture>
6+
</body>
67
</html>

test/fixtures/ignore.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html>
3-
<body>
4-
<img class="ignore-class" srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png">
5-
</body>
3+
<body>
4+
<img class="ignore-class" srcset="photo-lg.jpg 1000w, photo-md.jpg 500w, photo-sm.jpg 250w" src="photo.png">
5+
<img src="photo.png">
6+
</body>
67
</html>

0 commit comments

Comments
 (0)