}
}
}
+
+ // trim <p>...</p>
+ /** @var \DOMElement $paragraph */
+ foreach ($this->getDocument()->getElementsByTagName('p') as $paragraph) {
+ DOMUtil::normalize($paragraph);
+
+ if ($paragraph->firstChild && $paragraph->firstChild->nodeType === XML_TEXT_NODE) {
+ $oldNode = $paragraph->firstChild;
+ $newNode = $paragraph->ownerDocument->createTextNode(preg_replace('/^(\s|'.chr(226).chr(128).chr(175).'|'.chr(194).chr(160).')+/', '', $oldNode->textContent));
+ $paragraph->insertBefore($newNode, $oldNode);
+ $paragraph->removeChild($oldNode);
+
+ }
+
+ if ($paragraph->lastChild && $paragraph->lastChild->nodeType === XML_TEXT_NODE) {
+ $oldNode = $paragraph->lastChild;
+ $newNode = $paragraph->ownerDocument->createTextNode(preg_replace('/(\s|'.chr(226).chr(128).chr(175).'|'.chr(194).chr(160).')+$/', '', $oldNode->textContent));
+ $paragraph->insertBefore($newNode, $oldNode);
+ $paragraph->removeChild($oldNode);
+
+ }
+ }
}
/**
while ($element !== $commonAncestor);
}
+ /**
+ * Normalizes an element by joining adjacent text nodes.
+ *
+ * @param \DOMElement $element target element
+ */
+ public static function normalize(\DOMElement $element) {
+ $childNodes = DOMUtil::getChildNodes($element);
+ /** @var \DOMNode $lastTextNode */
+ $lastTextNode = null;
+ foreach ($childNodes as $childNode) {
+ if ($childNode->nodeType !== XML_TEXT_NODE) {
+ $lastTextNode = null;
+ continue;
+ }
+
+ if ($lastTextNode === null) {
+ $lastTextNode = $childNode;
+ }
+ else {
+ // merge with last text node
+ $newTextNode = $childNode->ownerDocument->createTextNode($lastTextNode->textContent . $childNode->textContent);
+ $element->insertBefore($newTextNode, $lastTextNode);
+
+ $element->removeChild($lastTextNode);
+ $element->removeChild($childNode);
+
+ $lastTextNode = $newTextNode;
+ }
+ }
+ }
+
/**
* Prepends a node to provided element.
*