diff options
author | Vincent Petry <pvince81@owncloud.com> | 2014-04-04 11:32:07 +0200 |
---|---|---|
committer | Vincent Petry <pvince81@owncloud.com> | 2014-04-04 11:34:26 +0200 |
commit | a4eafca77f86ab2630bec698c3302c93daddb991 (patch) | |
tree | d19954cea3761efe945e1cee0816c69fc555e94b /core | |
parent | 8a10c44eb33d45e2deba7d72b30e509fa332fb24 (diff) | |
download | nextcloud-server-a4eafca77f86ab2630bec698c3302c93daddb991.tar.gz nextcloud-server-a4eafca77f86ab2630bec698c3302c93daddb991.zip |
Moved code to replace svg with png to OC.Util
- Moved code that replaces the "svg" extension for the given file to
core as OC.Util.replaceSVGIcon.
- Added unit test for OC.Util.replaceSVGIcon
- Moved "replaceSVG" to OC.Util.replaceSVG and deprecated the global
"replaceSVG" function.
- Added alias for SVGSupport() as OC.Util.hasSVGSupport() (for now)
Diffstat (limited to 'core')
-rw-r--r-- | core/js/js.js | 105 | ||||
-rw-r--r-- | core/js/tests/specs/coreSpec.js | 26 |
2 files changed, 99 insertions, 32 deletions
diff --git a/core/js/js.js b/core/js/js.js index 9a3b2ee6a5d..1cbb9636dbe 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -498,7 +498,7 @@ var OC={ }); } if(!SVGSupport()) { - replaceSVG(); + OC.Util.replaceSVG(); } }).show(); }, 'html'); @@ -785,7 +785,7 @@ SVGSupport.checkMimeType=function(){ } }); if(headers["content-type"]!=='image/svg+xml'){ - replaceSVG(); + OC.Util.replaceSVG(); SVGSupport.checkMimeType.correct=false; } } @@ -793,35 +793,10 @@ SVGSupport.checkMimeType=function(){ }; SVGSupport.checkMimeType.correct=true; -//replace all svg images with png for browser compatibility -function replaceSVG(){ - $('img.svg').each(function(index,element){ - element=$(element); - var src=element.attr('src'); - element.attr('src',src.substr(0,src.length-3)+'png'); - }); - $('.svg').each(function(index,element){ - element=$(element); - var background=element.css('background-image'); - if(background){ - var i=background.lastIndexOf('.svg'); - if(i>=0){ - background=background.substr(0,i)+'.png'+background.substr(i+4); - element.css('background-image',background); - } - } - element.find('*').each(function(index,element) { - element=$(element); - var background=element.css('background-image'); - if(background){ - var i=background.lastIndexOf('.svg'); - if(i>=0){ - background=background.substr(0,i)+'.png'+background.substr(i+4); - element.css('background-image',background); - } - } - }); - }); +// replace all svg images with png for browser compatibility +// @deprecated use OC.Util.replaceSVG instead +function replaceSVG($el){ + return OC.Util.replaceSVG($el); } /** @@ -900,7 +875,7 @@ function initCore() { } if(!SVGSupport()){ //replace all svg images with png images for browser that dont support svg - replaceSVG(); + OC.Util.replaceSVG(); }else{ SVGSupport.checkMimeType(); } @@ -1134,6 +1109,72 @@ function relative_modified_date(timestamp) { else { return t('core','years ago'); } } +OC.Util = { + /** + * Returns whether the browser supports SVG + * + * @return true if the browser supports SVG, false otherwise + */ + // TODO: replace with original function + hasSVGSupport: SVGSupport, + /** + * If SVG is not supported, replaces the given icon's extension + * from ".svg" to ".png". + * If SVG is supported, return the image path as is. + * + * @param file image path with svg extension + * @return fixed image path with png extension if SVG is not + * supported + */ + replaceSVGIcon: function(file) { + if (!OC.Util.hasSVGSupport()) { + var i = file.lastIndexOf('.svg'); + if (i >= 0) { + file = file.substr(0, i) + '.png' + file.substr(i+4); + } + } + return file; + }, + /** + * Replace SVG images in all elements that have the "svg" class set + * with PNG images. + * + * @param $el root element from which to search, defaults to $('body') + */ + replaceSVG: function($el) { + if (!$el) { + $el = $('body'); + } + $el.find('img.svg').each(function(index,element){ + element=$(element); + var src=element.attr('src'); + element.attr('src',src.substr(0, src.length-3) + 'png'); + }); + $el.find('.svg').each(function(index,element){ + element = $(element); + var background = element.css('background-image'); + if (background){ + var i = background.lastIndexOf('.svg'); + if (i >= 0){ + background = background.substr(0,i) + '.png' + background.substr(i + 4); + element.css('background-image', background); + } + } + element.find('*').each(function(index, element) { + element = $(element); + var background = element.css('background-image'); + if (background) { + var i = background.lastIndexOf('.svg'); + if(i >= 0){ + background = background.substr(0,i) + '.png' + background.substr(i + 4); + element.css('background-image', background); + } + } + }); + }); + } +}; + /** * get a variable by name * @param string name diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 94a397b7892..89056807788 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -448,5 +448,31 @@ describe('Core base tests', function() { expect($navigation.is(':visible')).toEqual(true); }); }); + describe('SVG extension replacement', function() { + var svgSupportStub; + + beforeEach(function() { + svgSupportStub = sinon.stub(OC.Util, 'hasSVGSupport'); + }); + afterEach(function() { + svgSupportStub.restore(); + }); + it('does not replace svg extension with png when SVG is supported', function() { + svgSupportStub.returns(true); + expect( + OC.Util.replaceSVGIcon('/path/to/myicon.svg?someargs=1') + ).toEqual( + '/path/to/myicon.svg?someargs=1' + ); + }); + it('replaces svg extension with png when SVG not supported', function() { + svgSupportStub.returns(false); + expect( + OC.Util.replaceSVGIcon('/path/to/myicon.svg?someargs=1') + ).toEqual( + '/path/to/myicon.png?someargs=1' + ); + }); + }); }); |