Skip to content

Commit 22a5d3e

Browse files
committed
improved support of native srcset attribute; removed support of "h" suffix
1 parent a6d80a0 commit 22a5d3e

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

src/jquery.lazyloadxt.srcset.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
'use strict';
66

77
var options = $.lazyLoadXT,
8+
documentElement = document.documentElement,
89
srcsetSupport = (function () {
910
return 'srcset' in (new Image());
1011
})(),
11-
reUrl = /^\s*(\S*)/,
12+
reUrl = /^\s*(\S+)/,
1213
reWidth = /\S\s+(\d+)w/,
13-
reHeight = /\S\s+(\d+)h/,
1414
reDpr = /\S\s+([\d\.]+)x/,
1515
infty = [0, Infinity],
1616
one = [0, 1],
@@ -22,7 +22,6 @@
2222
},
2323
viewport = {
2424
w: 0,
25-
h: 0,
2625
x: 0
2726
},
2827
property,
@@ -49,18 +48,21 @@
4948
return item[property] === limit;
5049
}
5150

51+
function splitSrcset(srcset) {
52+
return srcset.replace(/^\s+|\s+$/g, '').replace(/(\s+[\d\.]+[wx]),\s*|\s*,\s+/g, '$1 @,@ ').split(' @,@ ');
53+
}
54+
5255
function parseSrcset($el) {
5356
var srcset = $el.attr(options.srcsetAttr);
5457

5558
if (!srcset) {
5659
return false;
5760
}
5861

59-
var list = $.map(srcset.replace(/(\s[\d.]+[whx]),/g, '$1 @,@ ').split(' @,@ '), function (item) {
62+
var list = $.map(splitSrcset(srcset), function (item) {
6063
return {
6164
url: reUrl.exec(item)[1],
6265
w: parseFloat((reWidth.exec(item) || infty)[1]),
63-
h: parseFloat((reHeight.exec(item) || infty)[1]),
6466
x: parseFloat((reDpr.exec(item) || one)[1])
6567
};
6668
});
@@ -69,26 +71,22 @@
6971
return false;
7072
}
7173

72-
var documentElement = document.documentElement,
73-
whx,
74-
src;
75-
7674
viewport = {
7775
w: window.innerWidth || documentElement.clientWidth,
78-
h: window.innerHeight || documentElement.clientHeight,
7976
x: window.devicePixelRatio || 1
8077
};
8178

82-
// Notice for DOMtastic users: currently $.grep method is not implemented in DOMtastic
79+
var wx,
80+
src;
8381

84-
for (whx in viewport) {
85-
property = whx;
82+
for (wx in viewport) {
83+
property = wx;
8684
limit = mathFilter(list, 'max');
8785
list = $.grep(list, compareMax);
8886
}
8987

90-
for (whx in viewport) {
91-
property = whx;
88+
for (wx in viewport) {
89+
property = wx;
9290
limit = mathFilter(list, 'min');
9391
list = $.grep(list, compareMin);
9492
}
@@ -106,7 +104,16 @@
106104
var srcset = $el.attr(options.srcsetAttr);
107105

108106
if (srcset) {
109-
if (!options.srcsetExtended && srcsetSupport) {
107+
if (srcsetSupport) {
108+
if (options.srcsetExtended) {
109+
srcset = $.map(splitSrcset(srcset), function (item) {
110+
var i = item.indexOf(' ');
111+
if (i < 0) {
112+
i = item.length;
113+
}
114+
return ($el.attr(options.srcsetBaseAttr) || '') + item.substr(0, i) + ($el.attr(options.srcsetExtAttr) || '') + item.substr(i);
115+
}).join(', ');
116+
}
110117
$el.attr('srcset', srcset);
111118
} else {
112119
$el.lazyLoadXT.srcAttr = parseSrcset;

0 commit comments

Comments
 (0)