Skip to content

Commit

Permalink
feat: Added a Method to Fix Bare Line Feed
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 3, 2024
1 parent c8b8a52 commit 23a0dc0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public function test09() {
$compiler = new TemplateCompiler('template.html');
$this->assertEquals("<div v-if=\"someVar <= 6 || someVar >= 8 || someVar === 6\">\r\n"
. " <script>\r\n"
. " \r\n"
. " function allIsGood() {\r\n"
. " if (a > 6) {\r\n"
. " alert(\"Oh. A is > 6 but probably < 100.\");\r\n"
Expand Down
36 changes: 35 additions & 1 deletion webfiori/ui/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,10 +756,44 @@ 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;
}
/**
* 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;
}

#[ReturnTypeWillChange]
/**
* Returns the element that the iterator is currently is pointing to.
Expand Down

0 comments on commit 23a0dc0

Please sign in to comment.