aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/assets/javascript/editor.js
diff options
context:
space:
mode:
Diffstat (limited to 'phpBB/assets/javascript/editor.js')
-rw-r--r--phpBB/assets/javascript/editor.js73
1 files changed, 64 insertions, 9 deletions
diff --git a/phpBB/assets/javascript/editor.js b/phpBB/assets/javascript/editor.js
index dfc7dab525..df353bc29d 100644
--- a/phpBB/assets/javascript/editor.js
+++ b/phpBB/assets/javascript/editor.js
@@ -111,7 +111,6 @@ function bbfontstyle(bbopen, bbclose) {
}
textarea.focus();
- return;
}
/**
@@ -159,7 +158,7 @@ function insert_text(text, spaces, popup) {
/**
* Add inline attachment at position
*/
-function attach_inline(index, filename) {
+function attachInline(index, filename) {
insert_text('[attachment=' + index + ']' + filename + '[/attachment]');
document.forms[form_name].elements[text_name].focus();
}
@@ -167,7 +166,7 @@ function attach_inline(index, filename) {
/**
* Add quote text to message
*/
-function addquote(post_id, username, l_wrote) {
+function addquote(post_id, username, l_wrote, attributes) {
var message_name = 'message_' + post_id;
var theSelection = '';
var divarea = false;
@@ -177,6 +176,9 @@ function addquote(post_id, username, l_wrote) {
// Backwards compatibility
l_wrote = 'wrote';
}
+ if (typeof attributes !== 'object') {
+ attributes = {};
+ }
if (document.all) {
divarea = document.all[message_name];
@@ -213,7 +215,8 @@ function addquote(post_id, username, l_wrote) {
if (theSelection) {
if (bbcodeEnabled) {
- insert_text('[quote="' + username + '"]' + theSelection + '[/quote]');
+ attributes.author = username;
+ insert_text(generateQuote(theSelection, attributes));
} else {
insert_text(username + ' ' + l_wrote + ':' + '\n');
var lines = split_lines(theSelection);
@@ -222,8 +225,61 @@ function addquote(post_id, username, l_wrote) {
}
}
}
+}
- return;
+/**
+* Create a quote block for given text
+*
+* Possible attributes:
+* - author: author's name (usually a username)
+* - post_id: post_id of the post being quoted
+* - user_id: user_id of the user being quoted
+* - time: timestamp of the original message
+*
+* @param {!string} text Quote's text
+* @param {!Object} attributes Quote's attributes
+* @return {!string} Quote block to be used in a new post/text
+*/
+function generateQuote(text, attributes) {
+ text = text.replace(/^\s+/, '').replace(/\s+$/, '');
+ var quote = '[quote';
+ if (attributes.author) {
+ // Add the author as the BBCode's default attribute
+ quote += '=' + formatAttributeValue(attributes.author);
+ delete attributes.author;
+ }
+ for (var name in attributes) {
+ if (attributes.hasOwnProperty(name)) {
+ var value = attributes[name];
+ quote += ' ' + name + '=' + formatAttributeValue(value.toString());
+ }
+ }
+ quote += ']';
+ var newline = ((quote + text + '[/quote]').length > 80 || text.indexOf('\n') > -1) ? '\n' : '';
+ quote += newline + text + newline + '[/quote]';
+
+ return quote;
+}
+
+/**
+* Format given string to be used as an attribute value
+*
+* Will return the string as-is if it can be used in a BBCode without quotes. Otherwise,
+* it will use either single- or double- quotes depending on whichever requires less escaping.
+* Quotes and backslashes are escaped with backslashes where necessary
+*
+* @param {!string} str Original string
+* @return {!string} Same string if possible, escaped string within quotes otherwise
+*/
+function formatAttributeValue(str) {
+ if (!/[ "'\\\]]/.test(str)) {
+ // Return as-is if it contains none of: space, ' " \ or ]
+ return str;
+ }
+ var singleQuoted = "'" + str.replace(/[\\']/g, '\\$&') + "'",
+ doubleQuoted = '"' + str.replace(/[\\"]/g, '\\$&') + '"';
+
+ return (singleQuoted.length < doubleQuoted.length) ? singleQuoted : doubleQuoted;
}
function split_lines(text) {
@@ -266,10 +322,6 @@ function mozWrap(txtarea, open, close) {
var selEnd = txtarea.selectionEnd;
var scrollTop = txtarea.scrollTop;
- if (selEnd === 1 || selEnd === 2) {
- selEnd = selLength;
- }
-
var s1 = (txtarea.value).substring(0,selStart);
var s2 = (txtarea.value).substring(selStart, selEnd);
var s3 = (txtarea.value).substring(selEnd, selLength);
@@ -359,6 +411,9 @@ function getCaretPosition(txtarea) {
textarea = doc.forms[form_name].elements[text_name];
phpbb.applyCodeEditor(textarea);
+ if ($('#attach-panel').length) {
+ phpbb.showDragNDrop(textarea);
+ }
});
})(jQuery);