From 088eb3391e303c514138d136865a0f6f77fb562b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 11 Oct 2019 15:27:05 -0400 Subject: [PATCH 1/2] Move urlRegex JS constant to main.js --- view/js/linkPreview.js | 1 - view/js/main.js | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/view/js/linkPreview.js b/view/js/linkPreview.js index c018563848..09493eebad 100644 --- a/view/js/linkPreview.js +++ b/view/js/linkPreview.js @@ -52,7 +52,6 @@
\
'; var text; - var urlRegex = /^(?:https?\:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(?:\/+[a-z0-9_.\:\;-]*)*(?:\?[\&\%\|\+a-z0-9_=,\.\:\;-]*)?(?:[\&\%\|\+&a-z0-9_=,\:\;\.-]*)(?:[\!\#\/\&\%\|\+a-z0-9_=,\:\;\.-]*)}*$/i; var binurl; var block = false; var blockTitle = false; diff --git a/view/js/main.js b/view/js/main.js index e2cee43d54..7e84a08d87 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -86,6 +86,8 @@ var last_popup_menu = null; var last_popup_button = null; var lockLoadContent = false; +const urlRegex = /^(?:https?:\/\/|\s)[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})(?:\/+[a-z0-9_.:;-]*)*(?:\?[&%|+a-z0-9_=,.:;-]*)?(?:[&%|+&a-z0-9_=,:;.-]*)(?:[!#\/&%|+a-z0-9_=,:;.-]*)}*$/i; + $(function() { $.ajaxSetup({cache: false}); From 5fcdb5de00d7727e77db61395a9a96f99915166e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 11 Oct 2019 16:12:36 -0400 Subject: [PATCH 2/2] Move duplicated insertFormatting function to main.js - Add insertBBCodeInTextarea function - Add BBCode url exception to insertBBCodeInTextarea --- view/js/main.js | 55 ++++++++++++++++++++++++ view/theme/duepuntozero/theme.php | 22 ---------- view/theme/frio/js/textedit.js | 39 +---------------- view/theme/quattro/js/quattro.js | 22 ---------- view/theme/smoothly/templates/bottom.tpl | 34 --------------- view/theme/smoothly/theme.php | 22 ---------- view/theme/vier/theme.php | 23 ---------- 7 files changed, 57 insertions(+), 160 deletions(-) diff --git a/view/js/main.js b/view/js/main.js index 7e84a08d87..47e7b968d0 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -406,6 +406,61 @@ $(function() { } }); +/** + * Inserts a BBCode tag in the comment textarea identified by id + * + * @param {string} BBCode + * @param {int} id + * @returns {boolean} + */ +function insertFormatting(BBCode, id) { + let textarea = document.getElementById('comment-edit-text-' + id); + + if (textarea.value === '') { + $(textarea) + .addClass("comment-edit-text-full") + .removeClass("comment-edit-text-empty"); + closeMenu("comment-fake-form-" + id); + openMenu("item-comments-" + id); + } + + insertBBCodeInTextarea(BBCode, textarea); + + return true; +} + +/** + * Inserts a BBCode tag in the provided textarea element, wrapping the currently selected text. + * For URL BBCode, it discriminates between link text and non-link text to determine where to insert the selected text. + * + * @param {string} BBCode + * @param {HTMLTextAreaElement} textarea + */ +function insertBBCodeInTextarea(BBCode, textarea) { + let selectionStart = textarea.selectionStart; + let selectionEnd = textarea.selectionEnd; + let selectedText = textarea.value.substring(selectionStart, selectionEnd); + let openingTag = '[' + BBCode + ']'; + let closingTag = '[/' + BBCode + ']'; + let cursorPosition = selectionStart + openingTag.length + selectedText.length; + + if (BBCode === 'url') { + if (urlRegex.test(selectedText)) { + openingTag = '[' + BBCode + '=' + selectedText + ']'; + selectedText = ''; + cursorPosition = selectionStart + openingTag.length; + } else { + openingTag = '[' + BBCode + '=]'; + cursorPosition = selectionStart + openingTag.length - 1; + } + } + + textarea.value = textarea.value.substring(0, selectionStart) + openingTag + selectedText + closingTag + textarea.value.substring(selectionEnd, textarea.value.length); + textarea.setSelectionRange(cursorPosition, cursorPosition); + textarea.dispatchEvent(new Event('change')); + textarea.focus(); +} + function NavUpdate() { if (!stopped) { var pingCmd = 'ping?format=json' + ((localUser != 0) ? '&f=&uid=' + localUser : ''); diff --git a/view/theme/duepuntozero/theme.php b/view/theme/duepuntozero/theme.php index 015e8090fc..363f6dc487 100644 --- a/view/theme/duepuntozero/theme.php +++ b/view/theme/duepuntozero/theme.php @@ -28,28 +28,6 @@ Renderer::setActiveTemplateEngine('smarty3'); } $a->page['htmlhead'] .= <<< EOT diff --git a/view/theme/smoothly/theme.php b/view/theme/smoothly/theme.php index 9dd958fff4..e2289c87be 100644 --- a/view/theme/smoothly/theme.php +++ b/view/theme/smoothly/theme.php @@ -23,28 +23,6 @@ function smoothly_init(App $a) { $a->page['htmlhead'] .= <<< EOT