copy_as_gfm_spec.rb 23.6 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Copy as GFM', :js do
4
  include MarkupHelper
5
  include RepoHelpers
6 7 8
  include ActionView::Helpers::JavaScriptHelper

  before do
9
    sign_in(create(:admin))
10
  end
11

12 13 14
  describe 'Copying rendered GFM' do
    before do
      @feat = MarkdownFeature.new
15

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

19
      visit project_issue_path(@project, @feat.issue)
20
    end
21

22
    # The filters referenced in lib/banzai/pipeline/gfm_pipeline.rb convert GitLab Flavored Markdown (GFM) to HTML.
23
    # The handlers defined in app/assets/javascripts/copy_as_gfm.js consequently convert that same HTML to GFM.
24 25
    # To make sure these filters and handlers are properly aligned, this spec tests the GFM-to-HTML-to-GFM cycle
    # by verifying (`html_to_gfm(gfm_to_html(gfm)) == gfm`) for a number of examples of GFM for every filter, using the `verify` helper.
Douwe Maan's avatar
Douwe Maan committed
26

27 28 29 30
    # These are all in a single `it` for performance reasons.
    it 'works', :aggregate_failures do
      verify(
        'nesting',
Douwe Maan's avatar
Douwe Maan committed
31

32 33
        '> 1. [x] **[$`2 + 2`$ {-=-}{+=+} 2^2 ~~:thumbsup:~~](http://google.com)**'
      )
34

