I’m using the following javascript:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
… to remove nested quotes from text before it is sent to a reply box, but it leaves behind line breaks in place of the purged nested quotes. So, for instance, if I quote a post from User 1 which says…
Text.
[quote="User 2"]Quote text.[/quote]
More text.
It shows up in the reply box as…
[quote="User 1"]Text.
More text.[/quote]
What would I add to this javascript to either remove this space or limit the space between new lines to just two line breaks, so that the final text displayed in the reply box appears as:
[quote="User 1"]Text.
More text.[/quote]
Thanks for any help.
EDIT:
Here’s a larger bit of the code:
if (theSelection)
{
for (var i = 0, l = theSelection.length; i < l; i++)
{
if (theSelection[i] == '[' && theSelection[i+1] == 'q') level++;
if (theSelection[i-6] == 'q' && theSelection[i-7] == '/' && theSelection[i-8] == '[') level--;
if (level<1) tmp.push(theSelection[i]);
}
theSelection = tmp.join('');
}
if (theSelection)
{
if (bbcodeEnabled)
{
insert_text('[quote="' + username + '"]' + '\n' + '\n' + theSelection + '[/quote]' + '\n' + '\n');
}
else
{
insert_text(username + ' ' + l_wrote + ':' + '\n');
var lines = split_lines(theSelection);
for (i = 0; i < lines.length; i++)
{
insert_text('> ' + lines[i] + '\n');
}
}
}
I placed…
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
… between the two existing “if (theSelection)” statements, and it worked great. One remaining issue though. The nested quote removal happens before the quoted text is placed within its own quote tags, and two line breaks are added after the opening quote tag. So if a quoted post contained a nested quote before any original text, then the text that the above code generates has four line breaks at the opening instead of the intended two. So I think I would need to place…
if (theSelection)
{
theSelection = theSelection.replace(/\n{2,}/g, "\n\n");
}
… after theSelection is inserted within its own quote tags, at which point “if (theSelection)” seems to no longer work, and I’m so new to javascript that I don’t know how to replace “theSelection” with something that will reflect the output of the previous “if (theSelection)” statement. I hope that made at least a little sense.
Use a RegExp. The following code will replace 2+ new lines by two newlines.
I recommend to show a sign that notifies the reader of the fact that a quote has disappeared. Consider the following:
After applying your code:
The previous poster might be offended, because he didn’t know the context of the reply.