Skip to content

Commit bece353

Browse files
author
Oliver Baumann
committed
Handle nodetype XML_COMMENT_NODE
Templates may contain XML-style comment nodes. These should be handled like text nodes, i.e. just pass them on into the output. Since they do not contain attributes or any other node properties that we can handle, they must be treated specially. Since the code now contains several different branches that depend on the node type, a switch-case seems a more aesthetic approach compared to multiple if-else.
1 parent 4812d51 commit bece353

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/Compiler.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,21 @@ public function convert(): string
5757

5858
public function convertNode(DOMNode $node): DOMNode
5959
{
60-
if ($node->nodeType === XML_TEXT_NODE) {
61-
return $node;
62-
}
63-
64-
if ($node->nodeType === XML_ELEMENT_NODE) {
65-
//echo "\nElement node found";
66-
/** @var DOMElement $node */
67-
$this->replaceShowWithIf($node);
68-
$this->handleIf($node);
69-
} elseif ($node->nodeType === XML_HTML_DOCUMENT_NODE) {
70-
$this->logger->warning("Document node found.");
60+
switch($node->nodeType) {
61+
case XML_TEXT_NODE:
62+
$this->logger->debug('Text node found', ['name' => $node->nodeName]);
63+
// fall through to next case, because we don't need to handle either of these node-types
64+
case XML_COMMENT_NODE:
65+
$this->logger->debug('Comment node found', ['name' => $node->nodeName]);
66+
return $node;
67+
case XML_ELEMENT_NODE:
68+
/** @var DOMElement $node */
69+
$this->replaceShowWithIf($node);
70+
$this->handleIf($node);
71+
break;
72+
case XML_HTML_DOCUMENT_NODE:
73+
$this->logger->warning("Document node found.");
74+
break;
7175
}
7276

7377
$this->stripEventHandlers($node);
@@ -315,4 +319,4 @@ protected function replacePlaceholders(string $string)
315319

316320
return $string;
317321
}
318-
}
322+
}

0 commit comments

Comments
 (0)