Commit b8638500 authored by Fatih Acet's avatar Fatih Acet

Fix copy markdown w/ elements with no text content

Copying and pasting markdown content
wasn’t working for DOM elements which
have no text content, eg: image tag.

This commit will fix it.
parent 61fb75f7
...@@ -327,7 +327,10 @@ export const getSelectedFragment = restrictToNode => { ...@@ -327,7 +327,10 @@ export const getSelectedFragment = restrictToNode => {
documentFragment.originalNodes.push(range.commonAncestorContainer); documentFragment.originalNodes.push(range.commonAncestorContainer);
} }
} }
if (documentFragment.textContent.length === 0) return null;
if (documentFragment.textContent.length === 0 && documentFragment.children.length === 0) {
return null;
}
return documentFragment; return documentFragment;
}; };
......
---
title: Fix copy markdown with elements with no text content
merge_request: 24020
author:
type: fixed
...@@ -52,6 +52,7 @@ describe('ShortcutsIssuable', function() { ...@@ -52,6 +52,7 @@ describe('ShortcutsIssuable', function() {
return documentFragment; return documentFragment;
}); });
}; };
describe('with empty selection', () => { describe('with empty selection', () => {
it('does not return an error', () => { it('does not return an error', () => {
ShortcutsIssuable.replyWithSelectedText(true); ShortcutsIssuable.replyWithSelectedText(true);
...@@ -297,5 +298,18 @@ describe('ShortcutsIssuable', function() { ...@@ -297,5 +298,18 @@ describe('ShortcutsIssuable', function() {
}); });
}); });
}); });
describe('with a valid selection with no text content', () => {
it('returns the proper markdown', done => {
stubSelection('<img src="foo" alt="image" />');
ShortcutsIssuable.replyWithSelectedText(true);
setTimeout(() => {
expect($(FORM_SELECTOR).val()).toBe('> ![image](http://localhost:9876/foo)\n\n');
done();
});
});
});
}); });
}); });
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