|
| 1 | +import subscribe from '../dist/index.esm.js' |
| 2 | + |
| 3 | +describe('paste-markdown', function() { |
| 4 | + describe('with quotable selection', function() { |
| 5 | + let subscription, textarea |
| 6 | + beforeEach(function() { |
| 7 | + document.body.innerHTML = ` |
| 8 | + <textarea data-paste-markdown></textarea> |
| 9 | + ` |
| 10 | + |
| 11 | + textarea = document.querySelector('textarea[data-paste-markdown]') |
| 12 | + subscription = subscribe(textarea) |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(function() { |
| 16 | + subscription.unsubscribe() |
| 17 | + document.body.innerHTML = '' |
| 18 | + }) |
| 19 | + |
| 20 | + it('pastes image uris as markdown', function() { |
| 21 | + paste(textarea, {'text/uri-list': 'https://github.com/github.png\r\nhttps://github.com/hubot.png'}) |
| 22 | + assert.include(textarea.value, '\n\n') |
| 23 | + }) |
| 24 | + |
| 25 | + it('pastes gfm into markdown', function() { |
| 26 | + paste(textarea, {'text/plain': 'hello', 'text/x-gfm': '# hello'}) |
| 27 | + assert.include(textarea.value, '# hello') |
| 28 | + }) |
| 29 | + |
| 30 | + it('pastes html tables as markdown', function() { |
| 31 | + const data = { |
| 32 | + 'text/html': ` |
| 33 | + <table> |
| 34 | + <thead><tr><th>name</th><th>origin</th></tr></thead> |
| 35 | + <tbody> |
| 36 | + <tr><td>hubot</td><td>github</td></tr> |
| 37 | + <tr><td>bender</td><td>futurama</td></tr> |
| 38 | + </tbody> |
| 39 | + </table> |
| 40 | + ` |
| 41 | + } |
| 42 | + paste(textarea, data) |
| 43 | + assert.include(textarea.value, 'name | origin\n-- | --\nhubot | github\nbender | futurama') |
| 44 | + }) |
| 45 | + |
| 46 | + it('does not paste excluded content as markdown', function() { |
| 47 | + const data = { |
| 48 | + 'text/html': ` |
| 49 | + <table class="js-comment"> |
| 50 | + <thead><tr><th>name</th><th>origin</th></tr></thead> |
| 51 | + <tbody> |
| 52 | + <tr><td>hubot</td><td>github</td></tr> |
| 53 | + <tr><td>bender</td><td>futurama</td></tr> |
| 54 | + </tbody> |
| 55 | + </table> |
| 56 | + ` |
| 57 | + } |
| 58 | + paste(textarea, data) |
| 59 | + assert.equal(textarea.value, '') |
| 60 | + }) |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +function paste(textarea, data) { |
| 65 | + const dataTransfer = new DataTransfer() |
| 66 | + for (const key in data) { |
| 67 | + dataTransfer.setData(key, data[key]) |
| 68 | + } |
| 69 | + const event = new ClipboardEvent('paste', { |
| 70 | + clipboardData: dataTransfer |
| 71 | + }) |
| 72 | + textarea.dispatchEvent(event) |
| 73 | +} |
0 commit comments