X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fhtml2bbcode.php;h=435f6b77c1198aee5db6d753010581fbc86a65a0;hb=28eb5d57a72aae22428061b26b90195f7feacadf;hp=10a2fd13e5e24eb6be995d5fb7fbc92965e234da;hpb=fc18123082f2e0606066ae18a385db5bffc19142;p=friendica.git diff --git a/include/html2bbcode.php b/include/html2bbcode.php index 10a2fd13e5..435f6b77c1 100644 --- a/include/html2bbcode.php +++ b/include/html2bbcode.php @@ -1,11 +1,14 @@ 0) { - if ($replace and (@$attr[$attribute] != '')) { + if ($replace && (@$attr[$attribute] != '')) { $startbb = preg_replace($value, $startbb, $attr[$attribute], -1, $count); @@ -76,23 +79,25 @@ function node2bbcodesub(&$doc, $oldnode, $attributes, $startbb, $endbb) return($replace); } -if(!function_exists('deletenode')) { -function deletenode(&$doc, $node) -{ - $xpath = new DomXPath($doc); - $list = $xpath->query("//".$node); - foreach ($list as $child) - $child->parentNode->removeChild($child); -}} - function html2bbcode($message) { $message = str_replace("\r", "", $message); - $message = preg_replace_callback("|
([^<]*)
|ism", function($m) {
- return "".str_replace("\n","
\n",$m[1]). "
";
- }, $message);
+ // Removing code blocks before the whitespace removal processing below
+ $codeblocks = [];
+ $message = preg_replace_callback('#(.*)
#iUs',
+ function ($matches) use (&$codeblocks) {
+ $return = '[codeblock-' . count($codeblocks) . ']';
+
+ $prefix = '[code]';
+ if ($matches[1] != '') {
+ $prefix = '[code=' . $matches[1] . ']';
+ }
+ $codeblocks[] = $prefix . $matches[2] . '[/code]';
+ return $return;
+ }
+ , $message);
$message = str_replace(array(
"", @@ -115,12 +120,12 @@ function html2bbcode($message) @$doc->loadHTML($message); - deletenode($doc, 'style'); - deletenode($doc, 'head'); - deletenode($doc, 'title'); - deletenode($doc, 'meta'); - deletenode($doc, 'xml'); - deletenode($doc, 'removeme'); + xml::deleteNode($doc, 'style'); + xml::deleteNode($doc, 'head'); + xml::deleteNode($doc, 'title'); + xml::deleteNode($doc, 'meta'); + xml::deleteNode($doc, 'xml'); + xml::deleteNode($doc, 'removeme'); $xpath = new DomXPath($doc); $list = $xpath->query("//pre"); @@ -236,8 +241,7 @@ function html2bbcode($message) node2bbcode($doc, 'audio', array('src'=>'/(.+)/'), '[audio]$1', '[/audio]'); node2bbcode($doc, 'iframe', array('src'=>'/(.+)/'), '[iframe]$1', '[/iframe]'); - node2bbcode($doc, 'code', array(), '[code]', '[/code]'); - node2bbcode($doc, 'key', array(), '[code]', '[/code]'); + node2bbcode($doc, 'key', array(), '[code]', '[/code]'); $message = $doc->saveHTML(); @@ -306,6 +310,18 @@ function html2bbcode($message) // Handling Yahoo style of mails $message = str_replace('[hr][b]From:[/b]', '[quote][b]From:[/b]', $message); - return(trim($message)); + // Restore code blocks + $message = preg_replace_callback('#\[codeblock-([0-9]+)\]#iU', + function ($matches) use ($codeblocks) { + $return = ''; + if (isset($codeblocks[intval($matches[1])])) { + $return = $codeblocks[$matches[1]]; + } + return $return; + } + , $message); + + $message = trim($message); + + return $message; } -?>