19 include_once($ROOT_PATH.
'wysiwyg_plugin.inc');
42 $this->_add_button(
'searchandreplace',
'SearchAndReplace',
'Search And Replace',
'false',
'69');
56 <script type=
"text/javascript" language=
"Javascript">
59 var global_text_nodes = Array();
62 HTMLArea.prototype._selectTextContent =
function(node, keyword, index, mcase, moption, direction, find_only) {
64 textNode = this._getTextNode(node);
65 if (textNode === null || textNode.nodeType != 3)
return -1;
67 var text = textNode.nodeValue;
68 if (text === null || keyword === null || !text.length || !keyword.length)
return -1;
71 text = mcase ? text : text.toLowerCase();
72 keyword = mcase ? keyword : keyword.toLowerCase();
75 if (direction === undefined) direction =
true;
76 if ((direction && (index + keyword.length) > text) || (!direction && (index - keyword.length) < 0))
return -1;
78 if (index === undefined || index < 0) index = 0;
80 keyword = this._escapeRegexChars(keyword);
83 var correct_match =
false;
89 word_pos = direction ? text.indexOf(keyword, index) : text.lastIndexOf(keyword, index - keyword.length);
92 left_char_empty = text.charAt(word_pos - 1).match(/[a-z0-9]/i) === null ?
true :
false;
93 right_char_empty = text.charAt(word_pos + keyword.length).match(/[a-z0-9]/i) === null ?
true:
false;
95 correct_match = (moption ==
'whole' && left_char_empty && right_char_empty) ||
96 (moption ==
'start' && left_char_empty) ||
97 (moption ==
'end' && right_char_empty) ||
101 index = direction ? index + keyword.length : index - keyword.length;
102 }
while (!correct_match && index < text.length && index >= 0);
106 if (!correct_match)
return -1;
109 if (find_only !== undefined && find_only)
return word_pos;
112 if (HTMLArea.is_ie) {
113 range = this._createTextRange();
117 var span_el = document.createElement(
"span");
118 textNode.parentNode.insertBefore(span_el, textNode);
119 range.moveToElementText(span_el);
121 range.collapse(
true);
122 range.moveStart(
"character", word_pos);
123 range.moveEnd(
"character", keyword.length);
125 range.scrollIntoView();
128 textNode.parentNode.removeChild(span_el);
130 var sel = this._getSelection();
131 range = this._doc.createRange();
132 range.setStart(textNode, word_pos);
133 range.setEnd(textNode, word_pos + keyword.length);
134 sel.removeAllRanges();
136 textNode.parentNode.scrollIntoView();
138 return direction ? word_pos + keyword.length : word_pos;
143 HTMLArea.prototype._countWord =
function(text, regex){
158 HTMLArea.prototype._getTextNodes =
function(refresh) {
160 refresh = refresh === undefined || !refresh ?
false :
true;
162 if (!refresh && global_text_nodes.length > 0) {
163 return global_text_nodes;
166 var root = !HTMLArea.is_ie ? this._iframe.contentWindow.document.body : this._docContent;
168 var text_nodes = Array();
169 var node = root.childNodes[0];
171 while(node != null) {
172 if (node.nodeType == 3) {
173 text_nodes[count++] = node;
176 if (node.hasChildNodes()) {
177 node = node.firstChild;
180 while(node.nextSibling == null && node != root) {
181 node = node.parentNode;
183 node = node.nextSibling;
188 global_text_nodes = text_nodes;
194 HTMLArea.prototype._getTextNode =
function(index){
196 if (index < 0)
return null;
201 var child_nodes = this._getTextNodes();
202 while(node = child_nodes[count]) {
204 if (count == index)
break;
212 HTMLArea.prototype._getLastTextNodeIndex =
function(){
214 var text_nodes = this._getTextNodes();
216 return text_nodes.length - 1;
221 HTMLArea.prototype._refreshStoredTextNodes =
function(){
222 global_text_nodes = Array();
227 HTMLArea.prototype._escapeRegexChars =
function(text){
229 var regex_chars = Array(
'/',
'{',
'}',
'[',
']',
'(',
')',
'-',
'+',
'*',
'^',
'$',
'?',
'.');
230 arguments.callee.regex =
new RegExp(
'(\\'+regex_chars.join(
'|\\') +
')',
'g');
232 return text.replace(arguments.callee.regex,
'\\$1');
250 case "searchandreplace":
255 var current_node = 0;
257 var search_text_not_found =
false;
260 this._popupDialog(
"SearchAndReplace",
"<?php echo $this->get_popup_href('search_and_replace.php')?>", 380, 300,
false,
function(types){
262 if (types == null)
return false;
264 var action_type = types[0];
265 var search_str = types[1];
266 var replace_str = types[2];
268 var match_case =
false;
273 var match_option =
'none';
275 match_option =
'whole';
276 }
else if (types[5]) {
277 match_option =
'start';
278 }
else if (types[6]) {
279 match_option =
'end';
280 }
else if (types[7]) {
281 match_option =
'regex';
284 selected_content = editor.getSelectedHTML();
285 whole_content = editor.getHTML();
288 var direction =
true;
290 switch (action_type) {
294 case "search_previous":
298 case "replace_current":
299 if (start_pos != -1 && selected_content.length > 0) {
300 editor.insertHTML(replace_str);
301 editor._refreshStoredTextNodes();
307 case "replace_selection":
308 if (selected_content.length > 0 || action_type ===
"replace_all") {
312 if (match_option !=
'regex') {
313 var text_nodes = editor._getTextNodes(
true);
319 if (action_type ===
"replace_selection") {
321 if (selected_content.length > 0) {
322 var sel = editor._getSelection();
323 var selected_nodes = Array();
324 var start_selection =
false;
325 alert(sel.anchorNode.nodeValue);
326 for(var count=0; count < text_nodes.length; count++) {
327 if (!start_selection && sel.anchorNode == text_nodes[count])
328 start_selection =
true;
330 selected_nodes[count] = start_selection ? text_nodes[count] : null;
331 if (sel.focusNode == text_nodes[count])
break;
333 text_nodes = selected_nodes;
335 index = sel.anchorOffset;
336 var endOffset = sel.focusOffset;
339 text_nodes = Array();
344 while(node_count < text_nodes.length) {
346 if (text_nodes[node_count] === null) {
350 index = editor._selectTextContent(node_count, search_str, index, match_case, match_option,
true,
true);
353 if (endOffset !== undefined && index+search_str.length > endOffset && node_count == text_nodes.length - 1)
break;
356 node_value = text_nodes[node_count].nodeValue;
357 node_value = (index > 0 ? node_value.substring(0, index) :
'')
359 + node_value.substring(index+search_str.length, node_value.length);
361 text_nodes[node_count].nodeValue = node_value
362 index += replace_str.length;
371 alert(match_count+
" occurrences of the given text have been replaced");
374 var wysiwyg_content = (action_type ===
"replace_all") ? whole_content : selected_content;
375 search_regex =
new RegExp(search_str, match_case ?
'g' :
'gi');
376 match_count = editor._countWord(wysiwyg_content, search_regex);
377 wysiwyg_content = wysiwyg_content.replace(search_regex, replace_str);
379 if (action_type ===
"replace_all") {
380 editor.setHTML(wysiwyg_content);
382 editor.insertHTML(wygiwyg_content);
385 alert(match_count+
" number of matching occurrences for given regular expression have been replaced");
388 if (match_count > 0) {
389 editor._refreshStoredTextNodes();
401 var last_node_index = editor._getLastTextNodeIndex();
402 while(current_node >= -1 && current_node <= (last_node_index + 1)) {
403 node = editor._getTextNode(current_node);
408 if (search_text_not_found) {
409 alert(
"The given text was not found");
410 search_text_not_found =
false;
415 alert(
"Reached the end of the WYSIWYG content. Starting from the beginning");
419 alert(
"Reached the start of the WYSIWYG content. Starting from the end");
420 last_node = editor._getTextNode(last_node_index);
421 start_pos = last_node.nodeValue.length + search_str.length;
422 current_node = last_node_index;
427 search_text_not_found =
true;
430 node = !node ? editor._getTextNode(current_node) : node;
434 if (!direction && start_pos < 0) {
435 start_pos = node.nodeValue.length + search_str.length;
439 start_pos = editor._selectTextContent(current_node, search_str, start_pos, match_case, match_option, direction);
445 direction ? current_node++ : current_node--;
449 search_text_not_found =
false;