Skip to content

Commit

Permalink
Merge pull request #41 from WebFiori/dev
Browse files Browse the repository at this point in the history
feat: Added a Method to Fix Bare Line Feed
  • Loading branch information
usernane committed Jul 4, 2024
2 parents ccc48f9 + 22d40fc commit 3d01b28
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 41 deletions.
35 changes: 34 additions & 1 deletion webfiori/ui/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 &lt;form&gt; element to the body of the node.
*
Expand Down
85 changes: 45 additions & 40 deletions webfiori/ui/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,56 +135,61 @@ 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;
}

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.
*
Expand Down

0 comments on commit 3d01b28

Please sign in to comment.