Skip to content

Commit

Permalink
Better message content HTML handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveRandom committed May 22, 2016
1 parent 2ef9d2d commit d1a162f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/Chat/Event/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Room11\Jeeves\Chat\Event;

use function Room11\DOMUtils\domdocument_load_html;
use Room11\Jeeves\Chat\Event\Traits\RoomSource;
use Room11\Jeeves\Chat\Event\Traits\UserSource;
use Room11\Jeeves\Chat\Room\Room as ChatRoom;
Expand All @@ -17,7 +18,7 @@ abstract class MessageEvent extends BaseEvent implements UserSourcedEvent, RoomS
private $messageId;

/**
* @var string
* @var \DOMDocument
*/
private $messageContent;

Expand All @@ -31,15 +32,15 @@ protected function __construct(array $data, ChatRoom $room)
$this->userName = (string)$data['user_name'];

$this->messageId = (int)$data['message_id'];
$this->messageContent = (string)$data['content'] ?? '';
$this->messageContent = domdocument_load_html((string)($data['content'] ?? ''), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
}

public function getMessageId(): int
{
return $this->messageId;
}

public function getMessageContent(): string
public function getMessageContent(): \DOMDocument
{
return $this->messageContent;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Chat/Message/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ class Command extends Message

private $parameters;

private $text;

public function __construct(MessageEvent $event, ChatRoom $room)
{
parent::__construct($event, $room);

$commandParts = preg_split('#\s+#', trim($this->getText()), -1, PREG_SPLIT_NO_EMPTY);
$commandParts = preg_split('#\s+#', trim(parent::getText()), -1, PREG_SPLIT_NO_EMPTY);

$this->commandName = substr(array_shift($commandParts), 2);
$this->parameters = $commandParts;

var_dump($this->getText());
}

public function getCommandName(): string
Expand Down Expand Up @@ -52,4 +56,13 @@ public function hasParameters(int $minCount = -1): bool
{
return !empty($this->parameters) && ($minCount < 0 || count($this->parameters) >= $minCount);
}

public function getText(): string
{
if (!isset($this->text)) {
$this->text = ltrim(substr(parent::getText(), strlen($this->commandName) + 2));
}

return $this->text;
}
}
2 changes: 1 addition & 1 deletion src/Chat/Message/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Factory
{
public function build(MessageEvent $event): Message
{
if (strpos($event->getMessageContent(), '!!') === 0) {
if (strpos($event->getMessageContent()->textContent, '!!') === 0) {
return new Command($event, $event->getRoom());
}

Expand Down
16 changes: 15 additions & 1 deletion src/Chat/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Room11\Jeeves\Chat\Message;

use function Room11\DOMUtils\domdocument_load_html;
use Room11\Jeeves\Chat\Event\DeleteMessage;
use Room11\Jeeves\Chat\Event\EditMessage;
use Room11\Jeeves\Chat\Event\MentionMessage;
Expand All @@ -28,6 +29,8 @@ class Message

private $type;

private $text;

public function __construct(MessageEvent $event, ChatRoom $room)
{
$this->event = $event;
Expand All @@ -42,7 +45,18 @@ public function getEvent(): MessageEvent

public function getText(): string
{
return html_entity_decode($this->event->getMessageContent(), ENT_QUOTES);
if (!isset($this->text)) {
$doc = $this->event->getMessageContent();

/** @var \DOMElement $brEl */
foreach ($doc->getElementsByTagName('br') as $brEl) {
$brEl->parentNode->replaceChild(new \DOMText("\n"), $brEl);
}

$this->text = $doc->textContent;
}

return $this->text;
}

public function getId(): int
Expand Down
7 changes: 4 additions & 3 deletions src/Chat/Plugin/EvalCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ private function formatResult(string $result): string {
return $result;
}

public function eval(Command $command): Promise {
if (!$command->getParameters()) {
public function eval(Command $command): Promise
{
if (!$command->hasParameters()) {
return new Success();
}

$code = $this->normalizeCode(implode(' ', $command->getParameters()));
$code = $this->normalizeCode($command->getText());

$body = (new FormBody)
->addField("title", "")
Expand Down

0 comments on commit d1a162f

Please sign in to comment.