Skip to content

Commit ef26ca4

Browse files
Merge pull request #8368 from SimpleMachines/3.0/autolinker_fixes
3.0/autolinker fixes
2 parents 32f3186 + 91b8291 commit ef26ca4

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Sources/Autolinker.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,11 @@ public function detectUrls(string $string, bool $plaintext_only = false): array
384384

385385
// An entity right after the URL can break the autolinker.
386386
$this->setEntitiesRegex();
387-
$string = preg_replace('~(' . $this->entities_regex . ')*(?=\s|$)~u', ' ', $string);
387+
$string = preg_replace_callback(
388+
'~(' . $this->entities_regex . ')*(?=\s|$)~u',
389+
fn ($matches) => str_repeat(' ', strlen($matches[0])),
390+
$string,
391+
);
388392

389393
$this->setUrlRegex();
390394

@@ -412,7 +416,7 @@ public function detectUrls(string $string, bool $plaintext_only = false): array
412416

413417
// Overwrite all BBC markup elements.
414418
$string = preg_replace_callback(
415-
'/\[[^\]]*\]/i' . ($this->encoding === 'UTF-8' ? 'u' : ''),
419+
'~\[/?' . Parser::getBBCodeTagsRegex() . '[^\]]*\]~i' . ($this->encoding === 'UTF-8' ? 'u' : ''),
416420
fn ($matches) => str_repeat(' ', strlen($matches[0])),
417421
$string,
418422
);
@@ -786,7 +790,7 @@ public static function createJavaScriptFile(bool $force = false): void
786790
$regexes['email'] = self::load()->getJavaScriptEmailRegex();
787791

788792
// Don't autolink if the URL is inside a Markdown link construct.
789-
$md_lookbehind = !empty(Config::$modSettings['enableMarkdown']) ? '(?<!\[[^\]]*\](?:\(|:[ \t\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]*\n?[ \t\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]*))' : '';
793+
$md_lookbehind = !empty(Config::$modSettings['enableMarkdown']) ? '(?<!\[[^\]]*\](?:\(|:[ \t\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]*\n?[ \t\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000]*)|\[i?url(?:=[^\]]*)?\])' : '(?<!\[i?url(?:=[^\]]*)?\])';
790794

791795
foreach ($regexes as $key => $value) {
792796
$js[] = 'autolinker_regexes.set(' . Utils::escapeJavaScript($key) . ', new RegExp(' . Utils::escapeJavaScript($md_lookbehind . $value) . ', "giu"));';

Sources/Parser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,16 @@ public static function getSigTags(): array
354354
return BBcodeParser::getSigTags();
355355
}
356356

357+
/**
358+
* Gets a regular expression to match all known BBC tags.
359+
*
360+
* @return string Regular expression to match all BBCode tags.
361+
*/
362+
public static function getBBCodeTagsRegex(): string
363+
{
364+
return BBcodeParser::load()->getAllTagsRegex();
365+
}
366+
357367
/**
358368
* Highlight any code.
359369
*

Themes/default/scripts/jquery.sceditor.smf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@
544544
url[0] = '//' + url[0];
545545
}
546546

547-
// Wrap the URL in BBC tags.
548-
editor.insert('[' + bbc_tag + '="' + url[0] + '"]', '[/' + bbc_tag + ']');
547+
// Insert the URL.
548+
editor.wysiwygEditorInsertHtml('<a data-type="' + bbc_tag + '" href="' + url[0] + '">' + url[0] + '</a>');
549549
}
550550

551551
// Helper for this.signalKeydownEvent.

0 commit comments

Comments
 (0)