-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquote-comments.js
executable file
·240 lines (154 loc) · 5.69 KB
/
quote-comments.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Quote Comments JS
*/
function jsEncode(str){
// ugly hack
str = " " + str;
var aStr = str.split(''), i = aStr.length, aRet = [];
while (--i) {
var iC = aStr[i].charCodeAt();
if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
aRet.push('&#'+iC+';');
} else {
aRet.push(aStr[i]);
}
}
return aRet.reverse().join('');
}
function quote(postid, author, commentarea, commentID, mce) {
try {
// If you don't want quotes begin with "<author>:", uncomment the next line
//author = null;
// begin code
var posttext = '';
if (window.getSelection){
posttext = window.getSelection();
}
else if (document.getSelection){
posttext = document.getSelection();
}
else if (document.selection){
posttext = document.selection.createRange().text;
}
else {
return true;
}
if (posttext=='') { // quoting entire comment
// quoteing the entire thing
var selection = false;
var commentID = commentID.split("div-comment-")[1];
// quote entire comment as html
var theQuote = "q-"+commentID;
//var theQuote = "div-comment-"+commentID;
var posttext = document.getElementById(theQuote).innerHTML;
// remove nested divs
var posttext = posttext.replace(/<div(.*?)>((.|\n)*?)(<\/div>)/ig, "");
// remove nested blockquotes
var posttext = posttext.replace(/<blockquote(.*?)>((.|\n)*?)(<\/blockquote>)/ig, "");
var posttext = posttext.replace(/<blockquote(.*?)>((.|\n)*?)(<\/blockquote>)/ig, "");
// remove superfluous linebreaks
var posttext = posttext.replace(/\s\s/gm, "");
// do basic cleanups
var posttext = posttext.replace(/ /g, "");
//var posttext = posttext.replace(/<p>/g, "\n");
//var posttext = posttext.replace(/<\/\s*p>/g, "");
var posttext = posttext.replace(/<p>/g, "");
var posttext = posttext.replace(/<\/\s*p>/g, "\n\n");
var posttext = posttext.replace(/<br>/g, "")
// remove nonbreaking space
var posttext = posttext.replace(/ /g, " ");
// remove nested spans
var posttext = posttext.replace(/<span(.*?)>((.|\n)*?)(<\/span>)/ig, "");
// remove nested blockquotes
while (posttext != (posttext = posttext.replace(/<blockquote>[^>]*<\/\s*blockquote>/g, "")));
// remove nested quote links
var posttext = posttext.replace(/<a class="comment_quote_link"(.*?)>((.|\n)*?)(<\/a>)/ig, "");
var posttext = posttext.replace(/<a class="comment_reply_link"(.*?)>((.|\n)*?)(<\/a>)/ig, "");
}
// build quote
if (author) {
// prevent xss stuff
author = jsEncode(author);
var quote='\n<blockquote cite="comment-'+postid+'">\n\n<strong><a href="#comment-'+postid+'">'+unescape(author)+'</a></strong>: '+posttext+'</blockquote>\n';
} else {
var quote='\n<blockquote cite="comment-'+postid+'">\n\n'+posttext+'</blockquote>\n';
}
// send quoted content
if (mce == true) { // TinyMCE detected
//addQuoteMCE(comment,quote);
insertHTML(quote);
insertHTML("<p> </p>");
} else { // No TinyMCE detected
var comment=document.getElementById(commentarea);
addQuote(comment,quote);
}
return false;
} catch (e) {
alert("Quote Comments plugin is having some trouble! It could possibly be a problem with your Wordpress theme. Does it work if you use the default theme? Does it work if you disable all other plugins? If you look in the HTML source of a page with comments, can you find <div id='q-[id]'> where [id] is the ID of the comment?")
}
}
function inlinereply(postid, author, commentarea, commentID, mce) {
try {
// prevent xss stuff
author = jsEncode(author);
// build quote
var quote='\n<strong><a href="#comment-'+postid+'">'+unescape(author)+'</a></strong>, \n\n';
// send quoted content
if (mce == true) { // TinyMCE detected
//addQuoteMCE(comment,quote);
insertHTML(quote);
insertHTML("<p> </p>");
} else { // No TinyMCE detected
var comment=document.getElementById(commentarea);
addQuote(comment,quote);
}
return false;
} catch (e) {
alert("Quote Comments plugin is having some trouble! It could possibly be a problem with your Wordpress theme. Does it work if you use the default theme? Does it work if you disable all other plugins? If you look in the HTML source of a page with comments, can you find <div id='q-[id]'> where [id] is the ID of the comment?")
}
}
function addQuote(comment,quote){
/*
Derived from Alex King's JS Quicktags code (http://www.alexking.org/)
Released under LGPL license
*/
// IE support
if (document.selection) {
comment.focus();
sel = document.selection.createRange();
sel.text = quote;
comment.focus();
}
// Mozilla support
else if (comment.selectionStart || comment.selectionStart == '0') {
var startPos = comment.selectionStart;
var endPos = comment.selectionEnd;
var cursorPos = endPos;
var scrollTop = comment.scrollTop;
if (startPos != endPos) {
comment.value = comment.value.substring(0, startPos)
+ quote
+ comment.value.substring(endPos, comment.value.length);
cursorPos = startPos + quote.length
}
else {
comment.value = comment.value.substring(0, startPos)
+ quote
+ comment.value.substring(endPos, comment.value.length);
cursorPos = startPos + quote.length;
}
comment.focus();
comment.selectionStart = cursorPos;
comment.selectionEnd = cursorPos;
comment.scrollTop = scrollTop;
}
else {
comment.value += quote;
}
// If Live Preview Plugin is installed, refresh preview
try {
ReloadTextDiv();
}
catch ( e ) {
}
}