Commit 0d1a1065 by zubiar-arbi

update rewriteStaticLinks utility function(modifiy only relative urls)

STUD-674
parent 3fc461f4
...@@ -10,4 +10,9 @@ describe('utility.rewriteStaticLinks', function () { ...@@ -10,4 +10,9 @@ describe('utility.rewriteStaticLinks', function () {
it('returns "content" if "from" is not found', function () { it('returns "content" if "from" is not found', function () {
expect(rewriteStaticLinks('<img src="/static/foo.x"/>', '/statix/', 'howdy')).toBe('<img src="/static/foo.x"/>') expect(rewriteStaticLinks('<img src="/static/foo.x"/>', '/statix/', 'howdy')).toBe('<img src="/static/foo.x"/>')
}); });
it('does not replace of "from" to "to" if "from" is part of absolute url', function () {
expect(
rewriteStaticLinks('<img src="http://www.mysite.org/static/foo.x"/>', '/static/', 'howdy')
).toBe('<img src="http://www.mysite.org/static/foo.x"/>')
});
}); });
...@@ -24,7 +24,18 @@ window.rewriteStaticLinks = function(content, from, to) { ...@@ -24,7 +24,18 @@ window.rewriteStaticLinks = function(content, from, to) {
if (from === null || to === null) { if (from === null || to === null) {
return content; return content;
} }
// replace only relative urls
var regex = new RegExp(from, 'g'); function replacer(match){
return content.replace(regex, to); 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment