From 0d1a10659c1248db0ab468e2481e86560bbd2aaa Mon Sep 17 00:00:00 2001 From: zubiar-arbi Date: Thu, 26 Dec 2013 14:35:54 +0500 Subject: [PATCH] update rewriteStaticLinks utility function(modifiy only relative urls) STUD-674 --- common/static/js/spec/utility_spec.js | 5 +++++ common/static/js/src/utility.js | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/common/static/js/spec/utility_spec.js b/common/static/js/spec/utility_spec.js index 6e19798026..c44d36e893 100644 --- a/common/static/js/spec/utility_spec.js +++ b/common/static/js/spec/utility_spec.js @@ -10,4 +10,9 @@ describe('utility.rewriteStaticLinks', function () { it('returns "content" if "from" is not found', function () { expect(rewriteStaticLinks('', '/statix/', 'howdy')).toBe('') }); + it('does not replace of "from" to "to" if "from" is part of absolute url', function () { + expect( + rewriteStaticLinks('', '/static/', 'howdy') + ).toBe('') + }); }); diff --git a/common/static/js/src/utility.js b/common/static/js/src/utility.js index d3ed461b38..7b5dd5a6bb 100644 --- a/common/static/js/src/utility.js +++ b/common/static/js/src/utility.js @@ -24,7 +24,18 @@ window.rewriteStaticLinks = function(content, from, to) { if (from === null || to === null) { return content; } - - var regex = new RegExp(from, 'g'); - return content.replace(regex, to); -}; + // replace only relative urls + function replacer(match){ + if (match === from){ + return to; + } + else { + return match; + } + } + // change all relative urls only which may be embedded inside other tags in content. + // handle http and https + // note: add other protocols here + var regex = new RegExp("(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}([-a-zA-Z0-9@:%_\+.~#?&//=]*))?"+from, 'g'); + return content.replace(regex, replacer); +}; \ No newline at end of file