Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding customisation to interpolation selector #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Imager.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@
this.widthsMap = {};
this.refreshPixelRatio();
this.widthInterpolator = opts.widthInterpolator || returnFn;
this.widthInterpolationSelector = !opts.widthInterpolationSelector ? new RegExp('{width}', 'g') : new RegExp(opts.widthInterpolationSelector, 'g');
this.pixelRatioInterpolationSelector = !opts.pixelRatioInterpolationSelector ? new RegExp('{pixel_ratio}', 'g') : new RegExp(opts.pixelRatioInterpolationSelector, 'g');

// Needed as IE8 adds a default `width`/`height` attribute…
this.gif.removeAttribute('height');
Expand Down Expand Up @@ -360,8 +362,8 @@

Imager.prototype.changeImageSrcToUseNewImageDimensions = function (src, selectedWidth) {
return src
.replace(/{width}/g, Imager.transforms.width(selectedWidth, this.widthsMap))
.replace(/{pixel_ratio}/g, Imager.transforms.pixelRatio(this.devicePixelRatio));
.replace(this.widthInterpolationSelector, Imager.transforms.width(selectedWidth, this.widthsMap))
.replace(this.pixelRatioInterpolationSelector, Imager.transforms.pixelRatio(this.devicePixelRatio));
};

Imager.getPixelRatio = function getPixelRatio(context) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/custom-matcher.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="delayed-image-load" data-src="base/test/fixtures/media/C-xxwidthxx.jpg"></div>
<div class="delayed-image-load" data-src="base/test/fixtures/media/B-xxwidthxx.jpg" data-width="640"></div>
<div class="delayed-image-load" data-src="base/test/fixtures/media-xxwidthxx/fillmurray.jpg" data-width="320"></div>
16 changes: 16 additions & 0 deletions test/unit/html-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ describe('Imager.js HTML data-* API', function () {
cleanFixtures(fixtures);
});

describe('customising matcher for interpolation', function () {
it('should successfully resolve custom match expression', function (done) {
fixtures = loadFixtures('custom-matcher');
var imgr = new Imager({availableWidths: [320, 640], widthInterpolationSelector: 'xxwidthxx'});

imgr.ready(function () {
var src = applyEach(imgr.divs, function (el) {
return el.getAttribute('src');
});

expect(src).to.eql(['base/test/fixtures/media/C-640.jpg', 'base/test/fixtures/media/B-640.jpg', 'base/test/fixtures/media-320/fillmurray.jpg']);
done();
});
});
});

describe('handling {width} in data-src', function () {
it('should not use RegExp anymore', function (done) {
fixtures = loadFixtures('data-src-old');
Expand Down