Skip to content

Commit 3d01b28

Browse files
authored
Merge pull request #41 from WebFiori/dev
feat: Added a Method to Fix Bare Line Feed
2 parents ccc48f9 + 22d40fc commit 3d01b28

File tree

2 files changed

+79
-41
lines changed

2 files changed

+79
-41
lines changed

webfiori/ui/HTMLNode.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,10 +756,11 @@ public static function createComment(string $text) : HTMLNode {
756756
*/
757757
public static function createTextNode(string $nodeText, bool $escHtmlEntities = true) : HTMLNode {
758758
$text = new HTMLNode(self::TEXT_NODE);
759-
$text->setText($nodeText, $escHtmlEntities);
759+
$text->setText(self::fixBareLineFeed($nodeText), $escHtmlEntities);
760760

761761
return $text;
762762
}
763+
763764
#[ReturnTypeWillChange]
764765
/**
765766
* Returns the element that the iterator is currently is pointing to.
@@ -789,6 +790,38 @@ public function current() {
789790
public function div(array $attributes = []) : HTMLNode {
790791
return $this->addChild(new HTMLNode(), $attributes);
791792
}
793+
/**
794+
* Removes bare line feed characters (LF) and replaces them with CRLF.
795+
*
796+
* A bare line feed is LF which was not preceded by a carriage return (CR).
797+
*
798+
* @param string $str The string to be fixed.
799+
*
800+
* @return string The method will return a string with all bare line feed
801+
* characters replaced with CRLF.
802+
*/
803+
public static function fixBareLineFeed(string $str) : string {
804+
$finalStr = '';
805+
$index = 0;
806+
$len = strlen($str);
807+
808+
for ($index = 0 ; $index < $len ; $index++) {
809+
$char = $str[$index];
810+
811+
if ($char == "\n") {
812+
if ($index != 0 && $str[$index - 1] != "\r") {
813+
//Bare line feed found. Replace with \r\n
814+
$finalStr = trim($finalStr).HTMLDoc::NL;
815+
} else {
816+
$finalStr .= $char;
817+
}
818+
} else {
819+
$finalStr .= $char;
820+
}
821+
}
822+
823+
return $finalStr;
824+
}
792825
/**
793826
* Adds a &lt;form&gt; element to the body of the node.
794827
*

webfiori/ui/TemplateCompiler.php

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -135,56 +135,61 @@ public function compile(array $varsToPass = []) {
135135
*/
136136
public static function fromHTMLText(string $text, bool $asHTMLDocObj = true) {
137137
$nodesArr = self::htmlAsArray($text);
138-
$TN = 'tag-name';
139-
$retVal = [];
140-
138+
141139
if (count($nodesArr) >= 1) {
140+
$TN = 'tag-name';
141+
$retVal = [];
142+
142143
if ($asHTMLDocObj && ($nodesArr[0][$TN] == 'html' || $nodesArr[0][$TN] == '!DOCTYPE')) {
143-
$retVal = new HTMLDoc();
144-
$retVal->getHeadNode()->removeAllChildNodes();
145-
$retVal->getBody()->removeAttributes();
146-
147-
for ($x = 0 ; $x < count($nodesArr) ; $x++) {
148-
if ($nodesArr[$x][$TN] == 'html') {
149-
$htmlNode = self::fromHTMLTextHelper00($nodesArr[$x]);
150-
151-
for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) {
152-
$child = $htmlNode->children()->get($y);
153-
154-
if ($child->getNodeName() == 'head') {
155-
$retVal->setHeadNode($child);
156-
} else {
157-
if ($child->getNodeName() == 'body') {
158-
for ($z = 0 ; $z < $child->childrenCount() ; $z++) {
159-
$node = $child->children()->get($z);
160-
$retVal->addChild($node);
161-
}
162-
}
163-
}
164-
}
165-
} else {
166-
if ($nodesArr[$x][$TN] == 'head') {
167-
$headNode = self::fromHTMLTextHelper00($nodesArr[$x]);
168-
$retVal->setHeadNode($headNode);
169-
}
170-
}
171-
}
144+
$retVal = self::parseHTMLDoc($nodesArr);
172145
} else {
173-
if (count($nodesArr) != 1) {
174-
foreach ($nodesArr as $node) {
175-
$asHtmlNode = self::fromHTMLTextHelper00($node);
176-
$retVal[] = $asHtmlNode;
177-
}
178-
} else if (count($nodesArr) == 1) {
179-
return self::fromHTMLTextHelper00($nodesArr[0]);
180-
}
146+
$retVal = self::parseHTMLNode($nodesArr);
181147
}
182148

183149
return $retVal;
184150
}
185151

186152
return null;
187153
}
154+
private static function parseHTMLNode($nodesArr) {
155+
if (count($nodesArr) != 1) {
156+
$retVal = [];
157+
foreach ($nodesArr as $node) {
158+
$asHtmlNode = self::fromHTMLTextHelper00($node);
159+
$retVal[] = $asHtmlNode;
160+
}
161+
return $retVal;
162+
} else {
163+
return self::fromHTMLTextHelper00($nodesArr[0]);
164+
}
165+
}
166+
private static function parseHTMLDoc($children) : HTMLDoc {
167+
$retVal = new HTMLDoc();
168+
$retVal->getHeadNode()->removeAllChildNodes();
169+
$retVal->getBody()->removeAttributes();
170+
$TN = 'tag-name';
171+
172+
for ($x = 0 ; $x < count($children) ; $x++) {
173+
if ($children[$x][$TN] == 'html') {
174+
$htmlNode = self::fromHTMLTextHelper00($children[$x]);
175+
176+
for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) {
177+
$child = $htmlNode->children()->get($y);
178+
179+
if ($child->getNodeName() == 'head') {
180+
$retVal->setHeadNode($child);
181+
} else if ($child->getNodeName() == 'body') {
182+
for ($z = 0 ; $z < $child->childrenCount() ; $z++) {
183+
$node = $child->children()->get($z);
184+
$retVal->addChild($node);
185+
}
186+
187+
}
188+
}
189+
}
190+
}
191+
return $retVal;
192+
}
188193
/**
189194
* Returns an array that contains directories names of the calling files.
190195
*

0 commit comments

Comments
 (0)