diff --git a/lib/DOM/traits/Serialize.php b/lib/DOM/traits/Serialize.php index 6a352ab..b576fa7 100644 --- a/lib/DOM/traits/Serialize.php +++ b/lib/DOM/traits/Serialize.php @@ -23,12 +23,32 @@ trait Serialize { $node = $node->content; } - # 3. For each child node of the node, in tree order, run the following steps: - for ($i = 0; $i < $node->childNodes->length; $i++) { - # 1. Let current node be the child node being processed. - # 2. Append the appropriate string from the following list to s: - // Implementing this by allowing each of the node types to serialize themselves. - $s .= $node->childNodes->item($i); + $nodesLength = $node->childNodes->length; + if ($nodesLength > 0) { + // If the provided node is a document node and the first element in + // the tree is a document type then print the document type. There's + // no sense in checking for this on every single element in the tree. + // If the document type is present it will always be the first node + // because of how PHP's XML DOM works. + $start = 0; + if ($node->nodeType === XML_DOCUMENT_NODE && $node->childNodes->item(0)->nodeType === XML_DOCUMENT_TYPE_NODE) { + # Append the literal string "" (U+003E + # GREATER-THAN SIGN). + $s .= "childNodes->item(0)->name}>"; + $start = 1; + } + + # 3. For each child node of the node, in tree order, run the following steps: + for ($i = $start; $i < $nodesLength; $i++) { + # 1. Let current node be the child node being processed. + # 2. Append the appropriate string from the following list to s: + $s .= $node->childNodes->item($i); + } } # 4. The result of the algorithm is the string s.