Merge pull request #827 from edx/christina/course-info-links3

Rewriting of links for Course Updates and Course Handouts.
This commit is contained in:
Christina Roberts
2013-08-30 07:32:01 -07:00
14 changed files with 290 additions and 67 deletions

View File

@@ -53,6 +53,7 @@ lib_paths:
- common_static/js/vendor/sinon-1.7.1.js
- common_static/js/vendor/analytics.js
- common_static/js/test/add_ajax_prefix.js
- common_static/js/src/utility.js
# Paths to spec (test) JavaScript files
spec_paths:

View File

@@ -101,32 +101,25 @@ class @HTMLEditingDescriptor
# Show the Advanced (codemirror) Editor. Pulled out as a helper method for unit testing.
showAdvancedEditor: (visualEditor) ->
if visualEditor.isDirty()
content = @rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
content = rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
@advanced_editor.setValue(content)
@advanced_editor.setCursor(0)
@advanced_editor.refresh()
@advanced_editor.focus()
@showingVisualEditor = false
rewriteStaticLinks: (content, from, to) ->
if from == null || to == null
return content
regex = new RegExp(from, 'g')
return content.replace(regex, to)
# Show the Visual (tinyMCE) Editor. Pulled out as a helper method for unit testing.
showVisualEditor: (visualEditor) ->
# In order for isDirty() to return true ONLY if edits have been made after setting the text,
# both the startContent must be sync'ed up and the dirty flag set to false.
content = @rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url)
content = rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url)
visualEditor.setContent(content)
visualEditor.startContent = content
@focusVisualEditor(visualEditor)
@showingVisualEditor = true
initInstanceCallback: (visualEditor) =>
visualEditor.setContent(@rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url))
visualEditor.setContent(rewriteStaticLinks(@advanced_editor.getValue(), '/static/', @base_asset_url))
@focusVisualEditor(visualEditor)
focusVisualEditor: (visualEditor) =>
@@ -150,5 +143,5 @@ class @HTMLEditingDescriptor
text = @advanced_editor.getValue()
visualEditor = @getVisualEditor()
if @showingVisualEditor and visualEditor.isDirty()
text = @rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
text = rewriteStaticLinks(visualEditor.getContent({no_events: 1}), @base_asset_url, '/static/')
data: text

View File

@@ -0,0 +1,13 @@
describe('utility.rewriteStaticLinks', function () {
it('returns "content" if "from" or "to" is null', function () {
expect(rewriteStaticLinks('foo', null, 'bar')).toBe('foo');
expect(rewriteStaticLinks('foo', 'bar', null)).toBe('foo');
expect(rewriteStaticLinks('foo', null, null)).toBe('foo');
});
it('does a replace of "from" to "to"', function () {
expect(rewriteStaticLinks('<img src="/static/foo.x"/>', '/static/', 'howdy')).toBe('<img src="howdyfoo.x"/>')
});
it('returns "content" if "from" is not found', function () {
expect(rewriteStaticLinks('<img src="/static/foo.x"/>', '/statix/', 'howdy')).toBe('<img src="/static/foo.x"/>')
});
});

View File

@@ -18,3 +18,13 @@ window.isExternal = function (url) {
return true;
return false;
};
// Utility method for replacing a portion of a string.
window.rewriteStaticLinks = function(content, from, to) {
if (from === null || to === null) {
return content
}
var regex = new RegExp(from, 'g');
return content.replace(regex, to)
};

View File

@@ -52,10 +52,12 @@ lib_paths:
# Paths to source JavaScript files
src_paths:
- coffee/src
- js/src
# Paths to spec (test) JavaScript files
spec_paths:
- coffee/spec
- js/spec
# Regular expressions used to exclude *.js files from
# appearing in the test runner page.