35 protected $_blocks =
'address article area aside blockquote caption col colgroup dd 36 details div dl dt fieldset figure figcaption footer form h1 h2 h3 h4 h5 h6 header 37 hr hgroup legend map math menu nav noscript p pre section select style summary 38 table tbody td tfoot th thead tr ul ol option li';
43 protected $_inlines =
'a abbr audio b button canvas caption cite code command datalist 44 del dfn em embed i iframe img input ins kbd keygen label map mark meter object 45 output progress q rp rt ruby s samp script select small source span strong style 46 sub sup textarea time var video wbr';
53 protected $_descendList =
'article aside blockquote body details div footer form 61 protected $_alterList =
'article aside blockquote body details div footer header 73 $this->_blocks = preg_split(
'@\\s+@', $this->_blocks);
74 $this->_descendList = preg_split(
'@\\s+@', $this->_descendList);
75 $this->_alterList = preg_split(
'@\\s+@', $this->_alterList);
76 $this->_inlines = preg_split(
'@\\s+@', $this->_inlines);
77 $this->_unique =
md5(__FILE__);
104 $this->_doc = new \DOMDocument();
108 $use_internal_errors = libxml_use_internal_errors(
true);
110 $load_result = $this->_doc->loadHTML(
"<html><meta http-equiv='content-type' content='text/html; charset={$this->encoding}'><body>{$html}</body></html>", LIBXML_NOBLANKS);
112 libxml_use_internal_errors($use_internal_errors);
117 $this->_xpath = new \DOMXPath($this->_doc);
120 $nodeList = $this->_xpath->query(
'//body[1]');
121 if ($nodeList->item(0) instanceof \DOMText) {
124 $this->_xpath = new \DOMXPath($this->_doc);
125 $nodeList = $this->_xpath->query(
'//body[1]');
127 if ($nodeList->item(0) instanceof \DOMText) {
129 throw new RuntimeException(
'DOMXPath::query for BODY element returned a text node');
136 $html = $this->_doc->saveHTML();
141 $html = preg_replace(
'/(' . $this->_unique .
'NL){2,}/',
'</autop><autop>', $html);
142 $html =
str_replace([$this->_unique .
'BR', $this->_unique .
'NL',
'<br>'],
145 $html =
str_replace(
'<br /></autop>',
'</autop>', $html);
151 $use_internal_errors = libxml_use_internal_errors(
true);
153 $load_result = $this->_doc->loadHTML($html);
155 libxml_use_internal_errors($use_internal_errors);
161 $this->_xpath = new \DOMXPath($this->_doc);
164 foreach ($this->_xpath->query(
'//autop') as $autop) {
167 if (
trim($autop->textContent) !==
'') {
170 foreach ($autop->childNodes as $node) {
171 if ($node->nodeType === XML_ELEMENT_NODE) {
180 $autop->setAttribute(
'r',
'1');
185 foreach ($this->_xpath->query(
'//div') as $el) {
187 $autops = $this->_xpath->query(
'./autop', $el);
188 if ($autops->length === 1) {
189 $firstAutop = $autops->item(0);
191 $firstAutop->setAttribute(
'r',
'1');
195 $html = $this->_doc->saveHTML();
199 $bodyEnd =
elgg_strpos($html,
'</body>', $bodyStart + 6);
200 $html =
elgg_substr($html, $bodyStart + 6, $bodyEnd - $bodyStart - 6);
203 $html = preg_replace(
'@<autop r="1">(.*?)</autop>@',
'\\1', $html);
210 return str_replace($this->_unique .
'AMP',
'&', $html);
222 $elsToProcess = [$el];
223 $inlinesToProcess = [];
224 while ($el = array_shift($elsToProcess)) {
227 $alterInline = in_array($el->nodeName, $this->_alterList);
231 $ltrimFirstTextNode =
true;
238 $isFollowingBr =
false;
240 $node = $el->firstChild;
241 while (isset($node)) {
246 $autop = $el->insertBefore($this->_doc->createElement(
'autop'), $node);
250 $isElement = ($node->nodeType === XML_ELEMENT_NODE);
252 $isBlock = in_array($node->nodeName, $this->_blocks);
255 $ltrimFirstTextNode =
false;
262 $isText = ($node->nodeType === XML_TEXT_NODE);
263 $isLastInline = (!$node->nextSibling
264 || ($node->nextSibling->nodeType === XML_ELEMENT_NODE
265 && in_array($node->nextSibling->nodeName, $this->_blocks)));
267 $isFollowingBr = ($node->nodeName ===
'br');
271 $nodeText = $node->nodeValue;
273 if ($ltrimFirstTextNode) {
275 $nodeText = ltrim($nodeText);
276 $ltrimFirstTextNode =
false;
280 if ($isFollowingBr &&
preg_match(
'@^[ \\t]*\\n[ \\t]*@', $nodeText, $matches)) {
287 $nodeText =
rtrim($nodeText);
290 $nodeText =
str_replace(
"\n", $this->_unique .
'NL', $nodeText);
292 $node = $node->nextSibling;
295 $tmpNode->nodeValue = $nodeText;
296 $autop->appendChild($tmpNode);
302 if ($isBlock || !$node->nextSibling) {
304 if (in_array($node->nodeName, $this->_descendList)) {
305 $elsToProcess[] = $node;
311 $ltrimFirstTextNode =
true;
317 if ($isElement &&
elgg_strpos($tmpNode->textContent,
"\n") !==
false) {
318 $inlinesToProcess[] = $tmpNode;
321 $node = $node->nextSibling;
322 $autop->appendChild($tmpNode);
327 $node = $node->nextSibling;
333 while ($el = array_shift($inlinesToProcess)) {
334 $ignoreLeadingNewline =
false;
335 foreach ($el->childNodes as $node) {
336 if ($node->nodeType === XML_ELEMENT_NODE) {
337 if ($node->nodeValue ===
'BR') {
338 $ignoreLeadingNewline =
true;
340 $ignoreLeadingNewline =
false;
341 if (
elgg_strpos($node->textContent,
"\n") !==
false) {
342 $inlinesToProcess[] = $node;
345 }
elseif ($node->nodeType === XML_TEXT_NODE) {
346 $text = $node->nodeValue;
347 if (
$text[0] ===
"\n" && $ignoreLeadingNewline) {
349 $ignoreLeadingNewline =
false;
__construct()
Constructor.
Exception thrown if an error which can only be found on runtime occurs.
Create wrapper P and BR elements in HTML depending on newlines.
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
elgg_strlen()
Wrapper function for mb_strlen().
elgg_strpos()
Wrapper function for mb_strpos().
addParagraphs(\DOMElement $el)
Add P and BR elements as necessary.
elgg_substr()
Wrapper function for mb_substr().
process($html)
Create wrapper P and BR elements in HTML depending on newlines.