diff --git a/webfiori/ui/HTMLNode.php b/webfiori/ui/HTMLNode.php index e586353..de8448d 100644 --- a/webfiori/ui/HTMLNode.php +++ b/webfiori/ui/HTMLNode.php @@ -756,10 +756,11 @@ public static function createComment(string $text) : HTMLNode { */ public static function createTextNode(string $nodeText, bool $escHtmlEntities = true) : HTMLNode { $text = new HTMLNode(self::TEXT_NODE); - $text->setText($nodeText, $escHtmlEntities); + $text->setText(self::fixBareLineFeed($nodeText), $escHtmlEntities); return $text; } + #[ReturnTypeWillChange] /** * Returns the element that the iterator is currently is pointing to. @@ -789,6 +790,38 @@ public function current() { public function div(array $attributes = []) : HTMLNode { return $this->addChild(new HTMLNode(), $attributes); } + /** + * Removes bare line feed characters (LF) and replaces them with CRLF. + * + * A bare line feed is LF which was not preceded by a carriage return (CR). + * + * @param string $str The string to be fixed. + * + * @return string The method will return a string with all bare line feed + * characters replaced with CRLF. + */ + public static function fixBareLineFeed(string $str) : string { + $finalStr = ''; + $index = 0; + $len = strlen($str); + + for ($index = 0 ; $index < $len ; $index++) { + $char = $str[$index]; + + if ($char == "\n") { + if ($index != 0 && $str[$index - 1] != "\r") { + //Bare line feed found. Replace with \r\n + $finalStr = trim($finalStr).HTMLDoc::NL; + } else { + $finalStr .= $char; + } + } else { + $finalStr .= $char; + } + } + + return $finalStr; + } /** * Adds a <form> element to the body of the node. * diff --git a/webfiori/ui/TemplateCompiler.php b/webfiori/ui/TemplateCompiler.php index 2ffcb40..5d36344 100644 --- a/webfiori/ui/TemplateCompiler.php +++ b/webfiori/ui/TemplateCompiler.php @@ -135,49 +135,15 @@ public function compile(array $varsToPass = []) { */ public static function fromHTMLText(string $text, bool $asHTMLDocObj = true) { $nodesArr = self::htmlAsArray($text); - $TN = 'tag-name'; - $retVal = []; - + if (count($nodesArr) >= 1) { + $TN = 'tag-name'; + $retVal = []; + if ($asHTMLDocObj && ($nodesArr[0][$TN] == 'html' || $nodesArr[0][$TN] == '!DOCTYPE')) { - $retVal = new HTMLDoc(); - $retVal->getHeadNode()->removeAllChildNodes(); - $retVal->getBody()->removeAttributes(); - - for ($x = 0 ; $x < count($nodesArr) ; $x++) { - if ($nodesArr[$x][$TN] == 'html') { - $htmlNode = self::fromHTMLTextHelper00($nodesArr[$x]); - - for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) { - $child = $htmlNode->children()->get($y); - - if ($child->getNodeName() == 'head') { - $retVal->setHeadNode($child); - } else { - if ($child->getNodeName() == 'body') { - for ($z = 0 ; $z < $child->childrenCount() ; $z++) { - $node = $child->children()->get($z); - $retVal->addChild($node); - } - } - } - } - } else { - if ($nodesArr[$x][$TN] == 'head') { - $headNode = self::fromHTMLTextHelper00($nodesArr[$x]); - $retVal->setHeadNode($headNode); - } - } - } + $retVal = self::parseHTMLDoc($nodesArr); } else { - if (count($nodesArr) != 1) { - foreach ($nodesArr as $node) { - $asHtmlNode = self::fromHTMLTextHelper00($node); - $retVal[] = $asHtmlNode; - } - } else if (count($nodesArr) == 1) { - return self::fromHTMLTextHelper00($nodesArr[0]); - } + $retVal = self::parseHTMLNode($nodesArr); } return $retVal; @@ -185,6 +151,45 @@ public static function fromHTMLText(string $text, bool $asHTMLDocObj = true) { return null; } + private static function parseHTMLNode($nodesArr) { + if (count($nodesArr) != 1) { + $retVal = []; + foreach ($nodesArr as $node) { + $asHtmlNode = self::fromHTMLTextHelper00($node); + $retVal[] = $asHtmlNode; + } + return $retVal; + } else { + return self::fromHTMLTextHelper00($nodesArr[0]); + } + } + private static function parseHTMLDoc($children) : HTMLDoc { + $retVal = new HTMLDoc(); + $retVal->getHeadNode()->removeAllChildNodes(); + $retVal->getBody()->removeAttributes(); + $TN = 'tag-name'; + + for ($x = 0 ; $x < count($children) ; $x++) { + if ($children[$x][$TN] == 'html') { + $htmlNode = self::fromHTMLTextHelper00($children[$x]); + + for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) { + $child = $htmlNode->children()->get($y); + + if ($child->getNodeName() == 'head') { + $retVal->setHeadNode($child); + } else if ($child->getNodeName() == 'body') { + for ($z = 0 ; $z < $child->childrenCount() ; $z++) { + $node = $child->children()->get($z); + $retVal->addChild($node); + } + + } + } + } + } + return $retVal; + } /** * Returns an array that contains directories names of the calling files. *