1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
package table
import (
"testing"
"github.com/JohannesKaufmann/html-to-markdown/v2/collapse"
"github.com/JohannesKaufmann/html-to-markdown/v2/internal/tester"
"golang.org/x/net/html"
)
func TestSelectRowNodes(t *testing.T) {
runs := []struct {
desc string
input string
expected string
}{
{
desc: "invalid table",
input: `
<table>
<tbody>
<tr>there is no data cell tag</tr>
</tbody>
</table>
`,
// Note: "golang.org/x/net/html" automatically cleans up the "table"
expected: `
├─body
│ ├─#text "there is no data cell tag"
│ ├─table
│ │ ├─tbody
│ │ │ ├─tr (__test_normal_row__="true")
`,
},
{
desc: "completely empty table",
input: `<table></table>`,
expected: `
├─body
│ ├─table
`,
},
{
desc: "completely empty tbody",
input: `<table><tbody></tbody></table>`,
expected: `
├─body
│ ├─table
│ │ ├─tbody
`,
},
{
desc: "basic table",
input: `
<table>
<tr>
<td>A1</td>
<td>A2</td>
</tr>
<tr>
<td>B1</td>
<td>B2</td>
</tr>
</table>
`,
// Note: "golang.org/x/net/html" automatically adds the "tbody"
expected: `
├─body
│ ├─table
│ │ ├─tbody
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "A1"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "A2"
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "B1"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "B2"
`,
},
{
desc: "basic table with th",
input: `
<table>
<tr>
<th>Heading 1</td>
<th>Heading 2</td>
</tr>
<tr>
<td>A1</td>
<td>A2</td>
</tr>
</table>
`,
expected: `
├─body
│ ├─table
│ │ ├─tbody
│ │ │ ├─tr (__test_header_row__="true")
│ │ │ │ ├─th
│ │ │ │ │ ├─#text "Heading 1"
│ │ │ │ ├─th
│ │ │ │ │ ├─#text "Heading 2"
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "A1"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "A2"
`,
},
{
desc: "with caption, thead, tbody, tfoot",
input: `
<table>
<caption>
A description about the table
</caption>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">City</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Max Mustermann</th>
<td>Berlin</td>
<td>20</td>
</tr>
<tr>
<th scope="row">Max Müller</th>
<td>München</td>
<td>30</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row" colspan="2">Average age</th>
<td>25</td>
</tr>
</tfoot>
</table>
`,
expected: `
├─body
│ ├─table
│ │ ├─caption
│ │ │ ├─#text "A description about the table"
│ │ ├─thead
│ │ │ ├─tr (__test_header_row__="true")
│ │ │ │ ├─th (scope="col")
│ │ │ │ │ ├─#text "Name"
│ │ │ │ ├─th (scope="col")
│ │ │ │ │ ├─#text "City"
│ │ │ │ ├─th (scope="col")
│ │ │ │ │ ├─#text "Age"
│ │ ├─tbody
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─th (scope="row")
│ │ │ │ │ ├─#text "Max Mustermann"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "Berlin"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "20"
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─th (scope="row")
│ │ │ │ │ ├─#text "Max Müller"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "München"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "30"
│ │ ├─tfoot
│ │ │ ├─tr (__test_normal_row__="true")
│ │ │ │ ├─th (scope="row" colspan="2")
│ │ │ │ │ ├─#text "Average age"
│ │ │ │ ├─td
│ │ │ │ │ ├─#text "25"
`,
},
}
for _, run := range runs {
t.Run(run.desc, func(t *testing.T) {
doc := tester.Parse(t, run.input, "")
// NOTE FOR FUTURE: I discovered that "golang.org/x/net/html" automatically adds the "tbody".
// => So we probably don't need to do that much work beforehand.
collapse.Collapse(doc, nil)
{
// We can then see if we correctly *identified* all the necessary table components.
// For that we add an attribute (just for the test).
headerRow := selectHeaderRowNode(doc)
if headerRow != nil {
headerRow.Attr = append(headerRow.Attr, html.Attribute{
Key: "__test_header_row__",
Val: "true",
})
}
for _, n := range selectNormalRowNodes(doc, headerRow) {
n.Attr = append(n.Attr, html.Attribute{
Key: "__test_normal_row__",
Val: "true",
})
}
}
tester.ExpectRepresentation(t, doc, "output", run.expected)
})
}
}
|