Skip to content
Snippets Groups Projects
Commit 08a8f95e authored by Olav Morken's avatar Olav Morken
Browse files

Utilities: Add function to format DOM elements

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@933 44740490-163a-0410-bde0-09ae8108e29a
parent 9437553f
No related branches found
No related tags found
No related merge requests found
...@@ -1541,6 +1541,90 @@ class SimpleSAML_Utilities { ...@@ -1541,6 +1541,90 @@ class SimpleSAML_Utilities {
return $ret; return $ret;
} }
/**
* Format a DOM element.
*
* This function takes in a DOM element, and inserts whitespace to make it more
* readable. Note that whitespace added previously will be removed.
*
* @param DOMElement $root The root element which should be formatted.
* @param string $indentBase The indentation this element should be assumed to
* have. Default is an empty string.
*/
public static function formatDOMElement(DOMElement $root, $indentBase = '') {
assert(is_string($indentBase));
/* Check what this element contains. */
$fullText = ''; /* All text in this element. */
$textNodes = array(); /* Text nodes which should be deleted. */
$childNodes = array(); /* Other child nodes. */
for ($i = 0; $i < $root->childNodes->length; $i++) {
$child = $root->childNodes->item($i);
if($child instanceof DOMText) {
$textNodes[] = $child;
$fullText .= $child->wholeText;
} elseif ($child instanceof DOMComment || $child instanceof DOMElement) {
$childNodes[] = $child;
} else {
/* Unknown node type. We don't know how to format this. */
return;
}
}
$fullText = trim($fullText);
if (strlen($fullText) > 0) {
/* We contain text. */
$hasText = TRUE;
} else {
$hasText = FALSE;
}
$hasChildNode = (count($childNodes) > 0);
if ($hasText && $hasChildNode) {
/* Element contains both text and child nodes - we don't know how to format this one. */
return;
}
/* Remove text nodes. */
foreach ($textNodes as $node) {
$root->removeChild($node);
}
if ($hasText) {
/* Only text - add a single text node to the element with the full text. */
$root->appendChild(new DOMText($fullText));
return;
}
if (!$hasChildNode) {
/* Empty node. Nothing to do. */
return;
}
/* Element contains only child nodes - add indentation before each one, and
* format child elements.
*/
$childIndentation = $indentBase . ' ';
foreach ($childNodes as $node) {
/* Add indentation before node. */
$root->insertBefore(new DOMText("\n" . $childIndentation), $node);
/* Format child elements. */
if ($node instanceof DOMElement) {
self::formatDOMElement($node, $childIndentation);
}
}
/* Add indentation before closing tag. */
$root->appendChild(new DOMText("\n" . $indentBase));
}
} }
?> ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment