Skip to content

Commit 054275c

Browse files
authored
Merge pull request #22 from github/revert-advance-table-parsing
Revert "Merge pull request #20 from github/support-multiple-tables"
2 parents 9416e13 + 6a5747f commit 054275c

3 files changed

Lines changed: 12 additions & 50 deletions

File tree

src/paste-markdown-table.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,16 @@ function tableMarkdown(node: Element): string {
8484
function generateText(transfer: DataTransfer): string | undefined {
8585
if (Array.from(transfer.types).indexOf('text/html') === -1) return
8686

87-
let html = transfer.getData('text/html')
87+
const html = transfer.getData('text/html')
8888
if (!/<table/i.test(html)) return
8989

90-
html = html.replace(/<meta.*?>/, '')
91-
9290
const el = document.createElement('div')
9391
el.innerHTML = html
94-
const tables = el.querySelectorAll('table')
95-
96-
for (const table of tables) {
97-
if (table.closest('[data-paste-markdown-skip]')) {
98-
table.replaceWith(new Text(table.textContent || ''))
99-
}
100-
const formattedTable = tableMarkdown(table)
101-
table.replaceWith(new Text(formattedTable))
102-
}
92+
let table = el.querySelector('table')
93+
table = !table || table.closest('[data-paste-markdown-skip]') ? null : table
94+
if (!table) return
95+
96+
const formattedTable = tableMarkdown(table)
10397

104-
return el.innerHTML
98+
return html.replace(/<meta.*?>/, '').replace(/<table[.\S\s]*<\/table>/, `\n${formattedTable}`)
10599
}

test/test.js

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,7 @@ describe('paste-markdown', function () {
5757
assert.equal(
5858
textarea.value.trim(),
5959
// eslint-disable-next-line github/unescaped-html-literal
60-
'<p>Here is a cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Very cool</p>'
61-
)
62-
})
63-
64-
it('pastes multiple tables', async function () {
65-
const data = {
66-
'text/html': `
67-
<p>Here is a cool table</p>
68-
<table>
69-
<thead><tr><th>name</th><th>origin</th></tr></thead>
70-
<tbody>
71-
<tr><td>hubot</td><td>github</td></tr>
72-
<tr><td>bender</td><td>futurama</td></tr>
73-
</tbody>
74-
</table>
75-
<p>Another very cool table</p>
76-
<table>
77-
<thead><tr><th>name</th><th>origin</th></tr></thead>
78-
<tbody>
79-
<tr><td>hubot</td><td>github</td></tr>
80-
<tr><td>bender</td><td>futurama</td></tr>
81-
</tbody>
82-
</table>
83-
`
84-
}
85-
86-
paste(textarea, data)
87-
assert.equal(
88-
textarea.value.trim(),
89-
// eslint-disable-next-line github/unescaped-html-literal
90-
'<p>Here is a cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Another very cool table</p>\n \nname | origin\n-- | --\nhubot | github\nbender | futurama'
60+
'<p>Here is a cool table</p>\n \n\nname | origin\n-- | --\nhubot | github\nbender | futurama\n\n\n <p>Very cool</p>'
9161
)
9262
})
9363

@@ -105,10 +75,9 @@ describe('paste-markdown', function () {
10575
}
10676
paste(textarea, data)
10777

108-
assert.equal(
109-
textarea.value,
110-
'\n \n nameorigin\n \n hubotgithub\n benderfuturama\n \n \n '
111-
)
78+
// Synthetic paste events don't manipulate the DOM. A empty textarea
79+
// means that the event handler didn't fire and normal paste happened.
80+
assert.equal(textarea.value, '')
11281
})
11382

11483
it('accepts x-gfm', function () {

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
"target": "es2017",
55
"lib": [
66
"es2018",
7-
"dom",
8-
"dom.iterable"
7+
"dom"
98
],
109
"strict": true,
1110
"declaration": true,

0 commit comments

Comments
 (0)