update rewriteStaticLinks utility function(modifiy only relative urls)
STUD-674
This commit is contained in:
@@ -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"/>')
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
Reference in New Issue
Block a user