From 87df996e5e0edc7dd47239060af1d16b9f5f4154 Mon Sep 17 00:00:00 2001 From: Thijs Kok Date: Thu, 7 Mar 2024 23:26:46 +0100 Subject: [PATCH 1/2] Ignore unknown node types --- src/Node/BlockNode.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Node/BlockNode.php b/src/Node/BlockNode.php index 9dac9bc..b7ee878 100644 --- a/src/Node/BlockNode.php +++ b/src/Node/BlockNode.php @@ -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); From 587f8840c84a71a95e17ccac2bb5abc9f1413e48 Mon Sep 17 00:00:00 2001 From: Thijs Kok Date: Fri, 8 Mar 2024 00:06:29 +0100 Subject: [PATCH 2/2] Added test --- tests/Node/Block/DocumentTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Node/Block/DocumentTest.php b/tests/Node/Block/DocumentTest.php index 9645e76..0ad77ac 100644 --- a/tests/Node/Block/DocumentTest.php +++ b/tests/Node/Block/DocumentTest.php @@ -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()); + } }