Skip to content

Commit

Permalink
Move newline endings outside elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Spirit55555 committed Jun 23, 2020
1 parent 9b6fb6e commit e9561ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/MinecraftColors.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ static public function convertToHTML($text, $line_break_element = false, $css_cl
if ($open_tags != 0)
$text = $text.str_repeat(self::CLOSE_TAG, $open_tags);

//Move newline endings outside elements.
while (strpos($text, "\n".self::CLOSE_TAG) !== false)
$text = str_replace("\n".self::CLOSE_TAG, self::CLOSE_TAG."\n", $text);

//Replace \n with <br />
if ($line_break_element) {
$text = str_replace("\n", self::LINE_BREAK, $text);
$text = str_replace('\n', self::LINE_BREAK, $text);
}
if ($line_break_element)
$text = str_replace(array('\n', "\n"), self::LINE_BREAK, $text);

//Return the text without empty HTML tags. Only to clean up bad color formatting from the user.
return preg_replace(self::EMPTY_TAGS, '', $text);
Expand Down
8 changes: 4 additions & 4 deletions tests/MinecraftColorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function testClean(): void {
public function testConvertToHTML(): void {
$text = "§4Lorem §3§lipsum\n§rdolor &nsit &c&mamet";

$this->assertSame('<span style="color: #AA0000">Lorem </span><span style="color: #00AAAA"><span style="font-weight: bold;">ipsum'."\n".'</span></span>dolor <span style="text-decoration: underline;">sit </span><span style="color: #FF5555"><span style="text-decoration: line-through;">amet</span></span>', MinecraftColors::convertToHTML($text));
$this->assertSame('<span style="color: #AA0000">Lorem </span><span style="color: #00AAAA"><span style="font-weight: bold;">ipsum<br /></span></span>dolor <span style="text-decoration: underline;">sit </span><span style="color: #FF5555"><span style="text-decoration: line-through;">amet</span></span>', MinecraftColors::convertToHTML($text, true));
$this->assertSame('<span class="minecraft-formatted--dark-red">Lorem </span><span class="minecraft-formatted--dark-aqua"><span class="minecraft-formatted--bold">ipsum<br /></span></span>dolor <span class="minecraft-formatted--underline">sit </span><span class="minecraft-formatted--red"><span class="minecraft-formatted--line-strikethrough">amet</span></span>', MinecraftColors::convertToHTML($text, true, true));
$this->assertSame('<span class="mc-motd--dark-red">Lorem </span><span class="mc-motd--dark-aqua"><span class="mc-motd--bold">ipsum<br /></span></span>dolor <span class="mc-motd--underline">sit </span><span class="mc-motd--red"><span class="mc-motd--line-strikethrough">amet</span></span>', MinecraftColors::convertToHTML($text, true, true, 'mc-motd--'));
$this->assertSame('<span style="color: #AA0000">Lorem </span><span style="color: #00AAAA"><span style="font-weight: bold;">ipsum</span></span>'."\n".'dolor <span style="text-decoration: underline;">sit </span><span style="color: #FF5555"><span style="text-decoration: line-through;">amet</span></span>', MinecraftColors::convertToHTML($text));
$this->assertSame('<span style="color: #AA0000">Lorem </span><span style="color: #00AAAA"><span style="font-weight: bold;">ipsum</span></span><br />dolor <span style="text-decoration: underline;">sit </span><span style="color: #FF5555"><span style="text-decoration: line-through;">amet</span></span>', MinecraftColors::convertToHTML($text, true));
$this->assertSame('<span class="minecraft-formatted--dark-red">Lorem </span><span class="minecraft-formatted--dark-aqua"><span class="minecraft-formatted--bold">ipsum</span></span><br />dolor <span class="minecraft-formatted--underline">sit </span><span class="minecraft-formatted--red"><span class="minecraft-formatted--line-strikethrough">amet</span></span>', MinecraftColors::convertToHTML($text, true, true));
$this->assertSame('<span class="mc-motd--dark-red">Lorem </span><span class="mc-motd--dark-aqua"><span class="mc-motd--bold">ipsum</span></span><br />dolor <span class="mc-motd--underline">sit </span><span class="mc-motd--red"><span class="mc-motd--line-strikethrough">amet</span></span>', MinecraftColors::convertToHTML($text, true, true, 'mc-motd--'));
}

public function testConvertToMOTD(): void {
Expand Down

0 comments on commit e9561ec

Please sign in to comment.