// dynamic node handlers
$this->invokeNodeHandlers('wcf\system\html\input\node\HtmlInputNode', ['img', 'woltlab-metacode']);
+ // remove whitespace at the start/end of the message
+ $this->trim();
+
// detect mentions, urls, emails and smileys
$textParser = new HtmlInputNodeTextParser($this);
$textParser->parse();
EventHandler::getInstance()->fireAction($this, 'afterProcess');
}
+ /**
+ * Trims leading and trailing whitespace. It will only remove text nodes containing
+ * just whitespaces and <p><br></p> (including any whitespace-only text nodes).
+ *
+ * It is still possible to work around this by inserting useless text formats such
+ * as bold to circumvent this check. The point of this method is to remove unintentional
+ * and/or potentially unwanted whitespace, not guarding against people being jerks.
+ */
+ protected function trim() {
+ $body = $this->getDocument()->getElementsByTagName('body')->item(0);
+
+ foreach (['firstChild', 'lastChild'] as $property) {
+ while ($node = $body->$property) {
+ if ($node->nodeType === XML_TEXT_NODE) {
+ if (StringUtil::trim($node->textContent) === '') {
+ $body->removeChild($node);
+ }
+ else {
+ break;
+ }
+ }
+ else {
+ /** @var \DOMElement $node */
+ if ($node->nodeName === 'p') {
+ for ($i = 0, $length = $node->childNodes->length; $i < $length; $i++) {
+ /** @var \DOMNode $child */
+ $child = $node->childNodes[$i];
+ if ($child->nodeType === XML_TEXT_NODE) {
+ if (StringUtil::trim($child->textContent) !== '') {
+ // terminate for() and while()
+ break 2;
+ }
+ }
+ else if ($child->nodeName !== 'br') {
+ // terminate for() and while()
+ break 2;
+ }
+ }
+
+ $body->removeChild($node);
+ }
+ else {
+ break;
+ }
+ }
+ }
+ }
+ }
+
/**
* Checks the input html for disallowed bbcodes and returns any matches.
*