diff --git a/src/core/const.js b/src/core/const.js index 569d9f9..1853e9b 100644 --- a/src/core/const.js +++ b/src/core/const.js @@ -296,7 +296,7 @@ * @type {RegExp|string} * @example `image.png` */ -export const IMAGE_TYPE = /\.(gif|jpe?g|tiff|png|svg)$/i; +export const URL_FILE_EXTENSION = /\.(\w{3,4})(?:$|\?|#)/i; /** * Regexp for data URI. diff --git a/src/core/const.js b/src/core/const.js index 569d9f9..1853e9b 100644 --- a/src/core/const.js +++ b/src/core/const.js @@ -296,7 +296,7 @@ * @type {RegExp|string} * @example `image.png` */ -export const IMAGE_TYPE = /\.(gif|jpe?g|tiff|png|svg)$/i; +export const URL_FILE_EXTENSION = /\.(\w{3,4})(?:$|\?|#)/i; /** * Regexp for data URI. diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index e8ab42a..cbf1684 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -1,5 +1,5 @@ import { - uid, getImageTypeOfUrl, decomposeDataUri, getSvgSize, + uid, getUrlFileExtension, decomposeDataUri, getSvgSize, getResolutionOfUrl, BaseTextureCache, TextureCache, } from '../utils'; import { RESOLUTION, SCALE_MODES, MIPMAP_TEXTURES, WRAP_MODES } from '../const'; @@ -399,7 +399,7 @@ // Check for subType validity const firstSubType = dataUri.subType.split('+')[0]; - imageType = getImageTypeOfUrl(`.${firstSubType}`); + imageType = getUrlFileExtension(`.${firstSubType}`); if (!imageType) { @@ -408,7 +408,7 @@ } else { - imageType = getImageTypeOfUrl(this.imageUrl); + imageType = getUrlFileExtension(this.imageUrl); if (!imageType) { diff --git a/src/core/const.js b/src/core/const.js index 569d9f9..1853e9b 100644 --- a/src/core/const.js +++ b/src/core/const.js @@ -296,7 +296,7 @@ * @type {RegExp|string} * @example `image.png` */ -export const IMAGE_TYPE = /\.(gif|jpe?g|tiff|png|svg)$/i; +export const URL_FILE_EXTENSION = /\.(\w{3,4})(?:$|\?|#)/i; /** * Regexp for data URI. diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index e8ab42a..cbf1684 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -1,5 +1,5 @@ import { - uid, getImageTypeOfUrl, decomposeDataUri, getSvgSize, + uid, getUrlFileExtension, decomposeDataUri, getSvgSize, getResolutionOfUrl, BaseTextureCache, TextureCache, } from '../utils'; import { RESOLUTION, SCALE_MODES, MIPMAP_TEXTURES, WRAP_MODES } from '../const'; @@ -399,7 +399,7 @@ // Check for subType validity const firstSubType = dataUri.subType.split('+')[0]; - imageType = getImageTypeOfUrl(`.${firstSubType}`); + imageType = getUrlFileExtension(`.${firstSubType}`); if (!imageType) { @@ -408,7 +408,7 @@ } else { - imageType = getImageTypeOfUrl(this.imageUrl); + imageType = getUrlFileExtension(this.imageUrl); if (!imageType) { diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 4413edf..0d8a7ff 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -1,4 +1,4 @@ -import { RETINA_PREFIX, DATA_URI, IMAGE_TYPE, SVG_SIZE, VERSION } from '../const'; +import { RETINA_PREFIX, DATA_URI, URL_FILE_EXTENSION, SVG_SIZE, VERSION } from '../const'; import EventEmitter from 'eventemitter3'; import pluginTarget from './pluginTarget'; @@ -129,9 +129,9 @@ * @param {string} url - the image path * @return {string|undefined} image extension */ -export function getImageTypeOfUrl(url) +export function getUrlFileExtension(url) { - const extension = IMAGE_TYPE.exec(url); + const extension = URL_FILE_EXTENSION.exec(url); if (extension) { diff --git a/src/core/const.js b/src/core/const.js index 569d9f9..1853e9b 100644 --- a/src/core/const.js +++ b/src/core/const.js @@ -296,7 +296,7 @@ * @type {RegExp|string} * @example `image.png` */ -export const IMAGE_TYPE = /\.(gif|jpe?g|tiff|png|svg)$/i; +export const URL_FILE_EXTENSION = /\.(\w{3,4})(?:$|\?|#)/i; /** * Regexp for data URI. diff --git a/src/core/textures/BaseTexture.js b/src/core/textures/BaseTexture.js index e8ab42a..cbf1684 100644 --- a/src/core/textures/BaseTexture.js +++ b/src/core/textures/BaseTexture.js @@ -1,5 +1,5 @@ import { - uid, getImageTypeOfUrl, decomposeDataUri, getSvgSize, + uid, getUrlFileExtension, decomposeDataUri, getSvgSize, getResolutionOfUrl, BaseTextureCache, TextureCache, } from '../utils'; import { RESOLUTION, SCALE_MODES, MIPMAP_TEXTURES, WRAP_MODES } from '../const'; @@ -399,7 +399,7 @@ // Check for subType validity const firstSubType = dataUri.subType.split('+')[0]; - imageType = getImageTypeOfUrl(`.${firstSubType}`); + imageType = getUrlFileExtension(`.${firstSubType}`); if (!imageType) { @@ -408,7 +408,7 @@ } else { - imageType = getImageTypeOfUrl(this.imageUrl); + imageType = getUrlFileExtension(this.imageUrl); if (!imageType) { diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 4413edf..0d8a7ff 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -1,4 +1,4 @@ -import { RETINA_PREFIX, DATA_URI, IMAGE_TYPE, SVG_SIZE, VERSION } from '../const'; +import { RETINA_PREFIX, DATA_URI, URL_FILE_EXTENSION, SVG_SIZE, VERSION } from '../const'; import EventEmitter from 'eventemitter3'; import pluginTarget from './pluginTarget'; @@ -129,9 +129,9 @@ * @param {string} url - the image path * @return {string|undefined} image extension */ -export function getImageTypeOfUrl(url) +export function getUrlFileExtension(url) { - const extension = IMAGE_TYPE.exec(url); + const extension = URL_FILE_EXTENSION.exec(url); if (extension) { diff --git a/test/core/util.js b/test/core/util.js index 983ac28..c5d7f9d 100755 --- a/test/core/util.js +++ b/test/core/util.js @@ -94,17 +94,81 @@ }); }); - describe('getImageTypeOfUrl', function () + describe('getUrlFileExtension', function () { it('should exist', function () { - expect(PIXI.utils.getImageTypeOfUrl) + expect(PIXI.utils.getUrlFileExtension) .to.be.a('function'); }); - it('should return image type of URL in lower case', function () + it('should return extension of URL in lower case', function () { - var imageType = PIXI.utils.getImageTypeOfUrl('http://foo.bar/baz.PNG'); + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG'); + + expect(imageType) + .to.equal('png'); + }); + + it('should return extension of URL when absolute', function () + { + var imageType = PIXI.utils.getUrlFileExtension('/you/baz.PNG'); + + expect(imageType) + .to.equal('png'); + }); + + it('should return extension of URL when relative', function () + { + var imageType = PIXI.utils.getUrlFileExtension('me/baz.PNG'); + + expect(imageType) + .to.equal('png'); + }); + + it('should return extension of URL when just an extension', function () + { + var imageType = PIXI.utils.getUrlFileExtension('.PNG'); + + expect(imageType) + .to.equal('png'); + }); + + it('should work with a hash on the url', function () + { + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG#derp'); + + expect(imageType) + .to.equal('png'); + }); + + it('should work with a hash path on the url', function () + { + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG#derp/this/is/a/path/me.jpg'); + + expect(imageType) + .to.equal('png'); + }); + + it('should work with a query string on the url', function () + { + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG?v=1&file=me.jpg'); + + expect(imageType) + .to.equal('png'); + }); + + it('should work with a hash and query string on the url', function () + { + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG?v=1&file=me.jpg#not-today'); + + expect(imageType) + .to.equal('png'); + }); + + it('should work with a hash path and query string on the url', function () + { + var imageType = PIXI.utils.getUrlFileExtension('http://foo.bar/baz.PNG?v=1&file=me.jpg#path/self/not-today.svg'); expect(imageType) .to.equal('png');