Skip to content

Commit 614bc80

Browse files
authored
Fix support for tables without rows, cells
Closes GH-53. Closes GH-54. Closes GH-55. Co-authored-by: Titus Wormer <[email protected]>
1 parent f9d479f commit 614bc80

File tree

3 files changed

+70
-9
lines changed

3 files changed

+70
-9
lines changed

lib/handlers/table.js

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
module.exports = table
44

5+
var xtend = require('xtend')
56
var visit = require('unist-util-visit')
67
var all = require('../all')
78

89
function table(h, node) {
910
var align = alignment(node)
10-
return h(node, 'table', {align: align}, patch(all(h, node), align.length))
11+
return h(node, 'table', {align: align}, toRows(all(h, node), align.length))
1112
}
1213

1314
// Infer the alignment of the table.
@@ -60,23 +61,74 @@ function cell(node) {
6061
}
6162

6263
// Ensure the amount of cells in a row matches `align.left`.
63-
function patch(rows, count) {
64+
function toRows(rows, count) {
65+
var nodes = []
6466
var length = rows.length
6567
var index = -1
68+
var node
69+
var queue
6670

6771
while (++index < length) {
68-
one(rows[index], count)
72+
node = rows[index]
73+
74+
if (node.type === 'tableRow') {
75+
flush()
76+
add(node)
77+
} else {
78+
queue = (queue || []).concat(node)
79+
}
80+
}
81+
82+
flush()
83+
84+
return nodes
85+
86+
function flush() {
87+
if (queue) {
88+
add({type: 'tableRow', children: queue})
89+
queue = undefined
90+
}
6991
}
7092

71-
return rows
93+
function add(node) {
94+
nodes.push(cells(node, count))
95+
}
7296
}
7397

74-
function one(row, count) {
75-
var children = row.children
76-
var length = count + 1
77-
var index = children.length
98+
function cells(row, count) {
99+
var nodes = []
100+
var cells = row.children
101+
var length = cells.length
102+
var index = -1
103+
var node
104+
var queue
105+
106+
while (++index < length) {
107+
node = cells[index]
108+
109+
if (node.type === 'tableCell') {
110+
flush()
111+
nodes.push(node)
112+
} else {
113+
queue = (queue || []).concat(node)
114+
}
115+
}
116+
117+
flush()
118+
119+
index = nodes.length
120+
length = count + 1
78121

79122
while (++index < length) {
80-
children.push({type: 'tableCell', children: []})
123+
nodes.push({type: 'tableCell', children: []})
124+
}
125+
126+
return xtend(row, {children: nodes})
127+
128+
function flush() {
129+
if (queue) {
130+
nodes.push({type: 'tableCell', children: queue})
131+
queue = undefined
132+
}
81133
}
82134
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<table>
3+
<tr><th>Things</th></tr>
4+
<!-- alpha -->
5+
<input type="hidden" name="search_to" />
6+
</table>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Things |
2+
| -------------- |
3+
| <!-- alpha --> |

0 commit comments

Comments
 (0)