2019-07-25 05:24:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-23 04:21:11 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'erb'
|
|
|
|
|
|
|
|
# This feature spec is intended to be a comprehensive exercising of all of
|
|
|
|
# GitLab's non-standard Markdown parsing and the integration thereof.
|
|
|
|
#
|
|
|
|
# These tests should be very high-level. Anything low-level belongs in the specs
|
|
|
|
# for the corresponding HTML::Pipeline filter or helper method.
|
|
|
|
#
|
|
|
|
# The idea is to pass a Markdown document through our entire processing stack.
|
|
|
|
#
|
|
|
|
# The process looks like this:
|
|
|
|
#
|
|
|
|
# Raw Markdown
|
|
|
|
# -> `markdown` helper
|
2019-01-31 08:58:23 +00:00
|
|
|
# -> CommonMark::Render::GitlabHTML converts Markdown to HTML
|
2015-04-23 04:21:11 +00:00
|
|
|
# -> Post-process HTML
|
2015-08-13 02:45:16 +00:00
|
|
|
# -> `gfm` helper
|
2015-04-23 04:21:11 +00:00
|
|
|
# -> HTML::Pipeline
|
2015-07-23 01:22:57 +00:00
|
|
|
# -> SanitizationFilter
|
|
|
|
# -> Other filters, depending on pipeline
|
2015-04-23 04:21:11 +00:00
|
|
|
# -> `html_safe`
|
|
|
|
# -> Template
|
|
|
|
#
|
|
|
|
# See the MarkdownFeature class for setup details.
|
|
|
|
|
2020-06-16 18:09:01 +00:00
|
|
|
RSpec.describe 'GitLab Markdown', :aggregate_failures do
|
2015-04-23 04:21:11 +00:00
|
|
|
include Capybara::Node::Matchers
|
2017-04-03 09:55:15 +00:00
|
|
|
include MarkupHelper
|
2015-07-23 01:22:57 +00:00
|
|
|
include MarkdownMatchers
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2015-07-23 01:22:57 +00:00
|
|
|
# Sometimes it can be useful to see the parsed output of the Markdown document
|
|
|
|
# for debugging. Call this method to write the output to
|
|
|
|
# `tmp/capybara/<filename>.html`.
|
|
|
|
def write_markdown(filename = 'markdown_spec')
|
|
|
|
File.open(Rails.root.join("tmp/capybara/#{filename}.html"), 'w') do |file|
|
|
|
|
file.puts @html
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2015-07-29 03:27:08 +00:00
|
|
|
def doc(html = @html)
|
2016-04-01 18:03:39 +00:00
|
|
|
@doc ||= Nokogiri::HTML::DocumentFragment.parse(html)
|
2015-07-29 03:27:08 +00:00
|
|
|
end
|
|
|
|
|
2015-07-23 01:22:57 +00:00
|
|
|
# Shared behavior that all pipelines should exhibit
|
|
|
|
shared_examples 'all pipelines' do
|
2018-06-14 08:30:16 +00:00
|
|
|
it 'includes extensions' do
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'does not parse emphasis inside of words' do
|
2015-07-23 01:22:57 +00:00
|
|
|
expect(doc.to_html).not_to match('foo<em>bar</em>baz')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'parses table Markdown' do
|
|
|
|
expect(doc).to have_selector('th:contains("Header")')
|
|
|
|
expect(doc).to have_selector('th:contains("Row")')
|
|
|
|
expect(doc).to have_selector('th:contains("Example")')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'allows Markdown in tables' do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.at_css('td:contains("Baz")').children.to_html)
|
|
|
|
.to eq '<strong>Baz</strong>'
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'parses fenced code blocks' do
|
|
|
|
expect(doc).to have_selector('pre.code.highlight.js-syntax-highlight.c')
|
|
|
|
expect(doc).to have_selector('pre.code.highlight.js-syntax-highlight.python')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'parses mermaid code block' do
|
|
|
|
expect(doc).to have_selector('pre[lang=mermaid] > code.js-render-mermaid')
|
2017-11-22 03:12:04 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'parses strikethroughs' do
|
2015-07-21 02:22:17 +00:00
|
|
|
expect(doc).to have_selector(%{del:contains("and this text doesn't")})
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
it 'includes SanitizationFilter' do
|
|
|
|
aggregate_failures 'permits b elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('b:contains("b tag")')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits em elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('em:contains("em tag")')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits code elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('code:contains("code tag")')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits kbd elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('kbd:contains("s")')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits strike elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('strike:contains(Emoji)')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits img elements' do
|
2017-07-24 17:36:52 +00:00
|
|
|
expect(doc).to have_selector('img[data-src*="smile.png"]')
|
2015-07-21 03:57:26 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits br elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('br')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits hr elements' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).to have_selector('hr')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits span elements' do
|
2015-07-21 02:22:17 +00:00
|
|
|
expect(doc).to have_selector('span:contains("span tag")')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits details elements' do
|
2016-09-28 11:46:11 +00:00
|
|
|
expect(doc).to have_selector('details:contains("Hiding the details")')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'permits summary elements' do
|
2016-09-28 11:46:11 +00:00
|
|
|
expect(doc).to have_selector('details summary:contains("collapsible")')
|
|
|
|
end
|
|
|
|
|
2018-06-14 08:30:16 +00:00
|
|
|
aggregate_failures 'permits align attribute in th elements' do
|
|
|
|
expect(doc.at_css('th:contains("Header")')['align']).to eq 'center'
|
|
|
|
expect(doc.at_css('th:contains("Row")')['align']).to eq 'right'
|
|
|
|
expect(doc.at_css('th:contains("Example")')['align']).to eq 'left'
|
2015-07-21 03:57:26 +00:00
|
|
|
end
|
2015-07-21 02:31:05 +00:00
|
|
|
|
2018-06-14 08:30:16 +00:00
|
|
|
aggregate_failures 'permits align attribute in td elements' do
|
|
|
|
expect(doc.at_css('td:contains("Foo")')['align']).to eq 'center'
|
|
|
|
expect(doc.at_css('td:contains("Bar")')['align']).to eq 'right'
|
|
|
|
expect(doc.at_css('td:contains("Baz")')['align']).to eq 'left'
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate_failures 'permits superscript elements' do
|
|
|
|
expect(doc).to have_selector('sup', count: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate_failures 'permits subscript elements' do
|
|
|
|
expect(doc).to have_selector('sub', count: 3)
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'removes `rel` attribute from links' do
|
2015-07-21 03:57:26 +00:00
|
|
|
expect(doc).not_to have_selector('a[rel="bookmark"]')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures "removes `href` from `a` elements if it's fishy" do
|
2015-07-21 02:22:17 +00:00
|
|
|
expect(doc).not_to have_selector('a[href*="javascript"]')
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Escaping' do
|
|
|
|
it 'escapes non-tag angle brackets' do
|
2015-07-21 03:57:26 +00:00
|
|
|
table = doc.css('table').last.at_css('tbody')
|
2015-04-23 04:21:11 +00:00
|
|
|
expect(table.at_xpath('.//tr[1]/td[3]').inner_html).to eq '1 < 3 & 5'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-06 21:21:19 +00:00
|
|
|
describe 'Edge Cases' do
|
|
|
|
it 'allows markup inside link elements' do
|
2015-07-21 02:31:05 +00:00
|
|
|
aggregate_failures do
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.at_css('a[href="#link-emphasis"]').to_html)
|
|
|
|
.to eq %{<a href="#link-emphasis"><em>text</em></a>}
|
2015-05-06 21:21:19 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.at_css('a[href="#link-strong"]').to_html)
|
|
|
|
.to eq %{<a href="#link-strong"><strong>text</strong></a>}
|
2015-05-06 21:21:19 +00:00
|
|
|
|
2017-06-21 13:48:12 +00:00
|
|
|
expect(doc.at_css('a[href="#link-code"]').to_html)
|
|
|
|
.to eq %{<a href="#link-code"><code>text</code></a>}
|
2015-07-21 02:31:05 +00:00
|
|
|
end
|
2015-05-06 21:21:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
it 'includes ExternalLinkFilter' do
|
|
|
|
aggregate_failures 'adds nofollow to external link' do
|
2015-07-23 01:22:57 +00:00
|
|
|
link = doc.at_css('a:contains("Google")')
|
2016-06-08 17:20:24 +00:00
|
|
|
|
2016-04-21 18:03:37 +00:00
|
|
|
expect(link.attr('rel')).to include('nofollow')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'adds noreferrer to external link' do
|
2016-04-21 18:03:37 +00:00
|
|
|
link = doc.at_css('a:contains("Google")')
|
2016-06-08 17:20:24 +00:00
|
|
|
|
2016-04-21 18:03:37 +00:00
|
|
|
expect(link.attr('rel')).to include('noreferrer')
|
2015-05-27 19:39:08 +00:00
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'adds _blank to target attribute for external links' do
|
2016-06-08 07:02:55 +00:00
|
|
|
link = doc.at_css('a:contains("Google")')
|
2016-06-08 17:20:24 +00:00
|
|
|
|
2016-06-08 07:02:55 +00:00
|
|
|
expect(link.attr('target')).to match('_blank')
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'ignores internal link' do
|
2015-07-23 01:22:57 +00:00
|
|
|
link = doc.at_css('a:contains("GitLab Root")')
|
2016-06-08 07:02:55 +00:00
|
|
|
|
2016-06-08 17:20:24 +00:00
|
|
|
expect(link.attr('rel')).not_to match 'nofollow'
|
2016-06-08 07:02:55 +00:00
|
|
|
expect(link.attr('target')).not_to match '_blank'
|
|
|
|
end
|
2015-05-27 19:39:08 +00:00
|
|
|
end
|
2015-07-23 01:22:57 +00:00
|
|
|
end
|
2015-05-27 19:39:08 +00:00
|
|
|
|
2016-06-07 17:45:44 +00:00
|
|
|
before do
|
2016-01-12 21:00:35 +00:00
|
|
|
@feat = MarkdownFeature.new
|
2015-07-29 03:27:08 +00:00
|
|
|
|
2017-12-01 09:21:05 +00:00
|
|
|
# `markdown` helper expects a `@project` and `@group` variable
|
2016-01-12 21:00:35 +00:00
|
|
|
@project = @feat.project
|
2017-12-01 09:21:05 +00:00
|
|
|
@group = @feat.group
|
2016-01-12 21:00:35 +00:00
|
|
|
end
|
2015-07-29 03:27:08 +00:00
|
|
|
|
2020-01-14 21:07:45 +00:00
|
|
|
let(:project) { @feat.project } # Shadow this so matchers can use it
|
|
|
|
|
2016-01-12 21:00:35 +00:00
|
|
|
context 'default pipeline' do
|
2016-06-07 17:45:44 +00:00
|
|
|
before do
|
2015-07-23 01:22:57 +00:00
|
|
|
@html = markdown(@feat.raw_markdown)
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2015-07-23 01:22:57 +00:00
|
|
|
it_behaves_like 'all pipelines'
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
it 'includes custom filters' do
|
2020-01-14 21:07:45 +00:00
|
|
|
aggregate_failures 'UploadLinkFilter' do
|
|
|
|
expect(doc).to parse_upload_links
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate_failures 'RepositoryLinkFilter' do
|
|
|
|
expect(doc).to parse_repository_links
|
2018-06-05 20:00:55 +00:00
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'EmojiFilter' do
|
|
|
|
expect(doc).to parse_emoji
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'TableOfContentsFilter' do
|
|
|
|
expect(doc).to create_header_links
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'AutolinkFilter' do
|
|
|
|
expect(doc).to create_autolinks
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'all reference filters' do
|
2015-07-23 01:22:57 +00:00
|
|
|
expect(doc).to reference_users
|
|
|
|
expect(doc).to reference_issues
|
|
|
|
expect(doc).to reference_merge_requests
|
|
|
|
expect(doc).to reference_snippets
|
|
|
|
expect(doc).to reference_commit_ranges
|
|
|
|
expect(doc).to reference_commits
|
|
|
|
expect(doc).to reference_labels
|
2015-12-24 13:43:07 +00:00
|
|
|
expect(doc).to reference_milestones
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
2015-05-04 21:37:37 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'TaskListFilter' do
|
|
|
|
expect(doc).to parse_task_lists
|
|
|
|
end
|
2016-07-12 17:28:39 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'InlineDiffFilter' do
|
|
|
|
expect(doc).to parse_inline_diffs
|
|
|
|
end
|
2016-07-12 17:28:39 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'VideoLinkFilter' do
|
|
|
|
expect(doc).to parse_video_links
|
|
|
|
end
|
2017-12-20 11:39:40 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'ColorFilter' do
|
|
|
|
expect(doc).to parse_colors
|
|
|
|
end
|
2017-12-20 11:39:40 +00:00
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|
2015-07-21 02:22:17 +00:00
|
|
|
|
2016-01-12 21:00:35 +00:00
|
|
|
context 'wiki pipeline' do
|
|
|
|
before do
|
2020-05-25 21:08:00 +00:00
|
|
|
@wiki = @feat.wiki
|
|
|
|
@wiki_page = @feat.wiki_page
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-10-02 03:21:46 +00:00
|
|
|
path = 'images/example.jpg'
|
|
|
|
gitaly_wiki_file = Gitlab::GitalyClient::WikiFile.new(path: path)
|
2020-05-25 21:08:00 +00:00
|
|
|
expect(@wiki).to receive(:find_file).with(path).and_return(Gitlab::Git::WikiFile.new(gitaly_wiki_file))
|
|
|
|
allow(@wiki).to receive(:wiki_base_path) { '/namespace1/gitlabhq/wikis' }
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2020-05-25 21:08:00 +00:00
|
|
|
@html = markdown(@feat.raw_markdown, { pipeline: :wiki, wiki: @wiki, page_slug: @wiki_page.slug })
|
2016-01-12 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'all pipelines'
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
it 'includes custom filters' do
|
2020-01-14 21:07:45 +00:00
|
|
|
aggregate_failures 'UploadLinkFilter' do
|
|
|
|
expect(doc).to parse_upload_links
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregate_failures 'RepositoryLinkFilter' do
|
|
|
|
expect(doc).not_to parse_repository_links
|
2018-06-05 20:00:55 +00:00
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'EmojiFilter' do
|
|
|
|
expect(doc).to parse_emoji
|
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'TableOfContentsFilter' do
|
|
|
|
expect(doc).to create_header_links
|
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'AutolinkFilter' do
|
|
|
|
expect(doc).to create_autolinks
|
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'all reference filters' do
|
2016-01-12 21:00:35 +00:00
|
|
|
expect(doc).to reference_users
|
|
|
|
expect(doc).to reference_issues
|
|
|
|
expect(doc).to reference_merge_requests
|
|
|
|
expect(doc).to reference_snippets
|
|
|
|
expect(doc).to reference_commit_ranges
|
|
|
|
expect(doc).to reference_commits
|
|
|
|
expect(doc).to reference_labels
|
|
|
|
expect(doc).to reference_milestones
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'TaskListFilter' do
|
|
|
|
expect(doc).to parse_task_lists
|
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'GollumTagsFilter' do
|
|
|
|
expect(doc).to parse_gollum_tags
|
|
|
|
end
|
2016-04-06 19:37:09 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'InlineDiffFilter' do
|
|
|
|
expect(doc).to parse_inline_diffs
|
|
|
|
end
|
2016-07-12 17:28:39 +00:00
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'VideoLinkFilter' do
|
|
|
|
expect(doc).to parse_video_links
|
|
|
|
end
|
2017-12-20 11:39:40 +00:00
|
|
|
|
2019-10-09 12:06:13 +00:00
|
|
|
aggregate_failures 'AudioLinkFilter' do
|
|
|
|
expect(doc).to parse_audio_links
|
|
|
|
end
|
|
|
|
|
2018-06-05 20:00:55 +00:00
|
|
|
aggregate_failures 'ColorFilter' do
|
|
|
|
expect(doc).to parse_colors
|
|
|
|
end
|
2017-12-20 11:39:40 +00:00
|
|
|
end
|
2016-01-12 21:00:35 +00:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:16:56 +00:00
|
|
|
# Fake a `current_user` helper
|
2015-07-21 02:22:17 +00:00
|
|
|
def current_user
|
|
|
|
@feat.user
|
|
|
|
end
|
2015-04-23 04:21:11 +00:00
|
|
|
end
|