-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTableParser.php
More file actions
55 lines (40 loc) · 1.61 KB
/
TableParser.php
File metadata and controls
55 lines (40 loc) · 1.61 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
<?php
namespace SyntaxPhoenix\EJSParserBundle\Parser\Extension;
use DOMElement;
use DOMDocument;
use Masterminds\HTML5;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
class TableParser implements EditorjsParserExtension
{
public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, string $prefix): DOMElement
{
$wrapper = $document->createElement('div');
$wrapper->setAttribute('class', "{$prefix}-table");
$table = $document->createElement('table');
$tableBody = $document->createElement('tbody');
$tableHead = $document->createElement('thead');
$withHeadings = isset($block->data->withHeadings) && $block->data->withHeadings === true;
foreach ($block->data->content as $i => $row) {
$isHeadingRow = $i === 0 && $withHeadings;
$tableRow = $document->createElement('tr');
foreach ($row as $item) {
$tableDefinition = $document->createElement($isHeadingRow ? 'th' : 'td');
if (strlen($item) > 0) {
$tableDefinition->appendChild($html5->loadHTMLFragment($item));
}
$tableRow->appendChild($tableDefinition);
}
if ($isHeadingRow) {
$tableHead->appendChild($tableRow);
} else {
$tableBody->appendChild($tableRow);
}
}
if ($withHeadings) {
$table->appendChild($tableHead);
}
$table->appendChild($tableBody);
$wrapper->appendChild($table);
return $wrapper;
}
}