Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Avoid triggering requests for unset <img> srcs #92

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ function fixOne(el) {
if (!ofi.img) {
ofi.img = new Image(el.width, el.height);
ofi.img.srcset = nativeGetAttribute.call(el, `data-ofi-srcset`) || el.srcset;
ofi.img.src = nativeGetAttribute.call(el, `data-ofi-src`) || el.src;
const src = nativeGetAttribute.call(el, `data-ofi-src`) || el.src;
if (src) {
ofi.img.src = src;
}

// preserve for any future cloneNode calls
// https://github.com/bfred-it/object-fit-images/issues/53
Expand All @@ -114,8 +117,10 @@ function fixOne(el) {
}

polyfillCurrentSrc(ofi.img);

el.style.backgroundImage = `url("${(ofi.img.currentSrc || ofi.img.src).replace(/"/g, '\\"')}")`;
const src = ofi.img.currentSrc || ofi.img.src;
if (src) {
el.style.backgroundImage = `url("${(ofi.img.currentSrc || ofi.img.src).replace(/"/g, '\\"')}")`;
}
el.style.backgroundPosition = style['object-position'] || 'center';
el.style.backgroundRepeat = 'no-repeat';
el.style.backgroundOrigin = 'content-box';
Expand Down