2015-04-23 00:21:11 -04: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
|
|
|
|
# -> Redcarpet::Render::GitlabHTML converts Markdown to HTML
|
|
|
|
# -> Post-process HTML
|
2015-08-12 22:45:16 -04:00
|
|
|
# -> `gfm` helper
|
2015-04-23 00:21:11 -04:00
|
|
|
# -> HTML::Pipeline
|
2015-07-22 21:22:57 -04:00
|
|
|
# -> SanitizationFilter
|
|
|
|
# -> Other filters, depending on pipeline
|
2015-04-23 00:21:11 -04:00
|
|
|
# -> `html_safe`
|
|
|
|
# -> Template
|
|
|
|
#
|
|
|
|
# See the MarkdownFeature class for setup details.
|
|
|
|
|
2015-06-28 16:19:52 -04:00
|
|
|
describe 'GitLab Markdown', feature: true do
|
2015-04-23 00:21:11 -04:00
|
|
|
include Capybara::Node::Matchers
|
|
|
|
include GitlabMarkdownHelper
|
2015-07-22 21:22:57 -04:00
|
|
|
include MarkdownMatchers
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04: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 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
2015-07-28 23:27:08 -04:00
|
|
|
def doc(html = @html)
|
|
|
|
Nokogiri::HTML::DocumentFragment.parse(html)
|
|
|
|
end
|
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
# Shared behavior that all pipelines should exhibit
|
|
|
|
shared_examples 'all pipelines' do
|
|
|
|
describe 'Redcarpet extensions' do
|
2015-04-23 00:21:11 -04:00
|
|
|
it 'does not parse emphasis inside of words' do
|
2015-07-22 21:22:57 -04:00
|
|
|
expect(doc.to_html).not_to match('foo<em>bar</em>baz')
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses table Markdown' do
|
2015-07-20 22:31:05 -04:00
|
|
|
aggregate_failures do
|
2015-07-22 21:22:57 -04:00
|
|
|
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-07-20 22:31:05 -04:00
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows Markdown in tables' do
|
2015-07-20 22:22:17 -04:00
|
|
|
expect(doc.at_css('td:contains("Baz")').children.to_html).
|
2015-04-23 00:21:11 -04:00
|
|
|
to eq '<strong>Baz</strong>'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses fenced code blocks' do
|
2015-07-20 22:31:05 -04:00
|
|
|
aggregate_failures do
|
2015-08-25 18:57:55 -04:00
|
|
|
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-07-20 22:31:05 -04:00
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses strikethroughs' do
|
2015-07-20 22:22:17 -04:00
|
|
|
expect(doc).to have_selector(%{del:contains("and this text doesn't")})
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'parses superscript' do
|
2015-07-22 21:22:57 -04:00
|
|
|
expect(doc).to have_selector('sup', count: 2)
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'SanitizationFilter' do
|
2015-07-20 23:57:26 -04:00
|
|
|
it 'permits b elements' do
|
|
|
|
expect(doc).to have_selector('b:contains("b tag")')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits em elements' do
|
|
|
|
expect(doc).to have_selector('em:contains("em tag")')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits code elements' do
|
|
|
|
expect(doc).to have_selector('code:contains("code tag")')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits kbd elements' do
|
|
|
|
expect(doc).to have_selector('kbd:contains("s")')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits strike elements' do
|
|
|
|
expect(doc).to have_selector('strike:contains(Emoji)')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits img elements' do
|
|
|
|
expect(doc).to have_selector('img[src*="smile.png"]')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits br elements' do
|
|
|
|
expect(doc).to have_selector('br')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits hr elements' do
|
|
|
|
expect(doc).to have_selector('hr')
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'permits span elements' do
|
2015-07-20 22:22:17 -04:00
|
|
|
expect(doc).to have_selector('span:contains("span tag")')
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
2015-07-20 23:57:26 -04:00
|
|
|
it 'permits style attribute in th elements' do
|
2015-07-20 22:31:05 -04:00
|
|
|
aggregate_failures do
|
|
|
|
expect(doc.at_css('th:contains("Header")')['style']).to eq 'text-align: center'
|
|
|
|
expect(doc.at_css('th:contains("Row")')['style']).to eq 'text-align: right'
|
|
|
|
expect(doc.at_css('th:contains("Example")')['style']).to eq 'text-align: left'
|
2015-07-20 23:57:26 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-20 22:31:05 -04:00
|
|
|
|
2015-07-20 23:57:26 -04:00
|
|
|
it 'permits style attribute in td elements' do
|
|
|
|
aggregate_failures do
|
2015-07-20 22:31:05 -04:00
|
|
|
expect(doc.at_css('td:contains("Foo")')['style']).to eq 'text-align: center'
|
|
|
|
expect(doc.at_css('td:contains("Bar")')['style']).to eq 'text-align: right'
|
|
|
|
expect(doc.at_css('td:contains("Baz")')['style']).to eq 'text-align: left'
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes `rel` attribute from links' do
|
2015-07-20 23:57:26 -04:00
|
|
|
expect(doc).not_to have_selector('a[rel="bookmark"]')
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes `href` from `a` elements if it's fishy" do
|
2015-07-20 22:22:17 -04:00
|
|
|
expect(doc).not_to have_selector('a[href*="javascript"]')
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Escaping' do
|
|
|
|
it 'escapes non-tag angle brackets' do
|
2015-07-20 23:57:26 -04:00
|
|
|
table = doc.css('table').last.at_css('tbody')
|
2015-04-23 00:21:11 -04:00
|
|
|
expect(table.at_xpath('.//tr[1]/td[3]').inner_html).to eq '1 < 3 & 5'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-06 17:21:19 -04:00
|
|
|
describe 'Edge Cases' do
|
|
|
|
it 'allows markup inside link elements' do
|
2015-07-20 22:31:05 -04:00
|
|
|
aggregate_failures do
|
|
|
|
expect(doc.at_css('a[href="#link-emphasis"]').to_html).
|
|
|
|
to eq %{<a href="#link-emphasis"><em>text</em></a>}
|
2015-05-06 17:21:19 -04:00
|
|
|
|
2015-07-20 22:31:05 -04:00
|
|
|
expect(doc.at_css('a[href="#link-strong"]').to_html).
|
|
|
|
to eq %{<a href="#link-strong"><strong>text</strong></a>}
|
2015-05-06 17:21:19 -04:00
|
|
|
|
2015-07-20 22:31:05 -04:00
|
|
|
expect(doc.at_css('a[href="#link-code"]').to_html).
|
|
|
|
to eq %{<a href="#link-code"><code>text</code></a>}
|
|
|
|
end
|
2015-05-06 17:21:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-27 15:39:08 -04:00
|
|
|
describe 'ExternalLinkFilter' do
|
|
|
|
it 'adds nofollow to external link' do
|
2015-07-22 21:22:57 -04:00
|
|
|
link = doc.at_css('a:contains("Google")')
|
|
|
|
expect(link.attr('rel')).to match 'nofollow'
|
2015-05-27 15:39:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores internal link' do
|
2015-07-22 21:22:57 -04:00
|
|
|
link = doc.at_css('a:contains("GitLab Root")')
|
|
|
|
expect(link.attr('rel')).not_to match 'nofollow'
|
2015-05-27 15:39:08 -04:00
|
|
|
end
|
|
|
|
end
|
2015-07-22 21:22:57 -04:00
|
|
|
end
|
2015-05-27 15:39:08 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
context 'default pipeline' do
|
|
|
|
before(:all) do
|
2015-07-28 23:27:08 -04:00
|
|
|
@feat = MarkdownFeature.new
|
|
|
|
|
2015-08-27 16:09:01 -04:00
|
|
|
# `markdown` helper expects a `@project` variable
|
2015-07-28 23:27:08 -04:00
|
|
|
@project = @feat.project
|
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
@html = markdown(@feat.raw_markdown)
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it_behaves_like 'all pipelines'
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes RelativeLinkFilter' do
|
|
|
|
expect(doc).to parse_relative_links
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes EmojiFilter' do
|
|
|
|
expect(doc).to parse_emoji
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes TableOfContentsFilter' do
|
|
|
|
expect(doc).to create_header_links
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes AutolinkFilter' do
|
|
|
|
expect(doc).to create_autolinks
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes all reference filters' do
|
|
|
|
aggregate_failures do
|
|
|
|
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-04-23 00:21:11 -04:00
|
|
|
end
|
|
|
|
end
|
2015-05-04 17:37:37 -04:00
|
|
|
|
2015-07-22 21:22:57 -04:00
|
|
|
it 'includes TaskListFilter' do
|
|
|
|
expect(doc).to parse_task_lists
|
2015-05-04 17:37:37 -04:00
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|
2015-07-20 22:22:17 -04:00
|
|
|
|
2015-09-01 18:16:56 -04:00
|
|
|
# Fake a `current_user` helper
|
2015-07-20 22:22:17 -04:00
|
|
|
def current_user
|
|
|
|
@feat.user
|
|
|
|
end
|
2015-04-23 00:21:11 -04:00
|
|
|
end
|