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