Skip to content

Commit

Permalink
Return null for external URLs that contain querystrings
Browse files Browse the repository at this point in the history
Photon doesn't support them.

Closes #4.

Squashed commit of the following:

commit 7a5e4049d05e865b9daa280df80369b8219a0c77
Author: Ben Lowery <ben.lowery@automattic.com>
Date:   Mon Feb 1 10:42:36 2016 -0500

    Return null for external URLs that contain querystrings

commit b7fd525f82d19a8208d2ff7e293c7fccebcd7b12
Author: Ben Lowery <ben.lowery@automattic.com>
Date:   Wed Apr 15 14:28:59 2015 -0400

    Don't attempt to photon URLs that use a querystring.

    Photon doesn't support them.
  • Loading branch information
blowery authored and sirreal committed Feb 20, 2019
1 parent 0e5360a commit e17c5ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/photon.js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ function photon (imageUrl, opts) {
params.pathname = parsedUrl.pathname;
params.hostname = parsedUrl.hostname;
} else {
// Photon does not support URLs with a querystring component
if (parsedUrl.search) {
return null;
}
params.pathname = url.format( parsedUrl ).substring(1);
params.hostname = serverFromPathname( params.pathname );
}
Expand Down Expand Up @@ -108,4 +112,3 @@ function serverFromPathname( pathname ) {
debug('determined server "%s" to use with "%s"', server, pathname);
return server + '.wp.com';
}

12 changes: 6 additions & 6 deletions packages/photon.js/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ describe('photon()', function () {
var alternateUrl = 'https://i1.wp.com/www.gravatar.com/avatar/693307b4e0cb9366f34862c9dfacd7fc';

assertHostedOnPhoton(photonedUrl);
assert.notStrictEqual(alternateUrl, photonedUrl);
assert.strictEqual(alternateUrl, photon(alternateUrl));
assert.notStrictEqual(photonedUrl, alternateUrl);
assert.strictEqual(photon(alternateUrl), alternateUrl);
});

it('should handle photoning a photoned url', function() {
var url = photon('http://example.com/image.png');
assert.strictEqual(url, photon(url));
assert.strictEqual(photon(url), url);
});

it('should add width parameters if specified', function() {
Expand All @@ -60,10 +60,10 @@ describe('photon()', function () {
assertQuery(photonedUrl, { 'w':'50' });
});

it('should encode query args for non-photon hosts', function() {
var photonedUrl = photon('http://example.com/image.png?foo=bar');
it('should return null for URLs with querystrings from non-photon hosts', function() {
var url = 'http://example.com/image.png?foo=bar';

assertPathname(photonedUrl, '/example.com/image.png%3Ffoo=bar');
assert.strictEqual(photon(url), null);
});

it('should handle protocolless URLs', function() {
Expand Down

0 comments on commit e17c5ad

Please sign in to comment.