1
1
Fork 0

Merge pull request #6556 from MrPetovan/bug/6554-fix-hashtag-in-code

Bug/fix hashtag in code
This commit is contained in:
Michael Vogel 2019-01-30 03:49:47 +01:00 committed by GitHub
commit 3a27d66b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 16 deletions

View file

@ -72,6 +72,21 @@ function babel_content()
'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'), 'title' => L10n::t('BBCode::toMarkdown => Markdown::convert => HTML::toBBCode'),
'content' => visible_whitespace($bbcode4) 'content' => visible_whitespace($bbcode4)
]; ];
$item = [
'body' => $bbcode,
'tag' => '',
];
\Friendica\Model\Item::setHashtags($item);
$results[] = [
'title' => L10n::t('Item Body'),
'content' => visible_whitespace($item['body'])
];
$results[] = [
'title' => L10n::t('Item Tags'),
'content' => $item['tag']
];
break; break;
case 'markdown': case 'markdown':
$markdown = trim($_REQUEST['text']); $markdown = trim($_REQUEST['text']);

View file

@ -1743,7 +1743,7 @@ class BBCode extends BaseObject
// Clean up the HTML by loading and saving the HTML with the DOM. // Clean up the HTML by loading and saving the HTML with the DOM.
// Bad structured html can break a whole page. // Bad structured html can break a whole page.
// For performance reasons do it only with ativated item cache or at export. // For performance reasons do it only with activated item cache or at export.
if (!$try_oembed || (get_itemcachepath() != "")) { if (!$try_oembed || (get_itemcachepath() != "")) {
$doc = new DOMDocument(); $doc = new DOMDocument();
$doc->preserveWhiteSpace = false; $doc->preserveWhiteSpace = false;
@ -1959,7 +1959,7 @@ class BBCode extends BaseObject
$string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string); $string = preg_replace('/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism', '#$2', $string);
// ignore anything in a code block // ignore anything in a code block
$string = preg_replace('/\[code\](.*?)\[\/code\]/sm', '', $string); $string = preg_replace('/\[code.*?\].*?\[\/code\]/sm', '', $string);
// Force line feeds at bbtags // Force line feeds at bbtags
$string = str_replace(['[', ']'], ["\n[", "]\n"], $string); $string = str_replace(['[', ']'], ["\n[", "]\n"], $string);

View file

@ -2386,18 +2386,6 @@ class Item extends BaseObject
public static function setHashtags(&$item) public static function setHashtags(&$item)
{ {
// What happens in [code], stays in [code]!
// escape the # and the [
// hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3
$item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism",
function ($match) {
// we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks
$find = ['#', '['];
$replace = [chr(27).'sharp', chr(27).'leftsquarebracket'];
return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]");
}, $item["body"]);
$tags = BBCode::getTags($item["body"]); $tags = BBCode::getTags($item["body"]);
// No hashtags? // No hashtags?
@ -2405,6 +2393,17 @@ class Item extends BaseObject
return false; return false;
} }
// What happens in [code], stays in [code]!
// escape the # and the [
// hint: we will also get in trouble with #tags, when we want markdown in posts -> ### Headline 3
$item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism",
function ($match) {
// we truly ESCape all # and [ to prevent gettin weird tags in [code] blocks
$find = ['#', '['];
$replace = [chr(27).'sharp', chr(27).'leftsquarebracket'];
return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]");
}, $item["body"]);
// This sorting is important when there are hashtags that are part of other hashtags // This sorting is important when there are hashtags that are part of other hashtags
// Otherwise there could be problems with hashtags like #test and #test2 // Otherwise there could be problems with hashtags like #test and #test2
rsort($tags); rsort($tags);
@ -2463,12 +2462,12 @@ class Item extends BaseObject
// Remember! What happens in [code], stays in [code] // Remember! What happens in [code], stays in [code]
// roleback the # and [ // roleback the # and [
$item["body"] = preg_replace_callback("/\[code(.*)\](.*?)\[\/code\]/ism", $item["body"] = preg_replace_callback("/\[code(.*?)\](.*?)\[\/code\]/ism",
function ($match) { function ($match) {
// we truly unESCape all sharp and leftsquarebracket // we truly unESCape all sharp and leftsquarebracket
$find = [chr(27).'sharp', chr(27).'leftsquarebracket']; $find = [chr(27).'sharp', chr(27).'leftsquarebracket'];
$replace = ['#', '[']; $replace = ['#', '['];
return ("[code" . str_replace($find, $replace, $match[1]) . "]" . $match[2] . "[/code]"); return ("[code" . $match[1] . "]" . str_replace($find, $replace, $match[2]) . "[/code]");
}, $item["body"]); }, $item["body"]);
} }