copy_as_gfm_spec.rb 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
require 'spec_helper'

describe 'Copy as GFM', feature: true, js: true do
  include GitlabMarkdownHelper
  include ActionView::Helpers::JavaScriptHelper

  before do
    @feat = MarkdownFeature.new

    # `markdown` helper expects a `@project` variable
    @project = @feat.project

    visit namespace_project_issue_path(@project.namespace, @project, @feat.issue)
  end

D
Douwe Maan 已提交
16 17 18
  # The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML.
  # The handlers defined in app/assets/javascripts/copy_as_gfm.js.es6 consequently convert that same HTML to GFM.
  # To make sure these filters and handlers are properly aligned, this spec tests the GFM-to-HTML-to-GFM cycle
19
  # by verifying (`html_to_gfm(gfm_to_html(gfm)) == gfm`) for a number of examples of GFM for every filter, using the `verify` helper.
20

21 22 23 24
  # These are all in a single `it` for performance reasons.
  it 'works', :aggregate_failures do
    verify(
      'nesting',
D
Douwe Maan 已提交
25

26 27
      '> 1. [x] **[$`2 + 2`$ {-=-}{+=+} 2^2 ~~:thumbsup:~~](http://google.com)**'
    )
D
Douwe Maan 已提交
28

29 30
    verify(
      'a real world example from the gitlab-ce README',
31

32 33
      <<-GFM.strip_heredoc
        # GitLab
34

35 36 37 38
        [![Build status](https://gitlab.com/gitlab-org/gitlab-ce/badges/master/build.svg)](https://gitlab.com/gitlab-org/gitlab-ce/commits/master)
        [![CE coverage report](https://gitlab.com/gitlab-org/gitlab-ce/badges/master/coverage.svg?job=coverage)](https://gitlab-org.gitlab.io/gitlab-ce/coverage-ruby)
        [![Code Climate](https://codeclimate.com/github/gitlabhq/gitlabhq.svg)](https://codeclimate.com/github/gitlabhq/gitlabhq)
        [![Core Infrastructure Initiative Best Practices](https://bestpractices.coreinfrastructure.org/projects/42/badge)](https://bestpractices.coreinfrastructure.org/projects/42)
39

40
        ## Canonical source
41

42
        The canonical source of GitLab Community Edition is [hosted on GitLab.com](https://gitlab.com/gitlab-org/gitlab-ce/).
43

44
        ## Open source software to collaborate on code
45

46
        To see how GitLab looks please see the [features page on our website](https://about.gitlab.com/features/).
47 48


49
        - Manage Git repositories with fine grained access controls that keep your code secure
50

51
        - Perform code reviews and enhance collaboration with merge requests
52

53
        - Complete continuous integration (CI) and CD pipelines to builds, test, and deploy your applications
54

55 56 57 58 59 60 61
        - Each project can also have an issue tracker, issue board, and a wiki

        - Used by more than 100,000 organizations, GitLab is the most popular solution to manage Git repositories on-premises

        - Completely free and open source (MIT Expat license)
      GFM
    )
D
Douwe Maan 已提交
62 63

    verify(
64 65
      'InlineDiffFilter',

66 67
      '{-Deleted text-}',
      '{+Added text+}'
D
Douwe Maan 已提交
68 69 70
    )

    verify(
71 72
      'TaskListFilter',

73 74 75 76
      '- [ ] Unchecked task',
      '- [x] Checked task',
      '1. [ ] Unchecked numbered task',
      '1. [x] Checked numbered task'
D
Douwe Maan 已提交
77 78 79
    )

    verify(
80 81
      'ReferenceFilter',

D
Douwe Maan 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
      # issue reference
      @feat.issue.to_reference,
      # full issue reference
      @feat.issue.to_reference(full: true),
      # issue URL
      namespace_project_issue_url(@project.namespace, @project, @feat.issue),
      # issue URL with note anchor
      namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123'),
      # issue link
      "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue)})",
      # issue link with note anchor
      "[Issue](#{namespace_project_issue_url(@project.namespace, @project, @feat.issue, anchor: 'note_123')})",
    )

96 97
    verify(
      'AutolinkFilter',
D
Douwe Maan 已提交
98

99 100
      'https://example.com'
    )
D
Douwe Maan 已提交
101

102 103
    verify(
      'TableOfContentsFilter',
D
Douwe Maan 已提交
104

105 106
      '[[_TOC_]]'
    )
D
Douwe Maan 已提交
107

108 109
    verify(
      'EmojiFilter',
D
Douwe Maan 已提交
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      ':thumbsup:'
    )

    verify(
      'ImageLinkFilter',

      '![Image](https://example.com/image.png)'
    )

    verify(
      'VideoLinkFilter',

      '![Video](https://example.com/video.mp4)'
    )

    verify(
      'MathFilter: math as converted from GFM to HTML',

      '$`c = \pm\sqrt{a^2 + b^2}`$',
130

131 132 133 134 135 136 137 138 139
      # math block
      <<-GFM.strip_heredoc
        ```math
        c = \pm\sqrt{a^2 + b^2}
        ```
      GFM
    )

    aggregate_failures('MathFilter: math as transformed from HTML to KaTeX') do
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
      gfm = '$`c = \pm\sqrt{a^2 + b^2}`$'

      html = <<-HTML.strip_heredoc
        <span class="katex">
          <span class="katex-mathml">
            <math>
              <semantics>
                <mrow>
                  <mi>c</mi>
                  <mo>=</mo>
                  <mo>±</mo>
                  <msqrt>
                    <mrow>
                      <msup>
                        <mi>a</mi>
                        <mn>2</mn>
                      </msup>
                      <mo>+</mo>
                      <msup>
                        <mi>b</mi>
                        <mn>2</mn>
                      </msup>
                    </mrow>
                  </msqrt>
                </mrow>
                <annotation encoding="application/x-tex">c = \\pm\\sqrt{a^2 + b^2}</annotation>
              </semantics>
            </math>
          </span>
          <span class="katex-html" aria-hidden="true">
              <span class="strut" style="height: 0.913389em;"></span>
              <span class="strut bottom" style="height: 1.04em; vertical-align: -0.126611em;"></span>
              <span class="base textstyle uncramped">
                <span class="mord mathit">c</span>
                <span class="mrel">=</span>
                <span class="mord">±</span>
                <span class="sqrt mord"><span class="sqrt-sign" style="top: -0.073389em;">
                  <span class="style-wrap reset-textstyle textstyle uncramped">√</span>
                </span>
                <span class="vlist">
                  <span class="" style="top: 0em;">
                    <span class="fontsize-ensurer reset-size5 size5">
                      <span class="" style="font-size: 1em;">​</span>
                    </span>
                    <span class="mord textstyle cramped">
                      <span class="mord">
                        <span class="mord mathit">a</span>
                        <span class="msupsub">
                          <span class="vlist">
                            <span class="" style="top: -0.289em; margin-right: 0.05em;">
                              <span class="fontsize-ensurer reset-size5 size5">
                                <span class="" style="font-size: 0em;">​</span>
                              </span>
                              <span class="reset-textstyle scriptstyle cramped">
                                <span class="mord mathrm">2</span>
                              </span>
                            </span>
                            <span class="baseline-fix">
                              <span class="fontsize-ensurer reset-size5 size5">
                                <span class="" style="font-size: 0em;">​</span>
                              </span>
                            ​</span>
                          </span>
                        </span>
                      </span>
                      <span class="mbin">+</span>
                      <span class="mord">
                        <span class="mord mathit">b</span>
                        <span class="msupsub">
                          <span class="vlist">
                            <span class="" style="top: -0.289em; margin-right: 0.05em;">
                              <span class="fontsize-ensurer reset-size5 size5">
                                <span class="" style="font-size: 0em;">​</span>
                              </span>
                              <span class="reset-textstyle scriptstyle cramped">
                                <span class="mord mathrm">2</span>
                              </span>
                            </span>
                            <span class="baseline-fix">
                              <span class="fontsize-ensurer reset-size5 size5">
                                <span class="" style="font-size: 0em;">​</span>
                              </span>
                            ​</span>
                          </span>
                        </span>
                      </span>
                    </span>
                  </span>
                  <span class="" style="top: -0.833389em;">
                    <span class="fontsize-ensurer reset-size5 size5">
                      <span class="" style="font-size: 1em;">​</span>
                    </span>
                    <span class="reset-textstyle textstyle uncramped sqrt-line"></span>
                  </span>
                  <span class="baseline-fix">
                    <span class="fontsize-ensurer reset-size5 size5">
                      <span class="" style="font-size: 1em;">​</span>
                    </span>
                  ​</span>
                </span>
              </span>
            </span>
          </span>
        </span>
      HTML

      output_gfm = html_to_gfm(html)
      expect(output_gfm.strip).to eq(gfm.strip)
    end

D
Douwe Maan 已提交
250
    verify(
251 252
      'SanitizationFilter',

D
Douwe Maan 已提交
253
      <<-GFM.strip_heredoc
254
      <a name="named-anchor"></a>
E
Eric Eastwood 已提交
255

D
Douwe Maan 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      <sub>sub</sub>

      <dl>
        <dt>dt</dt>
        <dd>dd</dd>
      </dl>

      <kbd>kbd</kbd>

      <q>q</q>

      <samp>samp</samp>

      <var>var</var>

      <ruby>ruby</ruby>

      <rt>rt</rt>

      <rp>rp</rp>

      <abbr>abbr</abbr>
      GFM
    )

D
Douwe Maan 已提交
281
    verify(
282 283
      'SanitizationFilter',

284 285 286 287 288 289
      <<-GFM.strip_heredoc,
        ```
        Plain text
        ```
      GFM

290
      <<-GFM.strip_heredoc,
291 292 293 294
        ```ruby
        def foo
          bar
        end
D
Douwe Maan 已提交
295 296
        ```
      GFM
297 298 299

      <<-GFM.strip_heredoc
        Foo
300

301 302 303 304 305 306
            This is an example of GFM

            ```js
            Code goes here
            ```
      GFM
D
Douwe Maan 已提交
307 308 309
    )

    verify(
310 311
      'MarkdownFilter',

312 313
      "Line with two spaces at the end  \nto insert a linebreak",

314 315 316 317
      '`code`',
      '`` code with ` ticks ``',

      '> Quote',
318

D
Douwe Maan 已提交
319 320 321 322 323 324 325
      # multiline quote
      <<-GFM.strip_heredoc,
        > Multiline
        > Quote
        >
        > With multiple paragraphs
      GFM
326 327 328 329 330 331 332 333

      '![Image](https://example.com/image.png)',

      '# Heading with no anchor link',

      '[Link](https://example.com)',

      '- List item',
D
Douwe Maan 已提交
334 335 336 337

      # multiline list item
      <<-GFM.strip_heredoc,
        - Multiline
338
            List item
D
Douwe Maan 已提交
339 340 341 342 343 344 345
      GFM

      # nested lists
      <<-GFM.strip_heredoc,
        - Nested


346 347 348 349 350 351 352 353
            - Lists
      GFM

      # list with blockquote
      <<-GFM.strip_heredoc,
        - List

            > Blockquote
D
Douwe Maan 已提交
354 355
      GFM

356
      '1. Numbered list item',
D
Douwe Maan 已提交
357 358 359 360

      # multiline numbered list item
      <<-GFM.strip_heredoc,
        1. Multiline
361
            Numbered list item
D
Douwe Maan 已提交
362 363 364 365 366 367 368
      GFM

      # nested numbered list
      <<-GFM.strip_heredoc,
        1. Nested


369
            1. Numbered lists
D
Douwe Maan 已提交
370
      GFM
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

      '# Heading',
      '## Heading',
      '### Heading',
      '#### Heading',
      '##### Heading',
      '###### Heading',

      '**Bold**',

      '_Italics_',

      '~~Strikethrough~~',

      '2^2',

      '-----',

D
Douwe Maan 已提交
389 390 391
      # table
      <<-GFM.strip_heredoc,
        | Centered | Right | Left |
392
        |:--------:|------:|------|
D
Douwe Maan 已提交
393 394 395
        | Foo | Bar | **Baz** |
        | Foo | Bar | **Baz** |
      GFM
396 397 398 399 400 401 402 403

      # table with empty heading
      <<-GFM.strip_heredoc,
        |  | x | y |
        |---|---|---|
        | a | 1 | 0 |
        | b | 0 | 1 |
      GFM
D
Douwe Maan 已提交
404
    )
405 406
  end

D
Douwe Maan 已提交
407 408
  alias_method :gfm_to_html, :markdown

409 410 411 412 413 414 415 416 417 418 419
  def html_to_gfm(html)
    js = <<-JS.strip_heredoc
      (function(html) {
        var node = document.createElement('div');
        node.innerHTML = html;
        return window.gl.CopyAsGFM.nodeToGFM(node);
      })("#{escape_javascript(html)}")
    JS
    page.evaluate_script(js)
  end

420 421
  def verify(label, *gfms)
    aggregate_failures(label) do
D
Douwe Maan 已提交
422 423 424 425 426 427 428 429
      gfms.each do |gfm|
        html = gfm_to_html(gfm)
        output_gfm = html_to_gfm(html)
        expect(output_gfm.strip).to eq(gfm.strip)
      end
    end
  end

430 431 432 433 434
  # Fake a `current_user` helper
  def current_user
    @feat.user
  end
end