Skip to content

Commit

Permalink
Fix: srcset parsing issue (#337)
Browse files Browse the repository at this point in the history
* Fix: srcset parsing

* Add: srcset
  • Loading branch information
eight04 authored Oct 5, 2023
1 parent 9a71160 commit 671e21f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"private": true,
"dependencies": {
"image-extensions": "^1.1.0",
"srcset": "^5.0.0",
"version-compare": "^1.4.0"
}
}
21 changes: 4 additions & 17 deletions src/lib/image-util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {parseSrcset} from "srcset";
import {pref} from "./pref.js";

let SRC_PROP = [];
Expand All @@ -17,25 +18,11 @@ function update() {
}

function getSrcFromSrcset(set) {
const rules = set.split(/\s*,\s*/).map(rule =>
rule.split(/\s+/).reduce((result, token) => {
if (token) {
let match;
if ((match = token.match(/^(\d+)[wx]$/))) {
result.scale = +match[1];
} else {
result.url = token;
}
}
return result;
}, {
scale: 1
})
);

const rules = parseSrcset(set);
let maxRule;
for (const rule of rules) {
if (!maxRule || rule.scale > maxRule.scale) {
// FIXME: what if the rules have both density and width?
if (!maxRule || (rule.density || rule.width || 0) > (maxRule.density || maxRule.width || 0)) {
maxRule = rule;
}
}
Expand Down

0 comments on commit 671e21f

Please sign in to comment.