Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
0d1a1065
Commit
0d1a1065
authored
Dec 26, 2013
by
zubiar-arbi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update rewriteStaticLinks utility function(modifiy only relative urls)
STUD-674
parent
3fc461f4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
4 deletions
+21
-4
common/static/js/spec/utility_spec.js
+5
-0
common/static/js/src/utility.js
+16
-4
No files found.
common/static/js/spec/utility_spec.js
View file @
0d1a1065
...
@@ -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"/>'
)
});
});
});
common/static/js/src/utility.js
View file @
0d1a1065
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment