diff --git a/source/FluidXml/FluidInsertionHandler.php b/source/FluidXml/FluidInsertionHandler.php index f47fd46..1c0e695 100644 --- a/source/FluidXml/FluidInsertionHandler.php +++ b/source/FluidXml/FluidInsertionHandler.php @@ -226,6 +226,11 @@ protected function attachNodes($parent, $nodes, $fn): array $context = []; foreach ($nodes as $el) { + // DOMDocumentType cannot be imported as a child node. + if ($el instanceof \DOMDocumentType) { + continue; + } + $el = $this->dom->importNode($el, true); $context[] = $fn($parent, $el); } diff --git a/specs/FluidXml.php b/specs/FluidXml.php index 6cac1ef..5132f19 100644 --- a/specs/FluidXml.php +++ b/specs/FluidXml.php @@ -142,6 +142,34 @@ assert_is_a($actual, \Exception::class); }); + + it('should import an XML file containing a DOCTYPE declaration', function () { + $file = "{$this->out_dir}.test_load_doctype.xml"; + $doc = "\n" + . "\n" + . ""; + + \file_put_contents($file, $doc); + $xml = FluidXml::load($file); + \unlink($file); + + $expected = "\n \n"; + assert_equal_xml($xml, $expected); + }); + }); + + describe('.addChild() with DOCTYPE', function () { + it('should not throw when adding an XML string with a DOCTYPE declaration', function () { + $xml = new FluidXml(null); + $xmlStr = "\n" + . "\n" + . ""; + + $xml->addChild($xmlStr); + + $expected = "\n \n"; + assert_equal_xml($xml, $expected); + }); }); if (\version_compare(\phpversion(), '7', '>=')) {