Skip to content

Don't copy empty elements that were not selected on purpose as GFM

Douwe Maan requested to merge dm-copy-as-gfm-without-empty-elements into master

You can reproduce the bug this fixes right here in GitLab <9.3 by triple clicking "Heading 1" below, and either hitting r or copy-pasting the selection into the GFM textarea below. Tested in Chrome.


Heading 1

Heading 2


You'll see that in the inserted text, there is an extra heading without any content:

# Heading 1

## 

The reason is that when you triple-click a block-level element (in Chrome at least), it actually selects that element, as well as an empty copy of the the element that follows it. In the example above, this looks like:

<h1>Heading1</h1>

<h2></h2>

Before this change, our Copy-as-GFM logic would happily translate this <h2>[nothing]</h2> to ## [nothing], hence the result above.

With this change, we automatically trim when appropriate, and skip empty elements that need text in Markdown, so that the selection described above will result in the below, as expected:

# Heading 1

Merge request reports