update rewriteStaticLinks utility function(modifiy only relative urls)

STUD-674
This commit is contained in:
zubiar-arbi
2013-12-26 14:35:54 +05:00
parent 3fc461f492
commit 0d1a10659c
2 changed files with 20 additions and 4 deletions

View File

@@ -10,4 +10,9 @@ describe('utility.rewriteStaticLinks', 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"/>')
});
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"/>')
});
});

View File

@@ -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);
};