1 // change_to: "on" or "off"
\r
2 EditArea.prototype.change_highlight= function(change_to){
\r
3 if(this.settings["syntax"].length==0 && change_to==false){
\r
4 this.switchClassSticky(_$("highlight"), 'editAreaButtonDisabled', true);
\r
5 this.switchClassSticky(_$("reset_highlight"), 'editAreaButtonDisabled', true);
\r
9 if(this.do_highlight==change_to)
\r
13 this.getIESelection();
\r
14 var pos_start= this.textarea.selectionStart;
\r
15 var pos_end= this.textarea.selectionEnd;
\r
17 if(this.do_highlight===true || change_to==false)
\r
18 this.disable_highlight();
\r
20 this.enable_highlight();
\r
21 this.textarea.focus();
\r
22 this.textarea.selectionStart = pos_start;
\r
23 this.textarea.selectionEnd = pos_end;
\r
24 this.setIESelection();
\r
28 EditArea.prototype.disable_highlight= function(displayOnly){
\r
29 var t= this, a=t.textarea, new_Obj, old_class, new_class;
\r
31 t.selection_field.innerHTML="";
\r
32 t.selection_field_text.innerHTML="";
\r
33 t.content_highlight.style.visibility="hidden";
\r
34 // replacing the node is far more faster than deleting it's content in firefox
\r
35 new_Obj= t.content_highlight.cloneNode(false);
\r
36 new_Obj.innerHTML= "";
\r
37 t.content_highlight.parentNode.insertBefore(new_Obj, t.content_highlight);
\r
38 t.content_highlight.parentNode.removeChild(t.content_highlight);
\r
39 t.content_highlight= new_Obj;
\r
40 old_class= parent.getAttribute( a,"class" );
\r
42 new_class= old_class.replace( "hidden","" );
\r
43 parent.setAttribute( a, "class", new_class );
\r
46 a.style.backgroundColor="transparent"; // needed in order to see the bracket finders
\r
48 //var icon= document.getElementById("highlight");
\r
49 //setAttribute(icon, "class", getAttribute(icon, "class").replace(/ selected/g, "") );
\r
50 //t.restoreClass(icon);
\r
51 //t.switchClass(icon,'editAreaButtonNormal');
\r
52 t.switchClassSticky(_$("highlight"), 'editAreaButtonNormal', true);
\r
53 t.switchClassSticky(_$("reset_highlight"), 'editAreaButtonDisabled', true);
\r
55 t.do_highlight=false;
\r
57 t.switchClassSticky(_$("change_smooth_selection"), 'editAreaButtonSelected', true);
\r
58 if(typeof(t.smooth_selection_before_highlight)!="undefined" && t.smooth_selection_before_highlight===false){
\r
59 t.change_smooth_selection_mode(false);
\r
62 // this.textarea.style.backgroundColor="#FFFFFF";
\r
65 EditArea.prototype.enable_highlight= function(){
\r
66 var t=this, a=t.textarea, new_class;
\r
67 t.show_waiting_screen();
\r
69 t.content_highlight.style.visibility="visible";
\r
70 new_class =parent.getAttribute(a,"class")+" hidden";
\r
71 parent.setAttribute( a, "class", new_class );
\r
73 // IE can't manage mouse click outside text range without this
\r
75 a.style.backgroundColor="#FFFFFF";
\r
77 t.switchClassSticky(_$("highlight"), 'editAreaButtonSelected', false);
\r
78 t.switchClassSticky(_$("reset_highlight"), 'editAreaButtonNormal', false);
\r
80 t.smooth_selection_before_highlight=t.smooth_selection;
\r
81 if(!t.smooth_selection)
\r
82 t.change_smooth_selection_mode(true);
\r
83 t.switchClassSticky(_$("change_smooth_selection"), 'editAreaButtonDisabled', true);
\r
86 t.do_highlight=true;
\r
87 t.resync_highlight();
\r
89 t.hide_waiting_screen();
\r
93 * Ask to update highlighted text
\r
94 * @param Array infos - Array of datas returned by EditArea.get_selection_infos()
\r
96 EditArea.prototype.maj_highlight= function(infos){
\r
98 var debug_opti="",tps_start= new Date().getTime(), tps_middle_opti=new Date().getTime();
\r
99 var t=this, hightlighted_text, updated_highlight;
\r
100 var textToHighlight=infos["full_text"], doSyntaxOpti = false, doHtmlOpti = false, stay_begin="", stay_end="", trace_new , trace_last;
\r
102 if(t.last_text_to_highlight==infos["full_text"] && t.resync_highlight!==true)
\r
105 // OPTIMISATION: will search to update only changed lines
\r
106 if(t.reload_highlight===true){
\r
107 t.reload_highlight=false;
\r
108 }else if(textToHighlight.length==0){
\r
109 textToHighlight="\n ";
\r
111 // get text change datas
\r
112 changes = t.checkTextEvolution(t.last_text_to_highlight,textToHighlight);
\r
114 // check if it can only reparse the changed text
\r
115 trace_new = t.get_syntax_trace(changes.newTextLine).replace(/\r/g, '');
\r
116 trace_last = t.get_syntax_trace(changes.lastTextLine).replace(/\r/g, '');
\r
117 doSyntaxOpti = ( trace_new == trace_last );
\r
119 // check if the difference comes only from a new line created
\r
120 // => we have to remember that the editor can automaticaly add tabulation or space after the new line)
\r
121 if( !doSyntaxOpti && trace_new == "\n"+trace_last && /^[ \t\s]*\n[ \t\s]*$/.test( changes.newText.replace(/\r/g, '') ) && changes.lastText =="" )
\r
123 doSyntaxOpti = true;
\r
126 // we do the syntax optimisation
\r
127 if( doSyntaxOpti ){
\r
129 tps_middle_opti=new Date().getTime();
\r
131 stay_begin= t.last_hightlighted_text.split("\n").slice(0, changes.lineStart).join("\n");
\r
132 if(changes.lineStart>0)
\r
134 stay_end= t.last_hightlighted_text.split("\n").slice(changes.lineLastEnd+1).join("\n");
\r
135 if(stay_end.length>0)
\r
136 stay_end= "\n"+stay_end;
\r
138 // Final check to see that we're not in the middle of span tags
\r
139 if( stay_begin.split('<span').length != stay_begin.split('</span').length
\r
140 || stay_end.split('<span').length != stay_end.split('</span').length )
\r
142 doSyntaxOpti = false;
\r
148 if(stay_begin.length==0 && changes.posLastEnd==-1)
\r
149 changes.newTextLine+="\n";
\r
150 textToHighlight=changes.newTextLine;
\r
153 if(t.settings["debug"]){
\r
155 debug_opti= ( doSyntaxOpti?"Optimisation": "No optimisation" )
\r
156 +" start: "+ch.posStart +"("+ch.lineStart+")"
\r
157 +" end_new: "+ ch.posNewEnd+"("+ch.lineNewEnd+")"
\r
158 +" end_last: "+ ch.posLastEnd+"("+ch.lineLastEnd+")"
\r
159 +"\nchanged_text: "+ch.newText+" => trace: "+trace_new
\r
160 +"\nchanged_last_text: "+ch.lastText+" => trace: "+trace_last
\r
161 //debug_opti+= "\nchanged: "+ infos["full_text"].substring(ch.posStart, ch.posNewEnd);
\r
162 + "\nchanged_line: "+ch.newTextLine
\r
163 + "\nlast_changed_line: "+ch.lastTextLine
\r
164 +"\nstay_begin: "+ stay_begin.slice(-100)
\r
165 +"\nstay_end: "+ stay_end.substr( 0, 100 );
\r
166 //debug_opti="start: "+stay_begin_len+ "("+nb_line_start_unchanged+") end: "+ (stay_end_len)+ "("+(splited.length-nb_line_end_unchanged)+") ";
\r
167 //debug_opti+="changed: "+ textToHighlight.substring(stay_begin_len, textToHighlight.length-stay_end_len)+" \n";
\r
169 //debug_opti+="changed: "+ stay_begin.substr(stay_begin.length-200)+ "----------"+ textToHighlight+"------------------"+ stay_end.substr(0,200) +"\n";
\r
174 // END OPTIMISATION
\r
177 tps_end_opti = new Date().getTime();
\r
180 updated_highlight = t.colorize_text(textToHighlight);
\r
181 tpsAfterReg = new Date().getTime();
\r
184 * see if we can optimize for updating only the required part of the HTML code
\r
186 * The goal here will be to find the text node concerned by the modification and to update it
\r
188 //-------------------------------------------
\r
194 var replacedBloc, i, nbStart = '', nbEnd = '', newHtml, lengthOld, lengthNew;
\r
195 replacedBloc = t.last_hightlighted_text.substring( stay_begin.length, t.last_hightlighted_text.length - stay_end.length );
\r
197 lengthOld = replacedBloc.length;
\r
198 lengthNew = updated_highlight.length;
\r
200 // find the identical caracters at the beginning
\r
201 for( i=0; i < lengthOld && i < lengthNew && replacedBloc.charAt(i) == updated_highlight.charAt(i) ; i++ )
\r
205 // find the identical caracters at the end
\r
206 for( i=0; i + nbStart < lengthOld && i + nbStart < lengthNew && replacedBloc.charAt(lengthOld-i-1) == updated_highlight.charAt(lengthNew-i-1) ; i++ )
\r
212 lastHtml = replacedBloc.substring( nbStart, lengthOld - nbEnd );
\r
213 newHtml = updated_highlight.substring( nbStart, lengthNew - nbEnd );
\r
216 // We can do the optimisation only if we havn't touch to span elements
\r
217 if( newHtml.indexOf('<span') == -1 && newHtml.indexOf('</span') == -1
\r
218 && lastHtml.indexOf('<span') == -1 && lastHtml.indexOf('</span') == -1 )
\r
220 var beginStr, nbOpendedSpan, nbClosedSpan, nbUnchangedChars, span, textNode;
\r
222 beginStr = t.last_hightlighted_text.substr( 0, stay_begin.length + nbStart );
\r
224 nbOpendedSpan = beginStr.split('<span').length - 1;
\r
225 nbClosedSpan = beginStr.split('</span').length - 1;
\r
226 // retrieve the previously opened span (Add 1 for the first level span?)
\r
227 span = t.content_highlight.getElementsByTagName('span')[ nbOpendedSpan ];
\r
230 // get the textNode to update
\r
232 // if we're inside a span, we'll take the one that is opened (can be a parent of the current span)
\r
234 maxStartOffset = maxEndOffset = 0;
\r
236 // it will be in the child of the root node
\r
237 if( nbOpendedSpan == nbClosedSpan )
\r
239 while( parentSpan.parentNode != t.content_highlight && parentSpan.parentNode.tagName != 'PRE' )
\r
241 parentSpan = parentSpan.parentNode;
\r
244 // get the last opened span
\r
247 maxStartOffset = maxEndOffset = beginStr.length + 1;
\r
248 // move to parent node for each closed span found after the lastest open span
\r
249 nbClosed = beginStr.substr( Math.max( 0, beginStr.lastIndexOf( '<span', maxStartOffset - 1 ) ) ).split('</span').length - 1;
\r
250 while( nbClosed > 0 )
\r
253 parentSpan = parentSpan.parentNode;
\r
256 // find the position of the last opended tag
\r
257 while( parentSpan.parentNode != t.content_highlight && parentSpan.parentNode.tagName != 'PRE' && ( tmpMaxStartOffset = Math.max( 0, beginStr.lastIndexOf( '<span', maxStartOffset - 1 ) ) ) < ( tmpMaxEndOffset = Math.max( 0, beginStr.lastIndexOf( '</span', maxEndOffset - 1 ) ) ) )
\r
259 maxStartOffset = tmpMaxStartOffset;
\r
260 maxEndOffset = tmpMaxEndOffset;
\r
263 // Note: maxEndOffset is no more used but maxStartOffset will be used
\r
265 if( parentSpan.parentNode == t.content_highlight || parentSpan.parentNode.tagName == 'PRE' )
\r
267 maxStartOffset = Math.max( 0, beginStr.indexOf( '<span' ) );
\r
270 // find the matching text node (this will be one that will be at the end of the beginStr
\r
271 if( maxStartOffset == beginStr.length )
\r
273 nbSubSpanBefore = 0;
\r
277 lastEndPos = Math.max( 0, beginStr.lastIndexOf( '>', maxStartOffset ) );
\r
279 // count the number of sub spans
\r
280 nbSubSpanBefore = beginStr.substr( lastEndPos ).split('<span').length-1;
\r
283 // there is no sub-span before
\r
284 if( nbSubSpanBefore == 0 )
\r
286 textNode = parentSpan.firstChild;
\r
288 // we need to find where is the text node modified
\r
291 // take the last direct child (no sub-child)
\r
292 lastSubSpan = parentSpan.getElementsByTagName('span')[ nbSubSpanBefore - 1 ];
\r
293 while( lastSubSpan.parentNode != parentSpan )
\r
295 lastSubSpan = lastSubSpan.parentNode;
\r
298 // associate to next text node following the last sub span
\r
299 if( lastSubSpan.nextSibling == null || lastSubSpan.nextSibling.nodeType != 3 )
\r
301 textNode = document.createTextNode('');
\r
302 lastSubSpan.parentNode.insertBefore( textNode, lastSubSpan.nextSibling );
\r
306 textNode = lastSubSpan.nextSibling;
\r
313 // update the textNode content
\r
315 // number of caracters after the last opened of closed span
\r
316 nbUnchangedChars = beginStr.length - Math.max( 0, beginStr.lastIndexOf( '>' ) + 1 );
\r
318 // console.log( span, textNode, nbOpendedSpan,nbClosedSpan, span.nextSibling, textNode.length, nbUnchangedChars, lastHtml, lastHtml.length, newHtml, newHtml.length );
\r
319 // alert( textNode.parentNode.className +'-'+ textNode.parentNode.tagName+"\n"+ textNode.data +"\n"+ nbUnchangedChars +"\n"+ lastHtml.length +"\n"+ newHtml +"\n"+ newHtml.length );
\r
321 // IE only manage \r for cariage return in textNode and not \n or \r\n
\r
324 nbUnchangedChars -= ( beginStr.substr( beginStr.length - nbUnchangedChars ).split("\n").length - 1 );
\r
325 //alert( textNode.data.replace(/\r/g, '_r').replace(/\n/g, '_n'));
\r
326 textNode.replaceData( nbUnchangedChars, lastHtml.replace(/\n/g, '').length, newHtml.replace(/\n/g, '') );
\r
330 textNode.replaceData( nbUnchangedChars, lastHtml.length, newHtml );
\r
335 // an exception shouldn't occured but if replaceData failed at least it won't break everything
\r
339 // console.log( e );
\r
340 doHtmlOpti = false;
\r
344 /*** END HTML update's optimisation ***/
\r
347 // console.log( (TPS6-TPS5), (TPS5-TPS4), (TPS4-TPS3), (TPS3-TPS2), (TPS2-TPS1), _CPT );
\r
348 // get the new highlight content
\r
349 tpsAfterOpti2 = new Date().getTime();
\r
350 hightlighted_text = stay_begin + updated_highlight + stay_end;
\r
353 // update the content of the highlight div by first updating a clone node (as there is no display in the same time for t node it's quite faster (5*))
\r
354 var new_Obj= t.content_highlight.cloneNode(false);
\r
355 if( ( t.isIE && t.isIE < 8 ) || ( t.isOpera && t.isOpera < 9.6 ) )
\r
356 new_Obj.innerHTML= "<pre><span class='"+ t.settings["syntax"] +"'>" + hightlighted_text + "</span></pre>";
\r
358 new_Obj.innerHTML= "<span class='"+ t.settings["syntax"] +"'>"+ hightlighted_text +"</span>";
\r
360 t.content_highlight.parentNode.replaceChild(new_Obj, t.content_highlight);
\r
362 t.content_highlight= new_Obj;
\r
365 t.last_text_to_highlight= infos["full_text"];
\r
366 t.last_hightlighted_text= hightlighted_text;
\r
368 tps3=new Date().getTime();
\r
370 if(t.settings["debug"]){
\r
371 //lineNumber=tab_text.length;
\r
372 //t.debug.value+=" \nNB char: "+_$("src").value.length+" Nb line: "+ lineNumber;
\r
374 t.debug.value= "Tps optimisation "+(tps_end_opti-tps_start)
\r
375 +" | tps reg exp: "+ (tpsAfterReg-tps_end_opti)
\r
376 +" | tps opti HTML : "+ (tpsAfterOpti2-tpsAfterReg) + ' '+ ( doHtmlOpti ? 'yes' : 'no' )
\r
377 +" | tps update highlight content: "+ (tps3-tpsAfterOpti2)
\r
378 +" | tpsTotal: "+ (tps3-tps_start)
\r
379 + "("+tps3+")\n"+ debug_opti;
\r
380 // t.debug.value+= "highlight\n"+hightlighted_text;*/
\r
385 EditArea.prototype.resync_highlight= function(reload_now){
\r
386 this.reload_highlight=true;
\r
387 this.last_text_to_highlight="";
\r
390 this.check_line_selection(false);
\r