From 787d03e48471ba62cd830379428f04d996f0b74b Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 17 Feb 2022 18:13:00 +0100 Subject: model update --- lib/HtmlFormatter.php | 252 +++++++++++++++++++++++++------------------------- 1 file changed, 126 insertions(+), 126 deletions(-) (limited to 'lib/HtmlFormatter.php') diff --git a/lib/HtmlFormatter.php b/lib/HtmlFormatter.php index 89605ac..0101f1b 100644 --- a/lib/HtmlFormatter.php +++ b/lib/HtmlFormatter.php @@ -1,126 +1,126 @@ -)/U', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); - $dom = self::parseDom($elements); - - $indent = 0; - $output = array(); - foreach ($dom as $index => $element) - { - if ($element['opening']) - { - $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); - - // make sure that only the elements who have not been blacklisted are being indented - if ( ! in_array($element['type'], explode(',', $tagsWithoutIndentation))) - { - ++$indent; - } - } - else if ($element['standalone']) - { - $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); - } - else if ($element['closing']) - { - --$indent; - $lf = "\n".str_repeat($indentWith, abs($indent)); - if (isset($dom[$index - 1]) && $dom[$index - 1]['opening']) - { - $lf = ''; - } - $output[] = $lf.trim($element['content']); - } - else if ($element['text']) - { - // $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); - $output[] = "\n".str_repeat($indentWith, $indent).preg_replace('/ [ \t]*/', ' ', $element['content']); - } - else if ($element['comment']) - { - $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); - } - } - - return trim(implode('', $output)); - } - - /** - * Parses an array of HTML tokens and adds basic information about about the type of - * tag the token represents. - * - * @param Array $elements Array of HTML tokens (tags and text tokens). - * @return Array HTML elements with extra information. - */ - public static function parseDom(Array $elements) - { - $dom = array(); - foreach ($elements as $element) - { - $isText = false; - $isComment = false; - $isClosing = false; - $isOpening = false; - $isStandalone = false; - - $currentElement = trim($element); - - // comment - if (strpos($currentElement, '$/', $currentElement)) - { - $isStandalone = true; - } - // normal opening tag - else if (strpos($currentElement, '<') === 0) - { - $isOpening = true; - } - // text - else - { - $isText = true; - } - - $dom[] = array( - 'text' => $isText, - 'comment' => $isComment, - 'closing' => $isClosing, - 'opening' => $isOpening, - 'standalone' => $isStandalone, - 'content' => $element, - 'type' => preg_replace('/^<\/?(\w+)[ >].*$/U', '$1', $element) - ); - } - return $dom; - } -} +)/U', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); + $dom = self::parseDom($elements); + + $indent = 0; + $output = array(); + foreach ($dom as $index => $element) + { + if ($element['opening']) + { + $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); + + // make sure that only the elements who have not been blacklisted are being indented + if ( ! in_array($element['type'], explode(',', $tagsWithoutIndentation))) + { + ++$indent; + } + } + else if ($element['standalone']) + { + $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); + } + else if ($element['closing']) + { + --$indent; + $lf = "\n".str_repeat($indentWith, abs($indent)); + if (isset($dom[$index - 1]) && $dom[$index - 1]['opening']) + { + $lf = ''; + } + $output[] = $lf.trim($element['content']); + } + else if ($element['text']) + { + // $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); + $output[] = "\n".str_repeat($indentWith, $indent).preg_replace('/ [ \t]*/', ' ', $element['content']); + } + else if ($element['comment']) + { + $output[] = "\n".str_repeat($indentWith, $indent).trim($element['content']); + } + } + + return trim(implode('', $output)); + } + + /** + * Parses an array of HTML tokens and adds basic information about about the type of + * tag the token represents. + * + * @param Array $elements Array of HTML tokens (tags and text tokens). + * @return Array HTML elements with extra information. + */ + public static function parseDom(Array $elements) + { + $dom = array(); + foreach ($elements as $element) + { + $isText = false; + $isComment = false; + $isClosing = false; + $isOpening = false; + $isStandalone = false; + + $currentElement = trim($element); + + // comment + if (strpos($currentElement, '$/', $currentElement)) + { + $isStandalone = true; + } + // normal opening tag + else if (strpos($currentElement, '<') === 0) + { + $isOpening = true; + } + // text + else + { + $isText = true; + } + + $dom[] = array( + 'text' => $isText, + 'comment' => $isComment, + 'closing' => $isClosing, + 'opening' => $isOpening, + 'standalone' => $isStandalone, + 'content' => $element, + 'type' => preg_replace('/^<\/?(\w+)[ >].*$/U', '$1', $element) + ); + } + return $dom; + } +} -- cgit v1.2.3