35 36
      verify(
        'a real world example from the gitlab-ce README',
37

38 39
        <<-GFM.strip_heredoc
          # GitLab
40

41 42 43 44
          [![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)
45

46
          ## Canonical source
47

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

50
          ## Open source software to collaborate on code
51

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

54
          - Manage Git repositories with fine grained access controls that keep your code secure
55

56
          - Perform code reviews and enhance collaboration with merge requests
57

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

60
          - Each project can also have an issue tracker, issue board, and a wiki
61

62
          - Used by more than 100,000 organizations, GitLab is the most popular solution to manage Git repositories on-premises
Douwe Maan's avatar
Douwe Maan committed
63

64 65 66
          - Completely free and open source (MIT Expat license)
        GFM
      )
67

Douwe Maan's avatar
Douwe Maan committed
68 69 70 71 72 73 74 75 76
      aggregate_failures('an accidentally selected empty element') do
        gfm = '# Heading1'

        html = <<-HTML.strip_heredoc
          <h1>Heading1</h1>

          <h2></h2>
        HTML

Douwe Maan's avatar
Douwe Maan committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        output_gfm = html_to_gfm(html)
        expect(output_gfm.strip).to eq(gfm.strip)
      end

      aggregate_failures('an accidentally selected other element') do
        gfm = 'Test comment with **Markdown!**'

        html = <<-HTML.strip_heredoc
          <li class="note">
            <div class="md">
              <p>
                Test comment with <strong>Markdown!</strong>
              </p>
            </div>
          </li>

          <li class="note"></li>
        HTML

Douwe Maan's avatar
Douwe Maan committed
96 97 98 99
        output_gfm = html_to_gfm(html)
        expect(output_gfm.strip).to eq(gfm.strip)
      end

100 101
      verify(
        'InlineDiffFilter',
Douwe Maan's avatar
Douwe Maan committed
102

103 104 105
        '{-Deleted text-}',
        '{+Added text+}'
      )
106

107 108
      verify(
        'TaskListFilter',
Douwe Maan's avatar
Douwe Maan committed
109

110 111 112 113 114
        '- [ ] Unchecked task',
        '- [x] Checked task',
        '1. [ ] Unchecked numbered task',
        '1. [x] Checked numbered task'
      )
115

116 117
      verify(
        'ReferenceFilter',
Douwe Maan's avatar
Douwe Maan committed
118

119 120 121 122 123
        # issue reference
        @feat.issue.to_reference,
        # full issue reference
        @feat.issue.to_reference(full: true),
        # issue URL
124
        project_issue_url(@project, @feat.issue),
125
        # issue URL with note anchor
126
        project_issue_url(@project, @feat.issue, anchor: 'note_123'),
127
        # issue link
128
        "[Issue](#{project_issue_url(@project, @feat.issue)})",
129
        # issue link with note anchor
130
        "[Issue](#{project_issue_url(@project, @feat.issue, anchor: 'note_123')})"
131
      )
Douwe Maan's avatar
Douwe Maan committed
132

133 134
      verify(
        'AutolinkFilter',
Douwe Maan's avatar
Douwe Maan committed
135

136 137
        'https://example.com'
      )
Douwe Maan's avatar
Douwe Maan committed
138

139 140
      verify(
        'TableOfContentsFilter',
Douwe Maan's avatar
Douwe Maan committed
141

142 143
        '[[_TOC_]]'
      )
Douwe Maan's avatar
Douwe Maan committed
144

145 146
      verify(
        'EmojiFilter',
147

148 149
        ':thumbsup:'
      )
150

151 152 153 154 155
      verify(
        'ImageLinkFilter',

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

157 158
      verify(
        'VideoLinkFilter',
159

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

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

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

168 169 170 171 172 173 174
        # math block
        <<-GFM.strip_heredoc
          ```math
          c = \pm\sqrt{a^2 + b^2}
          ```
        GFM
      )
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
      aggregate_failures('MathFilter: math as transformed from HTML to KaTeX') do
        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>
233
                              </span>
234 235 236 237 238
                              <span class="baseline-fix">
                                <span class="fontsize-ensurer reset-size5 size5">
                                  <span class="" style="font-size: 0em;">​</span>
                                </span>
                              ​</span>
239 240 241
                            </span>
                          </span>
                        </span>
242 243 244 245 246 247 248 249 250 251 252 253
                        <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>
254
                              </span>
255 256 257 258 259
                              <span class="baseline-fix">
                                <span class="fontsize-ensurer reset-size5 size5">
                                  <span class="" style="font-size: 0em;">​</span>
                                </span>
                              ​</span>
260 261 262 263 264
                            </span>
                          </span>
                        </span>
                      </span>
                    </span>
265 266 267 268 269
                    <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>
270
                    </span>
271 272 273 274 275
                    <span class="baseline-fix">
                      <span class="fontsize-ensurer reset-size5 size5">
                        <span class="" style="font-size: 1em;">​</span>
                      </span>
                    ​</span>
276 277 278 279 280
                  </span>
                </span>
              </span>
            </span>
          </span>
281
        HTML
282

283 284 285
        output_gfm = html_to_gfm(html)
        expect(output_gfm.strip).to eq(gfm.strip)
      end
286

Douwe Maan's avatar
Douwe Maan committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
      verify(
        'MermaidFilter: mermaid as converted from GFM to HTML',

        <<-GFM.strip_heredoc
          ```mermaid
          graph TD;
            A-->B;
          ```
        GFM
      )

      aggregate_failures('MermaidFilter: mermaid as transformed from HTML to SVG') do
        gfm = <<-GFM.strip_heredoc
          ```mermaid
          graph TD;
            A-->B;
          ```
        GFM

        html = <<-HTML.strip_heredoc
          <svg id="mermaidChart1" xmlns="http://www.w3.org/2000/svg" height="100%" viewBox="0 0 87.234375 174" style="max-width:87.234375px;" class="mermaid">
            <style>
              .mermaid {
                /* Flowchart variables */
                /* Sequence Diagram variables */
                /* Gantt chart variables */
                /** Section styling */
                /* Grid and axis */
                /* Today line */
                /* Task styling */
                /* Default task */
                /* Specific task settings for the sections*/
                /* Active task */
                /* Completed task */
                /* Tasks on the critical line */
              }
            </style>
            <g>
              <g class="output">
                <g class="clusters"></g>
                <g class="edgePaths">
                  <g class="edgePath" style="opacity: 1;">
                    <path class="path" d="M33.6171875,52L33.6171875,77L33.6171875,102" marker-end="url(#arrowhead65)" style="fill:none"></path>
                    <defs>
                      <marker id="arrowhead65" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="strokeWidth" markerWidth="8" markerHeight="6" orient="auto">
                        <path d="M 0 0 L 10 5 L 0 10 z" class="arrowheadPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"></path>
                      </marker>
                    </defs>
                  </g>
                </g>
                <g class="edgeLabels">
                  <g class="edgeLabel" style="opacity: 1;" transform="">
                    <g transform="translate(0,0)" class="label">
                      <foreignObject width="0" height="0">
                        <div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">
                          <span class="edgeLabel"></span>
                        </div>
                      </foreignObject>
                    </g>
                  </g>
                </g>
                <g class="nodes">
                  <g class="node" id="A" transform="translate(33.6171875,36)" style="opacity: 1;">
                    <rect rx="0" ry="0" x="-13.6171875" y="-16" width="27.234375" height="32"></rect>
                    <g class="label" transform="translate(0,0)">
                      <g transform="translate(-3.6171875,-6)">
                        <foreignObject width="7.234375" height="12">
                          <div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">A</div>
                        </foreignObject>
                      </g>
                    </g>
                  </g>
                  <g class="node" id="B" transform="translate(33.6171875,118)" style="opacity: 1;">
                    <rect rx="0" ry="0" x="-13.6171875" y="-16" width="27.234375" height="32">
                    </rect>
                    <g class="label" transform="translate(0,0)">
                      <g transform="translate(-3.6171875,-6)">
                        <foreignObject width="7.234375" height="12">
                          <div xmlns="http://www.w3.org/1999/xhtml" style="display: inline-block; white-space: nowrap;">B</div>
                        </foreignObject>
                      </g>
                    </g>
                  </g>
                </g>
              </g>
            </g>
            <text class="source" display="none">graph TD;
            A--&gt;B;
            </text>
          </svg>
        HTML

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

383 384
      verify(
        'SanitizationFilter',
385

386 387
        <<-GFM.strip_heredoc
        <sub>sub</sub>
388

389 390 391 392
        <dl>
          <dt>dt</dt>
          <dd>dd</dd>
        </dl>
393

394
        <kbd>kbd</kbd>
395

396
        <q>q</q>
397

398
        <samp>samp</samp>
399

400
        <var>var</var>
401

402
        <ruby>ruby</ruby>
403

404
        <rt>rt</rt>
405

406
        <rp>rp</rp>
407

408
        <abbr>abbr</abbr>
409

410
        <summary>summary</summary>
411

412 413 414
        <details>details</details>
        GFM
      )
415

416 417
      verify(
        'SanitizationFilter',
418

419 420 421 422 423
        <<-GFM.strip_heredoc,
          ```
          Plain text
          ```
        GFM
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
        <<-GFM.strip_heredoc,
          ```ruby
          def foo
            bar
          end
          ```
        GFM

        <<-GFM.strip_heredoc
          Foo

              This is an example of GFM

              ```js
              Code goes here
              ```
        GFM
      )

      verify(
        'MarkdownFilter',

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

        '`code`',
        '`` code with ` ticks ``',

        '> Quote',

        # multiline quote
        <<-GFM.strip_heredoc,
          > Multiline
          > Quote
          >
          > With multiple paragraphs
        GFM

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

        '# Heading with no anchor link',

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

        '- List item',

        # multiline list item
        <<-GFM.strip_heredoc,
          - Multiline
              List item
        GFM

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

              - Lists
        GFM

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

              > Blockquote
        GFM

        '1. Numbered list item',

        # multiline numbered list item
        <<-GFM.strip_heredoc,
          1. Multiline
              Numbered list item
        GFM

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

502 503
              1. Numbered lists
        GFM
504

505 506 507 508 509 510
        '# Heading',
        '## Heading',
        '### Heading',
        '#### Heading',
        '##### Heading',
        '###### Heading',
Douwe Maan's avatar
Douwe Maan committed
511

512
        '**Bold**',
513

514
        '_Italics_',
515

516
        '~~Strikethrough~~',
517

518
        '2^2',
519

520
        '-----',
521

522 523 524 525 526 527 528
        # table
        <<-GFM.strip_heredoc,
          | Centered | Right | Left |
          |:--------:|------:|------|
          | Foo | Bar | **Baz** |
          | Foo | Bar | **Baz** |
        GFM
529

530 531 532 533 534 535 536 537 538
        # table with empty heading
        <<-GFM.strip_heredoc,
          |  | x | y |
          |---|---|---|
          | a | 1 | 0 |
          | b | 0 | 1 |
        GFM
      )
    end
539

540
    alias_method :gfm_to_html, :markdown
541

542 543 544
    def verify(label, *gfms)
      aggregate_failures(label) do
        gfms.each do |gfm|
545
          html = gfm_to_html(gfm).gsub(/\A&#x000A;|&#x000A;\z/, '')
546 547 548 549 550
          output_gfm = html_to_gfm(html)
          expect(output_gfm.strip).to eq(gfm.strip)
        end
      end
    end
Douwe Maan's avatar
Douwe Maan committed
551

552 553 554 555 556
    # Fake a `current_user` helper
    def current_user
      @feat.user
    end
  end
Douwe Maan's avatar
Douwe Maan committed
557

558
  describe 'Copying code' do
559
    let(:project) { create(:project, :repository) }
Douwe Maan's avatar
Douwe Maan committed
560

561
    context 'from a diff' do
562 563 564 565 566
      shared_examples 'copying code from a diff' do
        context 'selecting one word of text' do
          it 'copies as inline code' do
            verify(
              '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"] .line .no',
Douwe Maan's avatar
Douwe Maan committed
567

568
              '`RuntimeError`',
569

570 571 572
              target: '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"]'
            )
          end
573
        end
574

575 576 577 578
        context 'selecting one line of text' do
          it 'copies as inline code' do
            verify(
              '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"]',
Douwe Maan's avatar
Douwe Maan committed
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
              '`raise RuntimeError, "System commands must be given as an array of strings"`',

              target: '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"]'
            )
          end
        end

        context 'selecting multiple lines of text' do
          it 'copies as a code block' do
            verify(
              '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_10"]',

              <<-GFM.strip_heredoc,
                ```ruby
                      raise RuntimeError, "System commands must be given as an array of strings"
                    end
                ```
              GFM

              target: '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"]'
            )
          end
602 603
        end
      end
Douwe Maan's avatar
Douwe Maan committed
604

605 606 607 608
      context 'inline diff' do
        before do
          visit project_commit_path(project, sample_commit.id, view: 'inline')
        end
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
        it_behaves_like 'copying code from a diff'
      end

      context 'parallel diff' do
        before do
          visit project_commit_path(project, sample_commit.id, view: 'parallel')
        end

        it_behaves_like 'copying code from a diff'

        context 'selecting code on the left' do
          it 'copies as a code block' do
            verify(
              '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_8_8"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_9_9"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_10"]',

              <<-GFM.strip_heredoc,
                ```ruby
                    unless cmd.is_a?(Array)
                      raise "System commands must be given as an array of strings"
                    end
                ```
              GFM

              target: '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_8_8"].left-side'
            )
          end
        end

        context 'selecting code on the right' do
          it 'copies as a code block' do
            verify(
              '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_8_8"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_9_9"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_9"], [id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_10_10"]',

              <<-GFM.strip_heredoc,
                ```ruby
                    unless cmd.is_a?(Array)
                      raise RuntimeError, "System commands must be given as an array of strings"
                    end
                ```
              GFM

              target: '[id="2f6fcd96b88b36ce98c38da085c795a27d92a3dd_8_8"].right-side'
            )
          end
654 655 656 657 658 659
        end
      end
    end

    context 'from a blob' do
      before do
660
        visit project_blob_path(project, File.join('master', 'files/ruby/popen.rb'))
661
        wait_for_requests
662
      end
Douwe Maan's avatar
Douwe Maan committed
663

664 665 666 667
      context 'selecting one word of text' do
        it 'copies as inline code' do
          verify(
            '.line[id="LC9"] .no',
Douwe Maan's avatar
Douwe Maan committed
668

669 670 671 672
            '`RuntimeError`'
          )
        end
      end
Douwe Maan's avatar
Douwe Maan committed
673

674 675 676 677
      context 'selecting one line of text' do
        it 'copies as inline code' do
          verify(
            '.line[id="LC9"]',
678

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
            '`raise RuntimeError, "System commands must be given as an array of strings"`'
          )
        end
      end

      context 'selecting multiple lines of text' do
        it 'copies as a code block' do
          verify(
            '.line[id="LC9"], .line[id="LC10"]',

            <<-GFM.strip_heredoc,
              ```ruby
                    raise RuntimeError, "System commands must be given as an array of strings"
                  end
              ```
            GFM
          )
        end
      end
    end
699

700 701
    context 'from a GFM code block' do
      before do
702
        visit project_blob_path(project, File.join('markdown', 'doc/api/users.md'))
703
        wait_for_requests
704
      end
705

706 707 708 709
      context 'selecting one word of text' do
        it 'copies as inline code' do
          verify(
            '.line[id="LC27"] .s2',
710

711 712 713 714
            '`"bio"`'
          )
        end
      end
715

716 717 718 719
      context 'selecting one line of text' do
        it 'copies as inline code' do
          verify(
            '.line[id="LC27"]',
720

721 722 723 724
            '`"bio": null,`'
          )
        end
      end
725

726
      context 'selecting multiple lines of text' do
727
        it 'copies as a code block with the correct language' do
728 729 730 731 732 733 734 735 736 737 738 739 740
          verify(
            '.line[id="LC27"], .line[id="LC28"]',

            <<-GFM.strip_heredoc,
              ```json
                  "bio": null,
                  "skype": "",
              ```
            GFM
          )
        end
      end
    end
741

742
    def verify(selector, gfm, target: nil)
743
      html = html_for_selector(selector)
744
      output_gfm = html_to_gfm(html, 'transformCodeSelection', target: target)
745 746
      expect(output_gfm.strip).to eq(gfm.strip)
    end
747 748
  end

749 750 751 752
  def html_for_selector(selector)
    js = <<-JS.strip_heredoc
      (function(selector) {
        var els = document.querySelectorAll(selector);
Phil Hughes's avatar
Phil Hughes committed
753
        var htmls = [].slice.call(els).map(function(el) { return el.outerHTML; });
754 755 756 757 758
        return htmls.join("\\n");
      })("#{escape_javascript(selector)}")
    JS
    page.evaluate_script(js)
  end
Douwe Maan's avatar
Douwe Maan committed
759

760
  def html_to_gfm(html, transformer = 'transformGFMSelection', target: nil)
761 762
    js = <<-JS.strip_heredoc
      (function(html) {
763
        var transformer = window.CopyAsGFM[#{transformer.inspect}];
Douwe Maan's avatar
Douwe Maan committed
764

765
        var node = document.createElement('div');
766 767 768 769 770 771 772
        $(html).each(function() { node.appendChild(this) });

        var targetSelector = #{target.to_json};
        var target;
        if (targetSelector) {
          target = document.querySelector(targetSelector);
        }
Douwe Maan's avatar
Douwe Maan committed
773

774
        node = transformer(node, target);
Douwe Maan's avatar
Douwe Maan committed
775 776
        if (!node) return null;

777
        return window.CopyAsGFM.nodeToGFM(node);
778 779 780 781 782
      })("#{escape_javascript(html)}")
    JS
    page.evaluate_script(js)
  end
end