Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore unknown node types #3

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Node/BlockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static function load(array $data, ?self $parent = null): self
// set content if defined
if (\array_key_exists('content', $data)) {
foreach ($data['content'] as $nodeData) {
// ignore undefined node types
if (!isset(Node::NODE_MAPPING[$nodeData['type']])) {
continue;
}

$class = Node::NODE_MAPPING[$nodeData['type']];
$child = $class::load($nodeData, $node);

Expand Down
14 changes: 14 additions & 0 deletions tests/Node/Block/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,18 @@ public function testLoad(): void

self::assertJsonStringEqualsJsonString($json, $doc->toJson());
}

public function testLoadWithUnknownNodeType(): void
{
$json = <<<'TXT'
{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"hello"},{"type":"unknown","attrs":{"id":"abc"}},{"type":"text","text":" "},{"type":"text","text":"world"}]}]}
TXT;
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
$doc = Document::load($data);

$expectedOutput = <<<'TXT'
{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"hello"},{"type":"text","text":" "},{"type":"text","text":"world"}]}]}
TXT;
self::assertJsonStringEqualsJsonString($expectedOutput, $doc->toJson());
}
}