improved nl2br to make it more intelligent

tag clean recognizes quotes in tags and escapes all non-tags
bugfixes in explode along tags
uses now simple <p>-tags normally and only in special cases margin=0
converts double newlines to paragraphs and single newlines to br-tags
but only inside text, not in tags and only where it is allowed by html standards
This commit is contained in:
Stephan Brunker 2018-07-01 22:23:15 +02:00 committed by onli
parent 280a2f1c00
commit e09b2600c6

View File

@ -277,11 +277,25 @@ class serendipity_event_nl2br extends serendipity_event
//Standardize line endings to Unix //Standardize line endings to Unix
$text = str_replace(array("\r\n", "\r"), "\n", $text); $text = str_replace(array("\r\n", "\r"), "\n", $text);
//move newlines from body to extended //framing newlines: pre and after everything
if ($element == 'body' && isset($eventData['extended'])) //without newline margin = 0 between body and extended
{ //to make splitting inside a paragraph possible
$eventData['extended'] = str_repeat("\n",strspn($text,"\n",-1)) . $eventData['extended']; //but with obligatory break because of the independent div-elements
$text = rtrim($text,"\n");
// rules for body <-> extended:
// no margins only if body ends with \n or no \n and extended starts without \n
// means: concatenate body and extended and there is no whiteline between them
if ($element == 'body' && isset($eventData['extended']) && !(strspn($text,"\n",-1) > 1) && strspn($eventData['extended'],"\n") )
{
$text = "\n" . $text;
}
elseif ($element == 'extended' && !strspn($text,"\n") && !(strspn($eventData['body'],"\n",-1) > 1))
{
$text = $text . "\n";
}
else
{
$text = "\n" . $text . "\n";
} }
$eventData[$element] = $this->nl2p($text); $eventData[$element] = $this->nl2p($text);
} }
@ -332,22 +346,15 @@ class serendipity_event_nl2br extends serendipity_event
/* nl2br plugin start */ /* nl2br plugin start */
p.wl_bottom { p.wl_nobottom {
margin-top: 0em;
margin-bottom: 1em;
}
p.wl_top {
margin-top: 1em;
margin-bottom: 0em; margin-bottom: 0em;
} }
p.wl_top_bottom { p.wl_notop {
margin-top: 1em; margin-top: 0em;
margin-bottom: 1em;
} }
p.break { p.wl_notopbottom {
margin-top: 0em; margin-top: 0em;
margin-bottom: 0em; margin-bottom: 0em;
} }
@ -413,7 +420,7 @@ p.break {
var $isolation_block_elements = array('pre','textarea'); var $isolation_block_elements = array('pre','textarea');
var $isolation_inline_elements = array('svg'); var $isolation_inline_elements = array('svg','style');
var $ignored_elements = array('area', 'br', 'col', 'command', 'embed', var $ignored_elements = array('area', 'br', 'col', 'command', 'embed',
'img', 'input', 'keygen', 'link', 'param', 'source', 'img', 'input', 'keygen', 'link', 'param', 'source',
@ -436,18 +443,17 @@ p.break {
'details', 'dl', 'dt', 'footer', 'header', 'summary' 'details', 'dl', 'dt', 'footer', 'header', 'summary'
); );
const P_TOP = '<p class="wl_top">';
const P_BOTTOM = '<p class="wl_bottom">';
const P_TOP_BOTTOM = '<p class="wl_top_bottom">';
const P_BREAK = '<p class="break">';
const P_END = '</p>'; const P_END = '</p>';
const P = '<p>';
const P_NOTOP = '<p class="wl_notop">';
const P_NOBOTTOM = '<p class="wl_nobottom">';
const P_NOTOPBOTTOM = '<p class="wl_notopbottom">';
function nl2p($text) function nl2p($text)
{ {
//homogenize tags //homogenize tags
$text = $this->tag_clean($text); $text = $this->tag_clean($text);
//delete isolation tags from other arrays //delete isolation tags from other arrays
if ($this->isolationtags) if ($this->isolationtags)
{ {
@ -467,6 +473,12 @@ p.break {
/** Make sure that all the tags are in lowercase /** Make sure that all the tags are in lowercase
* purge all \n from inside tags * purge all \n from inside tags
* remove spaces in endtags * remove spaces in endtags
* replace < > with &lt; $gt; for non-tags
* tags are split in three parts:
* tagstart - '<' character
* tagdef - type of tag like 'img'
* style - following content after a space
* isolation by quotes in the style part
* @param string text * @param string text
* @return text * @return text
*/ */
@ -478,25 +490,42 @@ p.break {
$tagdef = false; $tagdef = false;
$endtag = false; $endtag = false;
$tagstyle = false; $tagstyle = false;
$singlequote = false;
$doublequote = false;
for ($i = 0; $i < count($text); $i++) for ($i = 0; $i < count($text); $i++)
{ {
// start tag without closing tag
if ($text[$i] == '<' && !strpos($textstring,'>',$i+1) ) if ($text[$i] == '<' && !strpos($textstring,'>',$i+1) )
{ $text[$i] = '&lt;'; } { $text[$i] = '&lt;'; }
// end tag without previous start, definition or style section
elseif ($text[$i] == '>' && !($tagstart !== false || $tagdef || $tagstyle) ) elseif ($text[$i] == '>' && !($tagstart !== false || $tagdef || $tagstyle) )
{ $text[$i] = '&gt;'; } { $text[$i] = '&gt;'; }
elseif ($text[$i] == '<') // start tag inside quotes
elseif ( $text[$i] == '<' && ($singlequote || $doublequote) )
{ $text[$i] = '&lt;'; }
// end tag inside quotes
elseif ( $text[$i] == '>' && ($singlequote || $doublequote) )
{ $text[$i] = '&gt;'; }
// start tag inside tag
elseif ($text[$i] == '<' && $tagstart !== false )
{ $text[$i] = '&lt;'; }
// real start tag
elseif ($text[$i] == '<' )
{ $tagstart = $i; } { $tagstart = $i; }
// space after the start - not allowed in html
elseif ($text[$i] == ' ' && $tagstart !== false ) elseif ($text[$i] == ' ' && $tagstart !== false )
{ {
$text[$tagstart] = '&lt;'; $text[$tagstart] = '&lt;';
$tagstart = false; $tagstart = false;
} }
// < > without content
elseif ($text[$i] == '>' && $tagstart !== false ) elseif ($text[$i] == '>' && $tagstart !== false )
{ {
$text[$tagstart] = '&lt;'; $text[$tagstart] = '&lt;';
$text[$i] = '&gt;'; $text[$i] = '&gt;';
} }
// first space or closing tag in definition part
elseif ( ($text[$i] == ' ' || $text[$i] == '>') && $tagdef) elseif ( ($text[$i] == ' ' || $text[$i] == '>') && $tagdef)
{ {
//check if it is a real tag //check if it is a real tag
@ -511,51 +540,78 @@ p.break {
|| in_array($tag,$this->nested_block_elements) || in_array($tag,$this->nested_block_elements)
|| in_array($tag,$this->ignored_elements) )) || in_array($tag,$this->ignored_elements) ))
{ {
// unknown tag definition
$text[$tagstart_b] = '&lt;'; $text[$tagstart_b] = '&lt;';
$text[strpos($textstring,'>',$i)] = '&gt;'; $text[strpos($textstring,'>',$i)] = '&gt;';
} else } else
{ {
$tagstyle = true;
$tagdef = false;
}
if ($text[$i] == '>')
{
$tagstart = false;
$tagdef = false; $tagdef = false;
$tagstyle = false; // closing >
$endtag = false; if ($text[$i] == '>')
{
$tagstart = false;
$tagstyle = false;
$endtag = false;
}
// start of style part
else
{
$tagstyle = true;
}
} }
} }
// endtag starting with </
elseif ($text[$i] == '/' && $tagstart !== false) elseif ($text[$i] == '/' && $tagstart !== false)
{ $endtag = true; } { $endtag = true; }
// space is allowed in endtag like </ i>
elseif ($text[$i] == ' ' && $endtag) elseif ($text[$i] == ' ' && $endtag)
{ $text[$i] = ''; } { $text[$i] = ''; }
// remove newline in tags
elseif (($tagstart !== false || $tagdef || $tagstyle) && $text[$i] == "\n") elseif (($tagstart !== false || $tagdef || $tagstyle) && $text[$i] == "\n")
{ $text[$i] = ''; } { $text[$i] = ''; }
elseif ($text[$i] == '>' && ($tagdef || $tagstyle) ) // closing > after style part
elseif ($text[$i] == '>' && $tagstyle && !($singlequote || $doublequote) )
{ {
$tagstart = false; $tagstart = false;
$tagdef = false; $tagdef = false;
$tagstyle = false; $tagstyle = false;
$endtag = false; $endtag = false;
} }
elseif ($tagstart !== false) // first definition character after <
elseif ($tagstart !== false && !($tagdef || $tagstyle) )
{ {
$tagdef = $i; $tagdef = $i;
$tagstart_b = $tagstart; $tagstart_b = $tagstart;
$tagstart = false; $tagstart = false;
$text[$i] = strtolower($text[$i]); $text[$i] = strtolower($text[$i]);
} }
// definition characters
elseif ($tagdef) elseif ($tagdef)
{ $text[$i] = strtolower($text[$i]); } { $text[$i] = strtolower($text[$i]); }
// quotes in style - isolate
elseif ($tagstyle && $text[$i] == '\'' && !$doublequote )
{
if ($singlequote)
{ $singlequote = false; }
else
{ $singlequote = true; }
}
elseif ($tagstyle && $text[$i] == '"' && !$singlequote )
{
if ($doublequote)
{ $doublequote = false; }
else
{ $doublequote = true; }
}
} }
return implode($text); return implode($text);
} }
/* /*
* sophisticated nl to p - blocktag stage * sophisticated nl to p - blocktag stage
* handles content with blocktags, apply nl2p to the block elements if tag allows it * handles content with blocktags, apply nl2p to the block elements if tag allows it
* works also for ommitted closing tags and singleton tags * works also for ommitted closing tags
* @param: text * @param: text
* return string * return string
*/ */
@ -581,7 +637,7 @@ p.break {
//merge previous content, apply nl2p if needed and concatenate //merge previous content, apply nl2p if needed and concatenate
if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) )) if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) ))
{ {
$content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))); $content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))) . "\n";
} else } else
{ {
$content .= implode(array_slice($textarray,$start,$i-$start)); $content .= implode(array_slice($textarray,$start,$i-$start));
@ -606,7 +662,7 @@ p.break {
//merge previous content, apply nl2p if needed and concatenate //merge previous content, apply nl2p if needed and concatenate
if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) )) if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) ))
{ {
$content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))); $content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))) . "\n";
} else } else
{ {
$content .= implode(array_slice($textarray,$start,$i-$start)); $content .= implode(array_slice($textarray,$start,$i-$start));
@ -622,11 +678,11 @@ p.break {
//merge previous content, apply nl2p if needed and concatenate //merge previous content, apply nl2p if needed and concatenate
if (empty($tagstack) ) if (empty($tagstack) )
{ {
$content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))); $content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))) . "\n";
} elseif (in_array($tagstack[0], $this->allowed_p_parents) ) } elseif (in_array($tagstack[0], $this->allowed_p_parents) )
{ {
$content .= $textarray[$start] $content .= $textarray[$start]
. $this->nl2pblock(implode(array_slice($textarray,$start+1,$i-$start-1))); . $this->nl2pblock(implode(array_slice($textarray,$start+1,$i-$start-1))) . "\n";
} else } else
{ {
$content .= implode(array_slice($textarray,$start,$i-$start)); $content .= implode(array_slice($textarray,$start,$i-$start));
@ -650,7 +706,7 @@ p.break {
{ {
if (!$isolation_flag && in_array($tagstack[0], $this->allowed_p_parents) ) if (!$isolation_flag && in_array($tagstack[0], $this->allowed_p_parents) )
{ {
$content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))); $content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))) . "\n";
} else } else
{ {
$content .= implode(array_slice($textarray,$start,$i-$start)); $content .= implode(array_slice($textarray,$start,$i-$start));
@ -667,7 +723,7 @@ p.break {
//merge remainder //merge remainder
if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) )) if (!$isolation_flag && ( empty($tagstack) || in_array($tagstack[0], $this->allowed_p_parents) ))
{ {
$content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))); $content .= $this->nl2pblock(implode(array_slice($textarray,$start,$i-$start))) . "\n";
} else } else
{ {
$content .= implode(array_slice($textarray,$start,$i-$start)); $content .= implode(array_slice($textarray,$start,$i-$start));
@ -678,15 +734,17 @@ p.break {
/* /*
* sophisticated nl to p for content which is already * sophisticated nl to p for content which is already
* purged from block elements by blocktag_nl2p * purged from block elements by blocktag_nl2p
* explode content along \n * explode content along tags
* check for following \n * build stack of active tags
* explode along (inline) tags, get active tags across newlines * isolate content inside isolation tags or
* explode along newlines
* single breaks converted to <br>
* build every paragraph: p class | reopen active tags | content ... | new open tags | closing p tag * build every paragraph: p class | reopen active tags | content ... | new open tags | closing p tag
* for content which is not isolated by inline isolation tags like svg * class depends on framing newlines:
* Insert P_BOTTOM class at paragraphs ending with two newlines * Insert P_NOBOTTOM class at last paragraph if no newline is following
* Insert P_BREAK class at paragraphs ending with one newline * Insert P_NOTOP class at the first paragraph if is not starting with a nl
* Insert P_TOP class at the first paragraph if starting with a nl * Insert P_NOTOPBOTTOM class if it is just one paragraph
* Insert P_TOP_BOTTOM class if the first paragraph is ending with two newlines * normal P for everything else (default CSS has margin top,bottom)
* @param string text * @param string text
* @return string * @return string
*/ */
@ -699,108 +757,114 @@ p.break {
//check for start/end newlines //check for start/end newlines
$startnl = ( strspn($textstring,"\n") ) ? true : false; $startnl = ( strspn($textstring,"\n") ) ? true : false;
$endnl = ( strspn($textstring,"\n",-1 ) ) ? true : false; $endnl = ( strspn($textstring,"\n",-1 ) ) ? true : false;
$whiteline = false; $firstp = true;
$textstring = trim($textstring,"\n"); $textstring = trim($textstring,"\n");
if (empty($textstring)) { return ''; } if (empty($textstring)) { return ''; }
//explode in paragraphs
$textline = explode("\n",$textstring);
$tagstack = array();
$tagstack_prev = array();
$tagexplode = array(); $tagexplode = array();
//explode in tags and content
$tagexplode = $this->explode_along_tags($textstring);
$tagstack = array();
$textline = array();
$buffer = '';
$bufferhastext = false;
$content = ''; $content = '';
$tag = false;
$isolation_tag = false; $isolation_tag = false;
for($i=0; $i<count($textline);$i++) //first stage: explode in tags
for($i=0; $i<count($tagexplode);$i++)
{ {
//get tag or false if none
//explode in tags and content $tag = $this->extract_tag($tagexplode[$i]);
$tagexplode = $this->explode_along_tags($textline[$i]); // start isolation
//save active tags if ($tag && $this->is_starttag($tagexplode[$i]) && in_array($tag,$this->isolation_inline_elements))
$tagstack_prev = $tagstack;
//iterate through the tags in the paragraph
for ($j=0; $j<count($tagexplode); $j++)
{ {
//get tag or false if none $isolation_tag = $tag;
$tag = $this->extract_tag($tagexplode[$j]); }
// end isolation
elseif ($tag && !$this->is_starttag($tagexplode[$i]) && $tag == $isolation_tag)
{
$isolation_tag = false;
}
// put inlinetag to stack
elseif ($tag && !$isolation_tag && $this->is_starttag($tagexplode[$i]) && in_array($tag,$this->inline_elements) )
{
array_unshift($tagstack, $tagexplode[$i]);
}
// remove inlinetag from stack
elseif ($tag && !$isolation_tag && !$this->is_starttag($tagexplode[$i]) && !empty($tagstack) && $tag == $this->extract_tag($tagstack[0]))
{
array_shift($tagstack);
}
// put or remove tag from stack // put isolated content into buffer
if ($tag && $this->is_starttag($tagexplode[$j]) && in_array($tag,$this->isolation_inline_elements)) if ($isolation_tag || $tag)
{
$buffer .= $tagexplode[$i];
}
// explode content in textlines
else
{
$textline = explode("\n",$tagexplode[$i]);
//iterate through the paragraphs and build content
for ($j=0; $j<count($textline); $j++)
{ {
$isolation_tag = $tag;
} // whiteline \n\n found: make paragraph with buffer and this line
elseif ($tag && !$this->is_starttag($tagexplode[$j]) && $tag == $isolation_tag) if ( ($j < count($textline) - 1 && empty($textline[$j+1]) ) )
{ {
$isolation_tag = false; // p start tag, append buffer and empty buffer
} if ($firstp && !$startnl) { $content .= self::P_NOTOP . $buffer; }
elseif ($tag && !$isolation_tag && $this->is_starttag($tagexplode[$j]) && in_array($tag,$this->inline_elements) ) else { $content .= self::P . $buffer; }
{ $firstp = false;
array_unshift($tagstack, $tagexplode[$j]); $buffer = '';
} $bufferhastext = false;
elseif($tag && !$isolation_tag && !$this->is_starttag($tagexplode[$j]) && !empty($tagstack) && $tag == $this->extract_tag($tagstack[0]))
{ //append textline
array_shift($tagstack); $content .= $textline[$j];
//close open tags
foreach($tagstack as $ins_tag) { $content .= $this->html_end_tag($this->extract_tag($ins_tag)); }
//paragraph closing tag
$content .= self::P_END . "\n";
//put closed tags into buffer
foreach($tagstack as $ins_tag) { $buffer .= $ins_tag; }
//skip newline
$j += 1;
}
elseif ($j < count($textline) - 1) // single break
{
$buffer .= $textline[$j] . "<br>\n";
$bufferhastext = true;
}
else // last line
{
// append textline
$buffer .= $textline[$j];
$bufferhastext = true;
}
} }
} }
}
//concatenate if lines are isolated // handle last paragraph
if ($isolation_tag && $i < count($textline)-1) if (!$bufferhastext) { $content .= $buffer; }
{ else
$textline[$i+1] = $textline[$i] . "\n" . $textline[$i+1]; {
continue; if ($firstp && !$startnl && !$endnl ) { $content .= self::P_NOTOPBOTTOM; }
} elseif ($firstp && !$startnl) { $content .= self::P_NOTOP; }
elseif ($isolation_tag && $i == count($textline)-1) elseif (!$endnl) { $content .= self::P_NOBOTTOM; }
{ else { $content .= self::P; }
$textline[$i] .= $this->html_end_tag($this->extract_tag($isolation_tag)); $content .= $buffer;
} foreach($tagstack as $ins_tag) { $content .= $this->html_end_tag($this->extract_tag($ins_tag)); }
$content .= self::P_END;
//check for whiteline
if ($i < count($textline) - 1 && empty($textline[$i+1]) )
{
$whiteline = true;
}
elseif (empty($textline[$i]))
{
continue;
}
//build content
//paragraph class
if ($i == 0 && $startnl && ( $whiteline || ($i == count($textline)-1 && $endnl) ) )
{
$content .= self::P_TOP_BOTTOM;
}
elseif ($i == 0 && $startnl)
{
$content .= self::P_TOP;
}
elseif ($whiteline || ($i == count($textline)-1 && $endnl))
{
$content .= self::P_BOTTOM;
} else
{
$content .= self::P_BREAK;
}
//reopen active tags
foreach($tagstack_prev as $ins_tag)
{
$content .= $ins_tag;
}
//content paragraph
$content .= $textline[$i];
//close open tags
foreach($tagstack as $ins_tag)
{
$content .= $this->html_end_tag($this->extract_tag($ins_tag));
}
//paragraph closing tag
$content .= self::P_END . "\n";
$whiteline = false;
} }
return $content; return $content;
@ -859,7 +923,6 @@ p.break {
function html_start_tag($text) { return '<' . $text . '>'; } function html_start_tag($text) { return '<' . $text . '>'; }
function is_starttag($text) { return ($text[1] == "/") ? false : true; } function is_starttag($text) { return ($text[1] == "/") ? false : true; }
} }
/* vim: set sts=4 ts=4 expandtab : */ /* vim: set sts=4 ts=4 expandtab : */