Commit c846a832 authored by Filipa Lacerda's avatar Filipa Lacerda

Merge branch 'dm-copy-gfm-when-parts-of-other-elements-are-selected' into 'master'

Copy as GFM even when parts of other elements are selected

See merge request !11521
parents c013d23d f14bb942
...@@ -330,11 +330,27 @@ class CopyAsGFM { ...@@ -330,11 +330,27 @@ class CopyAsGFM {
} }
static transformGFMSelection(documentFragment) { static transformGFMSelection(documentFragment) {
// If the documentFragment contains more than just Markdown, don't copy as GFM. const gfmEls = documentFragment.querySelectorAll('.md, .wiki');
if (documentFragment.querySelector('.md, .wiki')) return null; switch (gfmEls.length) {
case 0: {
return documentFragment; return documentFragment;
} }
case 1: {
return gfmEls[0];
}
default: {
const allGfmEl = document.createElement('div');
for (let i = 0; i < gfmEls.length; i += 1) {
const lineEl = gfmEls[i];
allGfmEl.appendChild(lineEl);
allGfmEl.appendChild(document.createTextNode('\n\n'));
}
return allGfmEl;
}
}
}
static transformCodeSelection(documentFragment) { static transformCodeSelection(documentFragment) {
const lineEls = documentFragment.querySelectorAll('.line'); const lineEls = documentFragment.querySelectorAll('.line');
......
...@@ -38,7 +38,7 @@ import './shortcuts_navigation'; ...@@ -38,7 +38,7 @@ import './shortcuts_navigation';
} }
ShortcutsIssuable.prototype.replyWithSelectedText = function() { ShortcutsIssuable.prototype.replyWithSelectedText = function() {
var quote, documentFragment, selected, separator; var quote, documentFragment, el, selected, separator;
var replyField = $('.js-main-target-form #note_note'); var replyField = $('.js-main-target-form #note_note');
documentFragment = window.gl.utils.getSelectedFragment(); documentFragment = window.gl.utils.getSelectedFragment();
...@@ -47,10 +47,8 @@ import './shortcuts_navigation'; ...@@ -47,10 +47,8 @@ import './shortcuts_navigation';
return; return;
} }
// If the documentFragment contains more than just Markdown, don't copy as GFM. el = window.gl.CopyAsGFM.transformGFMSelection(documentFragment.cloneNode(true));
if (documentFragment.querySelector('.md, .wiki')) return; selected = window.gl.CopyAsGFM.nodeToGFM(el);
selected = window.gl.CopyAsGFM.nodeToGFM(documentFragment);
if (selected.trim() === "") { if (selected.trim() === "") {
return; return;
......
---
title: Copy as GFM even when parts of other elements are selected
merge_request:
author:
...@@ -78,6 +78,25 @@ describe 'Copy as GFM', feature: true, js: true do ...@@ -78,6 +78,25 @@ describe 'Copy as GFM', feature: true, js: true do
expect(output_gfm.strip).to eq(gfm.strip) expect(output_gfm.strip).to eq(gfm.strip)
end 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
output_gfm = html_to_gfm(html)
expect(output_gfm.strip).to eq(gfm.strip)
end
verify( verify(
'InlineDiffFilter', 'InlineDiffFilter',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment