-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHTMLStringTableParser.php
More file actions
242 lines (212 loc) · 6.64 KB
/
HTMLStringTableParser.php
File metadata and controls
242 lines (212 loc) · 6.64 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* @author Andrew Coulton <andrew@ingenerator.com>
* @licence BSD-3-Clause
*/
namespace Ingenerator\BehatTableAssert\TableParser\HTML;
use Behat\Gherkin\Node\TableNode;
use Dom\HTMLDocument;
use Ingenerator\BehatTableAssert\TableNode\PaddedTableNode;
use LibXMLError;
use Masterminds\HTML5;
use function Dom\import_simplexml;
/**
* Parses an HTML string for a <table> element into a TableNode. The table must have a single row
* in the <thead> and a <tbody> element (though this does not have to contain any rows). You can
* skip additional rows in the table by marking up like <tr data-behat-table="ignore">.
*
* If you need to prefix cell content with some string for behat (for example to differentiate
* two columns that would be visually different to a human user) add a data-behat-table-prefix
* attribute.
*
* <table>
* <thead>
* <tr data-behat-table="ignore">
* <th></th>
* <th colspan=2>Timings</th>
* <th></th>
* </tr>
* <tr>
* <th>Category</th>
* <th data-behat-table-prefix="Timings -">Before</th>
* <th data-behat-table-prefix="Timings -">After</th>
* <th>Count</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <th>Thing</th>
* <td>12.3</td>
* <td>14.2</td>
* <td>12</td>
* </tr>
* <tr>
* <th colspan="3">Total</th>
* <td>12</td>
* </tr>
* </tbody>
* </table>
*
* becomes:
*
* | Category | Timings - Before | Timings - After | Count |
* | Thing | 12.3 | 14.2 | 12 |
* | Total | ... | ... | 12 |
*
* @package Ingenerator\BehatTableAssert\TableParser\HTML
*/
class HTMLStringTableParser
{
/**
* @param string $html
*
* @return \Ingenerator\BehatTableAssert\TableNode\PaddedTableNode
*/
public function parse($html)
{
if ( ! (\is_string($html) && $html)) {
throw new \InvalidArgumentException('Expected an HTML string');
}
$html_table = $this->parseHTMLString($html);
if ($html_table->getName() !== 'table') {
throw new \InvalidArgumentException(
'Expected a <table> but got '.$html_table->getName()
);
}
$rows = $this->parseTable($html_table);
return new PaddedTableNode($rows);
}
/**
* @param string $html
*
* @return \SimpleXMLElement
*/
protected function parseHTMLString($html)
{
$html5 = new HTML5();
$dom = $html5->loadHTML(
'<!DOCTYPE html><html>'
.'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>'
.'<body>'.\trim($html).'</body>'
.'</html>',
);
$table_elem = $dom->getElementsByTagName('body')->item(0)->firstChild;
if (!$table_elem instanceof \DOMElement) {
throw new \InvalidArgumentException(
sprintf("Expected html root element but got %s\n\n===HTML===\n%s", get_debug_type($table_elem), $html),
);
}
$table = \simplexml_import_dom($table_elem);
if ($errors = $html5->getErrors()) {
throw new \InvalidArgumentException(
sprintf(
"Invalid HTML:\n%s\n\n===HTML===\n%s",
implode("\n", array_map(fn($err) => ' - '.$err, $errors)),
$html,
),
);
}
return $table;
}
/**
* @deprecated no longer called by the library
*/
protected function throwInvalidHTMLException($html, $errors)
{
$msg = 'Invalid HTML string:';
foreach ($errors as $error) {
$msg .= \strtr(
"\n #code:message (@line:column)",
\array_map('trim', (array) $error)
);
}
$msg .= "\n\n===HTML===\n$html";
throw new \InvalidArgumentException($msg);
}
/**
* @param \SimpleXMLElement $html_table
*
* @return array
*/
protected function parseTable(\SimpleXMLElement $html_table)
{
$header = $this->parseRows($this->requireSingleChild($html_table, 'thead'));
if (empty($header)) {
throw new \InvalidArgumentException('No <tr> found in <thead>');
} elseif (\count($header) > 1) {
throw new \InvalidArgumentException(
'Multiple <tr> found in <thead> - you can mark additional rows with data-behat-table="ignore"'
);
}
$body = $this->parseRows($this->requireSingleChild($html_table, 'tbody'));
return \array_merge($header, $body);
}
/**
* @param \SimpleXMLElement $html_table
* @param string $tag_name
*
* @return \SimpleXMLElement
*/
protected function requireSingleChild(\SimpleXMLElement $html_table, $tag_name)
{
if ( ! $child = $html_table->$tag_name) {
throw new \InvalidArgumentException(
'No <'.$tag_name.'> found in <'.$html_table->getName().'>'
);
}
/** @var \SimpleXmlElement $child */
if ($child->count() > 1) {
throw new \InvalidArgumentException(
'Multiple <'.$tag_name.'> found in <'.$html_table->getName().'>'
);
}
return $child;
}
/**
* @param \SimpleXMLElement $section
*
* @return array
*/
protected function parseRows(\SimpleXMLElement $section)
{
$rows = [];
foreach ($section->tr as $row) {
if ( (string) $row['data-behat-table'] === 'ignore') {
continue;
}
$rows[] = $this->findCellTextValues($row);
}
return $rows;
}
/**
* @param \SimpleXMLElement $table_row
*
* @return string[]
*/
protected function findCellTextValues(\SimpleXMLElement $table_row)
{
$row = [];
foreach ($table_row->children() as $child) {
/** @var \SimpleXMLElement $child */
$row[] = $this->parseCellText($child);
$colspan = (int) $child['colspan'];
for ($i = 1; $i < $colspan; $i++) {
$row[] = '...';
}
}
return $row;
}
/**
* @param \SimpleXmlElement $cell
*
* @return string
*/
protected function parseCellText(\SimpleXmlElement $cell)
{
$text = \trim(\preg_replace('/\s+/', ' ', \dom_import_simplexml($cell)->textContent));
if ($prefix = (string) $cell['data-behat-table-prefix']) {
$text = $prefix.' '.$text;
}
return $text;
}
}