Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schilling <rschilling@student.tugraz.at>2015-09-10 20:45:59 +0300
committerRobert Schilling <rschilling@student.tugraz.at>2015-09-10 20:45:59 +0300
commitb8c05a069b3204bf7ff5074d1ba747bd1d65fcc2 (patch)
tree6f35abcf4280115417eb5a2c7d3b52054d5a67a8
parent0a2c7fcc648c83402bae70c598d674817a78bc84 (diff)
parent9d3344adbbfc84ef8abc96366bdfa695293cd6c0 (diff)
Merge branch 'rescue-syntax-highlighting-errors' into 'master'
Gracefully handle errors in syntax highlighting by leaving the block unformatted A bug in Rouge caused an Exception: ```undefined method `sub' for :Literal:Symbol``` That caused https://gitlab.com/embeddable-common-lisp/ecl/issues/156 to hit Error 500 and fail to display. If a failure occurs, just render the text as preformatted. Closes #2433 See merge request !1274
-rw-r--r--CHANGELOG1
-rw-r--r--lib/gitlab/markdown/syntax_highlight_filter.rb6
-rw-r--r--spec/lib/gitlab/markdown/syntax_highlight_filter_spec.rb22
3 files changed, 28 insertions, 1 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 710f6e6891c..402fe61bbce 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date.
v 8.0.0 (unreleased)
+ - Gracefully handle errors in syntax highlighting by leaving the block unformatted (Stan Hu)
- Fix URL construction for merge requests, issues, notes, and commits for relative URL config (Stan Hu)
- Fix emoji URLs in Markdown when relative_url_root is used (Stan Hu)
- Omit filename in Content-Disposition header in raw file download to avoid RFC 6266 encoding issues (Stan HU)
diff --git a/lib/gitlab/markdown/syntax_highlight_filter.rb b/lib/gitlab/markdown/syntax_highlight_filter.rb
index 86f4385753a..f9527c7286e 100644
--- a/lib/gitlab/markdown/syntax_highlight_filter.rb
+++ b/lib/gitlab/markdown/syntax_highlight_filter.rb
@@ -21,7 +21,11 @@ module Gitlab
language = node.attr('class')
code = node.text
- highlighted = block_code(code, language)
+ begin
+ highlighted = block_code(code, language)
+ rescue
+ highlighted = "<pre>#{code}</pre>"
+ end
# Replace the parent `pre` element with the entire highlighted block
node.parent.replace(highlighted)
diff --git a/spec/lib/gitlab/markdown/syntax_highlight_filter_spec.rb b/spec/lib/gitlab/markdown/syntax_highlight_filter_spec.rb
new file mode 100644
index 00000000000..ecef31853f4
--- /dev/null
+++ b/spec/lib/gitlab/markdown/syntax_highlight_filter_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+module Gitlab::Markdown
+ describe SyntaxHighlightFilter do
+ include FilterSpecHelper
+
+ let(:project) { create(:empty_project) }
+ let(:reference) { snippet.to_reference }
+
+ it 'highlights valid code blocks' do
+ result = filter('<pre><code>def fun end</code>')
+ expect(result.to_html).to eq("<pre class=\"code highlight js-syntax-highlight plaintext\"><code>def fun end</code></pre>\n")
+ end
+
+ it 'passes through invalid code blocks' do
+ allow_any_instance_of(SyntaxHighlightFilter).to receive(:block_code).and_raise(StandardError)
+
+ result = filter('<pre><code>This is a test</code></pre>')
+ expect(result.to_html).to eq('<pre>This is a test</pre>')
+ end
+ end
+end