/** * Trim away the minimum indentation level for all elements in $elements. * * @param ezcTemplateBlockTstNode $parentBlock The block which owns elements in $elements. * @param array(ezcTemplateTstNode) $elements List of elements to trim. */ public function trimBlockLevelIndentation(ezcTemplateTstNode $parentBlock, array $elements) { // First figure out the smallest amount of indentation that can be removed $indentation = $parentBlock->minimumWhitespaceColumn(); $nrOfElements = sizeof($elements); for ($el = 0; $el < $nrOfElements; $el++) { $element = $elements[$el]; if ($element instanceof ezcTemplateTextBlockTstNode) { $lines = $element->lines; $count = count($lines); for ($i = 0; $i < $count; ++$i) { // Skip the first line if it is placed after another element (column > 0 ). // We can only modify lines with leading point at column 0. if ($i == 0 && $element->firstLineColumn() > 0) { // It prevents some text nodes from removal. continue; } // Trim the line and leave EOL alone $lines[$i][0] = $this->trimIndentationLine($lines[$i][0], $indentation); if ($i == $count - 1) { if ($el < $nrOfElements - 1) { if ($elements[$el + 1] instanceof ezcTemplateBlockTstNode && !$elements[$el + 1] instanceof ezcTemplateOutputBlockTstNode) { $trimmed = trim($lines[$i][0], " \t"); if (strlen($trimmed) == 0) { $lines[$i][0] = ""; } } } else { if ($parentBlock instanceof ezcTemplateBlockTstNode && !$parentBlock instanceof ezcTemplateOutputBlockTstNode) { $last = sizeof($lines) - 1; $trimmed = trim($lines[$last][0], " \t"); if (strlen(trim($lines[$last][0], " \t")) == 0) { $lines[$last][0] = ""; } } } } } $element->setTextLines($lines); } elseif ($element instanceof ezcTemplateConditionBodyTstNode) { $this->trimBlockLevelIndentation($element, $element->children); } } }