Browse Source

Making progress on pretty printing

pretty-print
Dustin Wilson 3 years ago
parent
commit
e38b2be10f
  1. 39
      lib/Parser/Serializer.php

39
lib/Parser/Serializer.php

@ -100,12 +100,12 @@ abstract class Serializer {
if ($reformatWhitespace) { if ($reformatWhitespace) {
$hasChildNodes = $n->hasChildNodes(); $hasChildNodes = $n->hasChildNodes();
$modify = false; $modify = false;
$isPreformattedContent = self::isPreformattedContent(); $isPreformattedContent = self::isPreformattedContent($n);
// If the node is an HTML element and its tag name is in the list of // If the node is an HTML element and its tag name is in the list of
// preformatted elements or if the node is not an html element and if the // preformatted elements or if the node is not an html element and if the
// element is preformatted content then there's no need to modify. // element is preformatted content then there's no need to modify.
if ((($htmlElement && in_array($tagName, self::PREFORMATTED_ELEMENTS) || !$htmlElement) && $isPreformattedContent) { if ((($htmlElement && in_array($tagName, self::PREFORMATTED_ELEMENTS)) || !$htmlElement) && $isPreformattedContent) {
$modify = false; $modify = false;
} }
// If the node is an HTML element, and either its tag name is in the list of // If the node is an HTML element, and either its tag name is in the list of
@ -132,7 +132,8 @@ abstract class Serializer {
// If we're modifying whitespace... // If we're modifying whitespace...
if ($modify) { if ($modify) {
$previousNonTextNodeSiblingName === null; $previousNonTextNodeSiblingName === null;
while ($nn = $n->previousSibling) { $nn = $n;
while ($nn = $nn->previousSibling) {
if (!$nn instanceof \DOMText) { if (!$nn instanceof \DOMText) {
$previousNonTextNodeSiblingName = $nn->nodeName; $previousNonTextNodeSiblingName = $nn->nodeName;
break; break;
@ -145,9 +146,9 @@ abstract class Serializer {
$s .= "\n"; $s .= "\n";
} }
if (!$first) { //if (!$first) {
$s .= "\n" . str_repeat($indentChar, $indentionLevel * $indentStep); $s .= "\n" . str_repeat($indentChar, $indentionLevel * $indentStep);
} //}
} }
} }
@ -249,7 +250,7 @@ abstract class Serializer {
// If reformatting whitespace indention needs to be applied to the template's // If reformatting whitespace indention needs to be applied to the template's
// serialized contents if not preformatted content. // serialized contents if not preformatted content.
if ($reformatWhitespace && !$isPreformattedContent && $indentionLevel > 0) { if ($reformatWhitespace && !$isPreformattedContent && $indentionLevel > 0) {
$ss = explode("\n", $ss); $ss = explode("\n", $ss);
foreach ($ss as $key => $value) { foreach ($ss as $key => $value) {
$ss[$key] = str_repeat($indentChar, $indentionLevel * $indentStep) . $ss[$key]; $ss[$key] = str_repeat($indentChar, $indentionLevel * $indentStep) . $ss[$key];
} }
@ -307,11 +308,11 @@ abstract class Serializer {
# Otherwise, append the value of current node's data IDL attribute, escaped as described below. # Otherwise, append the value of current node's data IDL attribute, escaped as described below.
else { else {
$t = $n->data; $t = $n->data;
if ($reformatWhitespace && !self::isPreformattedContent()) { if ($reformatWhitespace && !self::isPreformattedContent($n)) {
if (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n)) { if (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n)) {
// If the text node's data is made up of only whitespace characters continue // If the text node's data is made up of only whitespace characters continue
// onto the next node // onto the next node
if (strspn($text, Data::WHITESPACE) === strlen($t)) { if (strspn($t, Data::WHITESPACE) === strlen($t)) {
continue; continue;
} }
} }
@ -325,9 +326,10 @@ abstract class Serializer {
} }
# If current node is a Comment # If current node is a Comment
elseif ($n instanceof \DOMComment) { elseif ($n instanceof \DOMComment) {
if ($reformatWhitespace && !$first && !self::isPreformattedContent() && (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n))) { if ($reformatWhitespace /*&& !$first*/ && !self::isPreformattedContent($n) && (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n))) {
$previousNonTextNodeSiblingName === null; $previousNonTextNodeSiblingName === null;
while ($nn = $n->previousSibling) { $nn = $n;
while ($nn = $nn->previousSibling) {
if (!$nn instanceof \DOMText) { if (!$nn instanceof \DOMText) {
$previousNonTextNodeSiblingName = $nn->nodeName; $previousNonTextNodeSiblingName = $nn->nodeName;
break; break;
@ -352,8 +354,9 @@ abstract class Serializer {
} }
# If current node is a ProcessingInstruction # If current node is a ProcessingInstruction
elseif ($n instanceof \DOMProcessingInstruction) { elseif ($n instanceof \DOMProcessingInstruction) {
if ($reformatWhitespace && !$first && !self::isPreformattedContent() && (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n))) { if ($reformatWhitespace /* && !$first */ && !self::isPreformattedContent($n) && (self::treatAsBlock($n) || self::treatForeignContentAsBlock($n))) {
$previousNonTextNodeSiblingName === null; $previousNonTextNodeSiblingName === null;
$nn = $n;
while ($nn = $n->previousSibling) { while ($nn = $n->previousSibling) {
if (!$nn instanceof \DOMText) { if (!$nn instanceof \DOMText) {
$previousNonTextNodeSiblingName = $nn->nodeName; $previousNonTextNodeSiblingName = $nn->nodeName;
@ -470,8 +473,10 @@ abstract class Serializer {
// NOTE: This method is used only when pretty printing. Implementors of userland // NOTE: This method is used only when pretty printing. Implementors of userland
// PHP DOM solutions with template contents will need to extend this method to // PHP DOM solutions with template contents will need to extend this method to
// be able to moonwalk through document fragment hosts. // be able to moonwalk through document fragment hosts.
while ($n = $node->parentNode) {
if (($n->namespaceURI ?? Parser::HTML_NAMESPACE) === Parser::HTML_NAMESPACE && in_array($n->tagName, self::PREFORMATTED_ELEMENTS)) { $n = $node;
while ($n = $n->parentNode) {
if ($n instanceof \DOMElement && ($n->namespaceURI ?? Parser::HTML_NAMESPACE) === Parser::HTML_NAMESPACE && in_array($n->tagName, self::PREFORMATTED_ELEMENTS)) {
return true; return true;
} }
} }
@ -484,6 +489,11 @@ abstract class Serializer {
// PHP DOM solutions with template contents will need to extend this method to // PHP DOM solutions with template contents will need to extend this method to
// check for any templates and look within their content fragments for "block" // check for any templates and look within their content fragments for "block"
// content. // content.
if ($node->parentNode === null) {
return false;
}
$xpath = new \DOMXPath($node->ownerDocument); $xpath = new \DOMXPath($node->ownerDocument);
return ($xpath->evaluate(self::BLOCK_QUERY, $node->parentNode) > 0); return ($xpath->evaluate(self::BLOCK_QUERY, $node->parentNode) > 0);
} }
@ -492,7 +502,8 @@ abstract class Serializer {
// NOTE: This method is used only when pretty printing. Implementors of userland // NOTE: This method is used only when pretty printing. Implementors of userland
// PHP DOM solutions with template contents will need to extend this method to // PHP DOM solutions with template contents will need to extend this method to
// be able to moonwalk through document fragment hosts. // be able to moonwalk through document fragment hosts.
while ($n = $node->parentNode) { $n = $node;
while ($n = $n->parentNode) {
if (($n->parentNode->namespaceURI ?? Parser::HTML_NAMESPACE) === Parser::HTML_NAMESPACE) { if (($n->parentNode->namespaceURI ?? Parser::HTML_NAMESPACE) === Parser::HTML_NAMESPACE) {
if (self::treatAsBlock($n)) { if (self::treatAsBlock($n)) {
return true; return true;

Loading…
Cancel
Save