Index: trunk/extensions/UsabilityInitiative/js/plugins/codepress/engines/engine.js |
— | — | @@ -1,640 +1,638 @@ |
2 | | - |
3 | 2 | gecko = false; |
4 | 3 | if ( $.browser.safari || $.browser.mozilla ) { |
5 | | - gecko = true; |
| 4 | + gecko = true; |
6 | 5 | } |
7 | 6 | |
8 | 7 | WikiEditorEngine = { |
9 | | - scrolling : false, |
10 | | - autocomplete : true, |
11 | | - |
12 | | - initialize : function() { |
13 | | - if ( typeof ( editor ) == 'undefined' && !arguments[0] ) |
14 | | - return; |
15 | | - |
16 | | - if ( gecko ) { |
17 | | - body = document.getElementsByTagName( 'body' )[0]; |
18 | | - body.innerHTML = body.innerHTML.replace( /\n/g, "" ); |
19 | | - } |
20 | | - |
21 | | - chars = '|32|46|62|'; // charcodes that trigger syntax highlighting |
22 | | - |
23 | | - if ( gecko ) { |
24 | | - chars += '8|'; |
25 | | - } |
26 | | - |
27 | | - cc = '\u2009'; // carret char |
28 | | - editorTag = 'pre'; |
29 | | - if ( $.browser.opera ) { |
30 | | - editorTag = 'body'; |
31 | | - } |
32 | | - editor = document.getElementsByTagName( editorTag )[0]; |
33 | | - |
34 | | - if ( $.browser.msie ) { |
35 | | - editor.contentEditable = 'true'; |
36 | | - document.getElementsByTagName( 'body' )[0].onfocus = function() { |
37 | | - editor.focus(); |
38 | | - } |
39 | | - document.attachEvent( 'onkeydown', this.metaHandler ); |
40 | | - document.attachEvent( 'onkeypress', this.keyHandler ); |
41 | | - window.attachEvent( 'onscroll', function() { |
42 | | - if ( !WikiEditorEngine.scrolling ) |
43 | | - setTimeout( function() { |
44 | | - WikiEditorEngine.syntaxHighlight( 'scroll' ) |
45 | | - }, 1 ) |
46 | | - } ); |
47 | | - } else { // !$.browser.msie |
48 | | - document.designMode = 'on'; |
49 | | - if ( gecko ) { |
50 | | - document.addEventListener( 'keypress', this.keyHandler, true ); |
51 | | - } |
52 | | - if ( $.browser.opera ) { |
53 | | - document.addEventListener( 'keyup', this.keyHandler, true ); |
54 | | - } |
55 | | - window.addEventListener( 'scroll', function() { |
56 | | - if ( !WikiEditorEngine.scrolling ) |
57 | | - WikiEditorEngine.syntaxHighlight( 'scroll' ) |
58 | | - }, false ); |
59 | | - } |
60 | | - |
61 | | - completeChars = this.getCompleteChars(); |
62 | | - if ( !$.browser.opera ) { |
63 | | - completeEndingChars = this.getCompleteEndingChars(); |
64 | | - } |
65 | | - if ( $.browser.msie ) { |
66 | | - setTimeout( function() { |
67 | | - window.scroll( 0, 0 ) |
68 | | - }, 50 ); // scroll IE to top |
69 | | - } |
70 | | - }, |
71 | | - |
72 | | - keyHandler : function( evt ) { |
73 | | - if ( gecko ) { |
74 | | - keyCode = evt.keyCode; |
75 | | - charCode = evt.charCode; |
76 | | - fromChar = String.fromCharCode( charCode ); |
77 | | - |
78 | | - if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey |
79 | | - && charCode != 90 ) { // shortcuts = |
80 | | - // ctrl||appleKey+shift+key!=z(undo) |
81 | | - WikiEditorEngine.shortcuts( charCode ? charCode : keyCode ); |
82 | | - } else if ( ( completeEndingChars.indexOf( '|' + fromChar + '|' ) != -1 || completeChars |
83 | | - .indexOf( '|' + fromChar + '|' ) != -1 ) |
84 | | - && WikiEditorEngine.autocomplete ) { // auto complete |
85 | | - if ( !WikiEditorEngine.completeEnding( fromChar ) ) |
86 | | - WikiEditorEngine.complete( fromChar ); |
87 | | - } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 |
88 | | - || keyCode == 13 ) { // syntax highlighting |
89 | | - top.setTimeout( function() { |
90 | | - WikiEditorEngine.syntaxHighlight( 'generic' ); |
91 | | - }, 100 ); |
92 | | - } else if ( keyCode == 9 || evt.tabKey ) { // snippets activation |
93 | | - // (tab) |
94 | | - WikiEditorEngine.snippets( evt ); |
95 | | - } else if ( keyCode == 46 || keyCode == 8 ) { // save to history |
96 | | - // when delete or |
97 | | - // backspace pressed |
98 | | - WikiEditorEngine.actions.history[WikiEditorEngine.actions |
99 | | - .next()] = editor.innerHTML; |
100 | | - } else if ( ( charCode == 122 || charCode == 121 || charCode == 90 ) |
101 | | - && evt.ctrlKey ) { // undo and redo |
102 | | - ( charCode == 121 || evt.shiftKey ) ? WikiEditorEngine.actions |
103 | | - .redo() : WikiEditorEngine.actions.undo(); |
104 | | - evt.preventDefault(); |
105 | | - } else if ( charCode == 118 && evt.ctrlKey ) { // handle paste |
106 | | - top.setTimeout( function() { |
107 | | - WikiEditorEngine.syntaxHighlight( 'generic' ); |
108 | | - }, 100 ); |
109 | | - } else if ( charCode == 99 && evt.ctrlKey ) { // handle cut |
110 | | - // alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF')); |
111 | | - } |
112 | | - |
113 | | - } else if ( $.browser.opera ) { |
114 | | - keyCode = evt.keyCode; |
115 | | - charCode = evt.charCode; |
116 | | - |
117 | | - if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey |
118 | | - && charCode != 90 ) { // shortcuts = |
119 | | - // ctrl||appleKey+shift+key!=z(undo) |
120 | | - WikiEditorEngine.shortcuts( charCode ? charCode : keyCode ); |
121 | | - } else if ( completeChars.indexOf( '|' + String |
122 | | - .fromCharCode( charCode ) + '|' ) != -1 |
123 | | - && WikiEditorEngine.autocomplete ) { // auto complete |
124 | | - WikiEditorEngine.complete( String.fromCharCode( charCode ) ); |
125 | | - } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 |
126 | | - || keyCode == 13 ) { // syntax highlighting |
127 | | - WikiEditorEngine.syntaxHighlight( 'generic' ); |
128 | | - } else if ( keyCode == 9 || evt.tabKey ) { // snippets activation |
129 | | - // (tab) |
130 | | - WikiEditorEngine.snippets( evt ); |
131 | | - } else if ( keyCode == 46 || keyCode == 8 ) { // save to history |
132 | | - // when delete or |
133 | | - // backspace pressed |
134 | | - WikiEditorEngine.actions.history[WikiEditorEngine.actions |
135 | | - .next()] = editor.innerHTML; |
136 | | - } else if ( ( charCode == 122 || charCode == 121 || charCode == 90 ) |
137 | | - && evt.ctrlKey ) { // undo and redo |
138 | | - ( charCode == 121 || evt.shiftKey ) ? WikiEditorEngine.actions |
139 | | - .redo() : WikiEditorEngine.actions.undo(); |
140 | | - evt.preventDefault(); |
141 | | - } else if ( keyCode == 86 && evt.ctrlKey ) { // paste |
142 | | - // TODO: pasted text should be parsed and highlighted |
143 | | - } |
144 | | - } |
| 8 | + scrolling : false, |
| 9 | + autocomplete : true, |
| 10 | + |
| 11 | + initialize : function() { |
| 12 | + if ( typeof ( editor ) == 'undefined' && !arguments[0] ) |
| 13 | + return; |
| 14 | + |
| 15 | + if ( gecko ) { |
| 16 | + body = document.getElementsByTagName( 'body' )[0]; |
| 17 | + body.innerHTML = body.innerHTML.replace( /\n/g, "" ); |
| 18 | + } |
| 19 | + |
| 20 | + chars = '|32|46|62|'; // charcodes that trigger syntax highlighting |
| 21 | + |
| 22 | + if ( gecko ) { |
| 23 | + chars += '8|'; |
| 24 | + } |
| 25 | + |
| 26 | + cc = '\u2009'; // caret char |
| 27 | + editorTag = 'pre'; |
| 28 | + if ( $.browser.opera ) { |
| 29 | + editorTag = 'body'; |
| 30 | + } |
| 31 | + editor = document.getElementsByTagName( editorTag )[0]; |
| 32 | + |
| 33 | + if ( $.browser.msie ) { |
| 34 | + editor.contentEditable = 'true'; |
| 35 | + document.getElementsByTagName( 'body' )[0].onfocus = function() { |
| 36 | + editor.focus(); |
| 37 | + } |
| 38 | + document.attachEvent( 'onkeydown', this.metaHandler ); |
| 39 | + document.attachEvent( 'onkeypress', this.keyHandler ); |
| 40 | + window.attachEvent( 'onscroll', function() { |
| 41 | + if ( !WikiEditorEngine.scrolling ) |
| 42 | + setTimeout( function() { |
| 43 | + WikiEditorEngine.syntaxHighlight( 'scroll' ) |
| 44 | + }, 1 ) |
| 45 | + } ); |
| 46 | + } else { // !$.browser.msie |
| 47 | + document.designMode = 'on'; |
| 48 | + if ( gecko ) { |
| 49 | + document.addEventListener( 'keypress', this.keyHandler, true ); |
| 50 | + } |
| 51 | + if ( $.browser.opera ) { |
| 52 | + document.addEventListener( 'keyup', this.keyHandler, true ); |
| 53 | + } |
| 54 | + window.addEventListener( 'scroll', function() { |
| 55 | + if ( !WikiEditorEngine.scrolling ) |
| 56 | + WikiEditorEngine.syntaxHighlight( 'scroll' ) |
| 57 | + }, false ); |
| 58 | + } |
| 59 | + |
| 60 | + completeChars = this.getCompleteChars(); |
| 61 | + if ( !$.browser.opera ) { |
| 62 | + completeEndingChars = this.getCompleteEndingChars(); |
| 63 | + } |
| 64 | + if ( $.browser.msie ) { |
| 65 | + setTimeout( function() { |
| 66 | + window.scroll( 0, 0 ) |
| 67 | + }, 50 ); // scroll IE to top |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + keyHandler : function( evt ) { |
| 72 | + if ( gecko ) { |
| 73 | + keyCode = evt.keyCode; |
| 74 | + charCode = evt.charCode; |
| 75 | + fromChar = String.fromCharCode( charCode ); |
| 76 | + |
| 77 | + if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey && charCode != 90 ) { |
| 78 | + // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 79 | + WikiEditorEngine.shortcuts( charCode ? charCode : keyCode ); |
| 80 | + } else if ( ( completeEndingChars.indexOf( '|' + fromChar + '|' ) != -1 || |
| 81 | + completeChars.indexOf( '|' + fromChar + '|' ) != -1 ) && |
| 82 | + WikiEditorEngine.autocomplete ) { |
| 83 | + // autocomplete |
| 84 | + if ( !WikiEditorEngine.completeEnding( fromChar ) ) |
| 85 | + WikiEditorEngine.complete( fromChar ); |
| 86 | + } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 || keyCode == 13 ) { |
| 87 | + // syntax highlighting |
| 88 | + top.setTimeout( function() { |
| 89 | + WikiEditorEngine.syntaxHighlight( 'generic' ); |
| 90 | + }, 100 ); |
| 91 | + } else if ( keyCode == 9 || evt.tabKey ) { |
| 92 | + // snippets activation (tab) |
| 93 | + WikiEditorEngine.snippets( evt ); |
| 94 | + } else if ( keyCode == 46 || keyCode == 8 ) { |
| 95 | + // save to history when delete or backspace pressed |
| 96 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 97 | + } else if ( ( charCode == 122 || charCode == 121 || charCode == 90 ) && evt.ctrlKey ) { |
| 98 | + // undo and redo |
| 99 | + ( charCode == 121 || evt.shiftKey ) ? |
| 100 | + WikiEditorEngine.actions.redo() : |
| 101 | + WikiEditorEngine.actions.undo(); |
| 102 | + evt.preventDefault(); |
| 103 | + } else if ( charCode == 118 && evt.ctrlKey ) { |
| 104 | + // handle paste |
| 105 | + top.setTimeout( function() { |
| 106 | + WikiEditorEngine.syntaxHighlight( 'generic' ); |
| 107 | + }, 100 ); |
| 108 | + } else if ( charCode == 99 && evt.ctrlKey ) { |
| 109 | + // handle cut |
| 110 | + // alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF')); |
| 111 | + } |
| 112 | + } else if ( $.browser.opera ) { |
| 113 | + keyCode = evt.keyCode; |
| 114 | + charCode = evt.charCode; |
| 115 | + |
| 116 | + if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey && charCode != 90 ) { |
| 117 | + // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 118 | + WikiEditorEngine.shortcuts( charCode ? charCode : keyCode ); |
| 119 | + } else if ( completeChars.indexOf( '|' + String.fromCharCode( charCode ) + '|' ) != -1 && |
| 120 | + WikiEditorEngine.autocomplete ) { |
| 121 | + // autocomplete |
| 122 | + WikiEditorEngine.complete( String.fromCharCode( charCode ) ); |
| 123 | + } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 || keyCode == 13 ) { |
| 124 | + // syntax highlighting |
| 125 | + WikiEditorEngine.syntaxHighlight( 'generic' ); |
| 126 | + } else if ( keyCode == 9 || evt.tabKey ) { |
| 127 | + // snippets activation (tab) |
| 128 | + WikiEditorEngine.snippets( evt ); |
| 129 | + } else if ( keyCode == 46 || keyCode == 8 ) { |
| 130 | + // save to history when delete or backspace pressed |
| 131 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 132 | + } else if ( ( charCode == 122 || charCode == 121 || charCode == 90 ) |
| 133 | + && evt.ctrlKey ) { |
| 134 | + // undo and redo |
| 135 | + ( charCode == 121 || evt.shiftKey ) ? |
| 136 | + WikiEditorEngine.actions.redo() : |
| 137 | + WikiEditorEngine.actions.undo(); |
| 138 | + evt.preventDefault(); |
| 139 | + } else if ( keyCode == 86 && evt.ctrlKey ) { |
| 140 | + // paste |
| 141 | + // TODO: pasted text should be parsed and highlighted |
| 142 | + } |
| 143 | + } |
145 | 144 | |
146 | | - else if ( $.browser.msie ) { |
147 | | - charCode = evt.keyCode; |
148 | | - fromChar = String.fromCharCode( charCode ); |
149 | | - |
150 | | - if ( ( completeEndingChars.indexOf( '|' + fromChar + '|' ) != -1 || completeChars |
151 | | - .indexOf( '|' + fromChar + '|' ) != -1 ) |
152 | | - && WikiEditorEngine.autocomplete ) { // auto complete |
153 | | - if ( !WikiEditorEngine.completeEnding( fromChar ) ) |
154 | | - WikiEditorEngine.complete( fromChar ); |
155 | | - } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 |
156 | | - || charCode == 13 ) { // syntax highlighting |
157 | | - WikiEditorEngine.syntaxHighlight( 'generic' ); |
158 | | - } |
159 | | - } |
160 | | - }, |
161 | | - |
162 | | - // IE SPECIFIC FN |
163 | | - metaHandler : function( evt ) { |
164 | | - keyCode = evt.keyCode; |
165 | | - |
166 | | - if ( keyCode == 9 || evt.tabKey ) { |
167 | | - WikiEditorEngine.snippets(); |
168 | | - } else if ( ( keyCode == 122 || keyCode == 121 || keyCode == 90 ) |
169 | | - && evt.ctrlKey ) { // undo and redo |
170 | | - ( keyCode == 121 || evt.shiftKey ) ? WikiEditorEngine.actions |
171 | | - .redo() : WikiEditorEngine.actions.undo(); |
172 | | - evt.returnValue = false; |
173 | | - } else if ( keyCode == 34 || keyCode == 33 ) { // handle page up/down |
174 | | - // for IE |
175 | | - self.scrollBy( 0, ( keyCode == 34 ) ? 200 : -200 ); |
176 | | - evt.returnValue = false; |
177 | | - } else if ( keyCode == 46 || keyCode == 8 ) { // save to history when |
178 | | - // delete or backspace |
179 | | - // pressed |
180 | | - WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
181 | | - } else if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey |
182 | | - && keyCode != 90 ) { // shortcuts = |
183 | | - // ctrl||appleKey+shift+key!=z(undo) |
184 | | - WikiEditorEngine.shortcuts( keyCode ); |
185 | | - evt.returnValue = false; |
186 | | - } else if ( keyCode == 86 && evt.ctrlKey ) { // handle paste |
187 | | - window.clipboardData.setData( 'Text', window.clipboardData.getData( |
188 | | - 'Text' ).replace( /\t/g, '\u2008' ) ); |
189 | | - top.setTimeout( function() { |
190 | | - WikiEditorEngine.syntaxHighlight( 'paste' ); |
191 | | - }, 10 ); |
192 | | - } else if ( keyCode == 67 && evt.ctrlKey ) { // handle cut |
193 | | - // window.clipboardData.setData('Text',x[0]); |
194 | | - // code = window.clipboardData.getData('Text'); |
195 | | - } |
196 | | - }, |
197 | | - |
198 | | - // put cursor back to its original position after every parsing |
199 | | - findString : function() { |
200 | | - if ( gecko ) { |
201 | | - if ( self.find( cc ) ) |
202 | | - window.getSelection().getRangeAt( 0 ).deleteContents(); |
203 | | - } else if ( $.browser.opera ) { |
204 | | - var sel = window.getSelection(); |
205 | | - var range = window.document.createRange(); |
206 | | - var span = window.document.getElementsByTagName( 'span' )[0]; |
207 | | - |
208 | | - range.selectNode( span ); |
209 | | - sel.removeAllRanges(); |
210 | | - sel.addRange( range ); |
211 | | - span.parentNode.removeChild( span ); |
212 | | - // if(self.find(cc)) |
213 | | - // window.getSelection().getRangeAt(0).deleteContents(); |
214 | | - } else if ( $.browser.msie ) { |
215 | | - range = self.document.body.createTextRange(); |
216 | | - if ( range.findText( cc ) ) { |
217 | | - range.select(); |
218 | | - range.text = ''; |
219 | | - } |
220 | | - } |
221 | | - }, |
222 | | - |
223 | | - // split big files, highlighting parts of it |
224 | | - split : function( code, flag ) { |
225 | | - if ( gecko ) { |
226 | | - if ( flag == 'scroll' ) { |
227 | | - this.scrolling = true; |
228 | | - return code; |
229 | | - } else { |
230 | | - this.scrolling = false; |
231 | | - mid = code.indexOf( cc ); |
232 | | - if ( mid - 2000 < 0 ) { |
233 | | - ini = 0; |
234 | | - end = 4000; |
235 | | - } else if ( mid + 2000 > code.length ) { |
236 | | - ini = code.length - 4000; |
237 | | - end = code.length; |
238 | | - } else { |
239 | | - ini = mid - 2000; |
240 | | - end = mid + 2000; |
241 | | - } |
242 | | - code = code.substring( ini, end ); |
243 | | - return code; |
244 | | - } |
245 | | - } else if ( $.browser.opera ) { |
246 | | - if ( flag == 'scroll' ) { |
247 | | - this.scrolling = true; |
248 | | - return code; |
249 | | - } else { |
250 | | - this.scrolling = false; |
251 | | - mid = code.indexOf( '<SPAN>' ); |
252 | | - if ( mid - 2000 < 0 ) { |
253 | | - ini = 0; |
254 | | - end = 4000; |
255 | | - } else if ( mid + 2000 > code.length ) { |
256 | | - ini = code.length - 4000; |
257 | | - end = code.length; |
258 | | - } else { |
259 | | - ini = mid - 2000; |
260 | | - end = mid + 2000; |
261 | | - } |
262 | | - code = code.substring( ini, end ); |
263 | | - return code; |
264 | | - } |
265 | | - } else if ( $.browser.msie ) { |
266 | | - if ( flag == 'scroll' ) { |
267 | | - this.scrolling = true; |
268 | | - return code; |
269 | | - } else { |
270 | | - this.scrolling = false; |
271 | | - mid = code.indexOf( cc ); |
272 | | - if ( mid - 2000 < 0 ) { |
273 | | - ini = 0; |
274 | | - end = 4000; |
275 | | - } else if ( mid + 2000 > code.length ) { |
276 | | - ini = code.length - 4000; |
277 | | - end = code.length; |
278 | | - } else { |
279 | | - ini = mid - 2000; |
280 | | - end = mid + 2000; |
281 | | - } |
282 | | - code = code.substring( ini, end ); |
283 | | - return code.substring( code.indexOf( '<P>' ), code |
284 | | - .lastIndexOf( '</P>' ) + 4 ); |
285 | | - } |
286 | | - } |
287 | | - }, |
288 | | - |
289 | | - // GECKO SPECIFIC |
290 | | - getEditor : function() { |
291 | | - if ( !document.getElementsByTagName( 'pre' )[0] ) { |
292 | | - body = document.getElementsByTagName( 'body' )[0]; |
293 | | - if ( !body.innerHTML ) |
294 | | - return body; |
295 | | - if ( body.innerHTML == "<br>" ) |
296 | | - body.innerHTML = "<pre> </pre>"; |
297 | | - else |
298 | | - body.innerHTML = "<pre>" + body.innerHTML + "</pre>"; |
299 | | - } |
300 | | - return document.getElementsByTagName( 'pre' )[0]; |
301 | | - }, |
302 | | - |
303 | | - // syntax highlighting parser |
304 | | - syntaxHighlight : function( flag ) { |
305 | | - if ( gecko ) { |
306 | | - if ( flag != 'init' ) { |
307 | | - window.getSelection().getRangeAt( 0 ).insertNode( |
308 | | - document.createTextNode( cc ) ); |
309 | | - } |
310 | | - editor = WikiEditorEngine.getEditor(); |
311 | | - o = editor.innerHTML; |
312 | | - o = o.replace( /<br>/g, '\n' ); |
313 | | - o = o.replace( /<.*?>/g, '' ); |
314 | | - x = z = this.split( o, flag ); |
315 | | - x = x.replace( /\n/g, '<br>' ); |
316 | | - |
317 | | - if ( arguments[1] && arguments[2] ) |
318 | | - x = x.replace( arguments[1], arguments[2] ); |
319 | | - |
320 | | - for ( i = 0; i < Language.syntax.length; i++ ) |
321 | | - x = x.replace( Language.syntax[i].input, |
322 | | - Language.syntax[i].output ); |
323 | | - |
324 | | - editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? x |
325 | | - : o.split( z ).join( x ); |
326 | | - if ( flag != 'init' ) |
327 | | - this.findString(); |
328 | | - } else if ( $.browser.opera ) { |
329 | | - if ( flag != 'init' ) { |
330 | | - var span = document.createElement( 'span' ); |
331 | | - window.getSelection().getRangeAt( 0 ).insertNode( span ); |
332 | | - } |
333 | | - |
334 | | - o = editor.innerHTML; |
335 | | - |
336 | | - o = o.replace( /<(?!span|\/span|br).*?>/gi, '' ); |
337 | | - x = z = this.split( o, flag ); |
338 | | - x = x.replace( /\t/g, ' ' ); |
339 | | - |
340 | | - if ( arguments[1] && arguments[2] ) |
341 | | - x = x.replace( arguments[1], arguments[2] ); |
342 | | - |
343 | | - for ( i = 0; i < Language.syntax.length; i++ ) |
344 | | - x = x.replace( Language.syntax[i].input, |
345 | | - Language.syntax[i].output ); |
346 | | - |
347 | | - editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? x |
348 | | - : o.split( z ).join( x ); |
349 | | - |
350 | | - if ( flag != 'init' ) |
351 | | - this.findString(); |
352 | | - } else if ( $.browser.msie ) { |
353 | | - if ( flag != 'init' ) |
354 | | - document.selection.createRange().text = cc; |
355 | | - o = editor.innerHTML; |
356 | | - if ( flag == 'paste' ) { // fix pasted text |
357 | | - o = o.replace( /<BR>/g, '\r\n' ); |
358 | | - o = o.replace( /\u2008/g, '\t' ); |
359 | | - } |
360 | | - o = o.replace( /<P>/g, '\n' ); |
361 | | - o = o.replace( /<\/P>/g, '\r' ); |
362 | | - o = o.replace( /<.*?>/g, '' ); |
363 | | - o = o.replace( / /g, '' ); |
364 | | - o = '<PRE><P>' + o + '</P></PRE>'; |
365 | | - o = o.replace( /\n\r/g, '<P></P>' ); |
366 | | - o = o.replace( /\n/g, '<P>' ); |
367 | | - o = o.replace( /\r/g, '<\/P>' ); |
368 | | - o = o.replace( /<P>(<P>)+/, '<P>' ); |
369 | | - o = o.replace( /<\/P>(<\/P>)+/, '</P>' ); |
370 | | - o = o.replace( /<P><\/P>/g, '<P><BR/></P>' ); |
371 | | - x = z = this.split( o, flag ); |
372 | | - |
373 | | - if ( arguments[1] && arguments[2] ) |
374 | | - x = x.replace( arguments[1], arguments[2] ); |
375 | | - |
376 | | - for ( i = 0; i < Language.syntax.length; i++ ) |
377 | | - x = x.replace( Language.syntax[i].input, |
378 | | - Language.syntax[i].output ); |
379 | | - |
380 | | - editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? x |
381 | | - : o.replace( z, x ); |
382 | | - if ( flag != 'init' ) |
383 | | - this.findString(); |
384 | | - } |
385 | | - }, |
386 | | - |
387 | | - getLastWord : function() { |
388 | | - var rangeAndCaret = WikiEditorEngine.getRangeAndCaret(); |
389 | | - words = rangeAndCaret[0].substring( rangeAndCaret[1] - 40, |
390 | | - rangeAndCaret[1] ); |
391 | | - words = words.replace( /[\s\n\r\);\W]/g, '\n' ).split( '\n' ); |
392 | | - return words[words.length - 1].replace( /[\W]/gi, '' ).toLowerCase(); |
393 | | - }, |
394 | | - |
395 | | - snippets : function( evt ) { |
396 | | - var snippets = Language.snippets; |
397 | | - var trigger = this.getLastWord(); |
398 | | - for ( var i = 0; i < snippets.length; i++ ) { |
399 | | - if ( snippets[i].input == trigger ) { |
400 | | - var content = snippets[i].output.replace( /</g, '<' ); |
401 | | - content = content.replace( />/g, '>' ); |
402 | | - if ( content.indexOf( '$0' ) < 0 ) |
403 | | - content += cc; |
404 | | - else |
405 | | - content = content.replace( /\$0/, cc ); |
406 | | - content = content.replace( /\n/g, '<br>' ); |
407 | | - var pattern = new RegExp( trigger + cc, 'gi' ); |
408 | | - if ( !$.browser.msie ) { |
409 | | - evt.preventDefault(); // prevent the tab key from being |
410 | | - // added |
411 | | - } |
412 | | - this.syntaxHighlight( 'snippets', pattern, content ); |
413 | | - } |
414 | | - } |
415 | | - }, |
416 | | - |
417 | | - readOnly : function() { |
418 | | - if ( $.browser.msie ) { |
419 | | - editor.contentEditable = ( arguments[0] ) ? 'false' : 'true'; |
420 | | - } else if ( gecko || $.browser.opera ) { |
421 | | - document.designMode = ( arguments[0] ) ? 'off' : 'on'; |
422 | | - } |
423 | | - }, |
424 | | - |
425 | | - complete : function( trigger ) { |
426 | | - window.getSelection().getRangeAt( 0 ).deleteContents(); |
427 | | - var complete = Language.complete; |
428 | | - for ( var i = 0; i < complete.length; i++ ) { |
429 | | - if ( complete[i].input == trigger ) { |
430 | | - var pattern = new RegExp( '\\' + trigger + cc ); |
431 | | - var content = complete[i].output.replace( /\$0/g, cc ); |
432 | | - parent.setTimeout( function() { |
433 | | - WikiEditorEngine.syntaxHighlight( 'complete', pattern, |
434 | | - content ) |
435 | | - }, 0 ); // wait for char to appear on screen |
436 | | - } |
437 | | - } |
438 | | - }, |
439 | | - |
440 | | - getCompleteChars : function() { |
441 | | - var cChars = ''; |
442 | | - for ( var i = 0; i < Language.complete.length; i++ ) |
443 | | - cChars += '|' + Language.complete[i].input; |
444 | | - return cChars + '|'; |
445 | | - }, |
446 | | - |
447 | | - // not in Opera |
448 | | - getCompleteEndingChars : function() { |
449 | | - var cChars = ''; |
450 | | - for ( var i = 0; i < Language.complete.length; i++ ) |
451 | | - cChars += '|' + Language.complete[i].output |
452 | | - .charAt( Language.complete[i].output.length - 1 ); |
453 | | - return cChars + '|'; |
454 | | - }, |
455 | | - |
456 | | - // also not in opera |
457 | | - completeEnding : function( trigger ) { |
458 | | - if ( gecko ) { |
459 | | - var range = window.getSelection().getRangeAt( 0 ); |
460 | | - try { |
461 | | - range.setEnd( range.endContainer, range.endOffset + 1 ) |
462 | | - } catch ( e ) { |
463 | | - return false; |
464 | | - } |
465 | | - var next_character = range.toString() |
466 | | - range.setEnd( range.endContainer, range.endOffset - 1 ) |
467 | | - if ( next_character != trigger ) |
468 | | - return false; |
469 | | - else { |
470 | | - range.setEnd( range.endContainer, range.endOffset + 1 ) |
471 | | - range.deleteContents(); |
472 | | - return true; |
473 | | - } |
474 | | - } else if ( $.browser.msie ) { |
475 | | - var range = document.selection.createRange(); |
476 | | - try { |
477 | | - range.moveEnd( 'character', 1 ) |
478 | | - } catch ( e ) { |
479 | | - return false; |
480 | | - } |
481 | | - var next_character = range.text |
482 | | - range.moveEnd( 'character', -1 ) |
483 | | - if ( next_character != trigger ) |
484 | | - return false; |
485 | | - else { |
486 | | - range.moveEnd( 'character', 1 ) |
487 | | - range.text = '' |
488 | | - return true; |
489 | | - } |
490 | | - } |
491 | | - }, |
492 | | - |
493 | | - shortcuts : function() { |
494 | | - var cCode = arguments[0]; |
495 | | - if ( cCode == 13 ) |
496 | | - cCode = '[enter]'; |
497 | | - else if ( cCode == 32 ) |
498 | | - cCode = '[space]'; |
499 | | - else |
500 | | - cCode = '[' + String.fromCharCode( charCode ).toLowerCase() + ']'; |
501 | | - for ( var i = 0; i < Language.shortcuts.length; i++ ) |
502 | | - if ( Language.shortcuts[i].input == cCode ) |
503 | | - this.insertCode( Language.shortcuts[i].output, false ); |
504 | | - }, |
505 | | - |
506 | | - getRangeAndCaret : function() { |
507 | | - if ( $.browser.msie ) { |
508 | | - var range = document.selection.createRange(); |
509 | | - var caret = Math.abs( range.moveStart( 'character', -1000000 ) + 1 ); |
510 | | - range = this.getCode(); |
511 | | - range = range.replace( /\n\r/gi, ' ' ); |
512 | | - range = range.replace( /\n/gi, '' ); |
513 | | - return [ range.toString(), caret ]; |
514 | | - } else if ( !$.browser.msie ) { |
515 | | - var range = window.getSelection().getRangeAt( 0 ); |
516 | | - var range2 = range.cloneRange(); |
517 | | - var node = range.endContainer; |
518 | | - var caret = range.endOffset; |
519 | | - range2.selectNode( node ); |
520 | | - return [ range2.toString(), caret ]; |
521 | | - } |
522 | | - }, |
523 | | - |
524 | | - insertCode : function( code, replaceCursorBefore ) { |
525 | | - if ( $.browser.msie ) { |
526 | | - var repdeb = ''; |
527 | | - var repfin = ''; |
528 | | - |
529 | | - if ( replaceCursorBefore ) { |
530 | | - repfin = code; |
531 | | - } else { |
532 | | - repdeb = code; |
533 | | - } |
534 | | - |
535 | | - if ( typeof document.selection != 'undefined' ) { |
536 | | - var range = document.selection.createRange(); |
537 | | - range.text = repdeb + repfin; |
538 | | - range = document.selection.createRange(); |
539 | | - range.move( 'character', -repfin.length ); |
540 | | - range.select(); |
541 | | - } |
542 | | - } else if ( !$.browser.msie ) { |
543 | | - var range = window.getSelection().getRangeAt( 0 ); |
544 | | - var node = window.document.createTextNode( code ); |
545 | | - var selct = window.getSelection(); |
546 | | - var range2 = range.cloneRange(); |
547 | | - // Insert text at cursor position |
548 | | - selct.removeAllRanges(); |
549 | | - range.deleteContents(); |
550 | | - range.insertNode( node ); |
551 | | - // Move the cursor to the end of text |
552 | | - range2.selectNode( node ); |
553 | | - range2.collapse( replaceCursorBefore ); |
554 | | - selct.removeAllRanges(); |
555 | | - selct.addRange( range2 ); |
556 | | - } |
557 | | - }, |
558 | | - |
559 | | - getCode : function() { |
560 | | - if ( gecko && !document.getElementsByTagName( 'pre' )[0] |
561 | | - || editor.innerHTML == '' ) |
562 | | - editor = WikiEditorEngine.getEditor(); |
563 | | - var code = editor.innerHTML; |
564 | | - code = code.replace( /<br>/g, '\n' ); |
565 | | - code = code.replace( /\u2009/g, '' ); |
566 | | - code = code.replace( /<.*?>/g, '' ); |
567 | | - code = code.replace( /</g, '<' ); |
568 | | - code = code.replace( />/g, '>' ); |
569 | | - code = code.replace( /&/gi, '&' ); |
570 | | - |
571 | | - if ( $.browser.msie ) { |
572 | | - code = code.replace( /<\/p>/gi, '\r' ); |
573 | | - code = code.replace( /<p>/i, '' ); // IE first line fix |
574 | | - code = code.replace( /<p>/gi, '\n' ); |
575 | | - code = code.replace( / /gi, '' ); |
576 | | - } |
577 | | - return code; |
578 | | - }, |
579 | | - |
580 | | - setCode : function() { |
581 | | - var code = arguments[0]; |
582 | | - code = code.replace( /\u2009/gi, '' ); |
583 | | - code = code.replace( /&/gi, '&' ); |
584 | | - code = code.replace( /</g, '<' ); |
585 | | - code = code.replace( />/g, '>' ); |
586 | | - if ( $.browser.msie ) { |
587 | | - editor.innerHTML = '<pre>' + code + '</pre>'; |
588 | | - } else if ( !$.browser.msie ) { |
589 | | - editor.innerHTML = code; |
590 | | - } |
591 | | - if ( gecko && code == '' ) { |
592 | | - document.getElementsByTagName( 'body' )[0].innerHTML = ''; |
593 | | - } |
594 | | - }, |
595 | | - |
596 | | - // g |
597 | | - actions : { |
598 | | - pos : -1, // actual history position |
599 | | - history : [], // history vector |
600 | | - |
601 | | - undo : function() { |
602 | | - if ( gecko ) { |
603 | | - editor = WikiEditorEngine.getEditor(); |
604 | | - } |
605 | | - if ( editor.innerHTML.indexOf( cc ) == -1 ) { |
606 | | - if ( opera || ( gecko && editor.innerHTML != " " ) ) |
607 | | - window.getSelection().getRangeAt( 0 ).insertNode( |
608 | | - document.createTextNode( cc ) ); |
609 | | - if ( $.browser.msie ) { |
610 | | - document.selection.createRange().text = cc; |
611 | | - } |
612 | | - this.history[this.pos] = editor.innerHTML; |
613 | | - } |
614 | | - this.pos--; |
615 | | - if ( typeof ( this.history[this.pos] ) == 'undefined' ) |
616 | | - this.pos++; // in gecko it was pos ++ with a space. necessary? |
617 | | - editor.innerHTML = this.history[this.pos]; |
618 | | - if ( gecko && editor.innerHTML.indexOf( cc ) > -1 ) |
619 | | - editor.innerHTML += cc; |
620 | | - WikiEditorEngine.findString(); |
621 | | - }, |
622 | | - |
623 | | - redo : function() { |
624 | | - // editor = WikiEditorEngine.getEditor(); |
625 | | - this.pos++; |
626 | | - if ( typeof ( this.history[this.pos] ) == 'undefined' ) |
627 | | - this.pos--; |
628 | | - editor.innerHTML = this.history[this.pos]; |
629 | | - WikiEditorEngine.findString(); |
630 | | - }, |
631 | | - |
632 | | - next : function() { // get next vector position and clean old ones |
633 | | - if ( this.pos > 20 ) |
634 | | - this.history[this.pos - 21] = undefined; |
635 | | - return ++this.pos; |
636 | | - } |
637 | | - } |
638 | | - |
| 145 | + else if ( $.browser.msie ) { |
| 146 | + charCode = evt.keyCode; |
| 147 | + fromChar = String.fromCharCode( charCode ); |
| 148 | + |
| 149 | + if ( ( completeEndingChars.indexOf( '|' + fromChar + '|' ) != -1 || |
| 150 | + completeChars.indexOf( '|' + fromChar + '|' ) != -1 ) && |
| 151 | + WikiEditorEngine.autocomplete ) { |
| 152 | + // autocomplete |
| 153 | + if ( !WikiEditorEngine.completeEnding( fromChar ) ) |
| 154 | + WikiEditorEngine.complete( fromChar ); |
| 155 | + } else if ( chars.indexOf( '|' + charCode + '|' ) != -1 || charCode == 13 ) { |
| 156 | + // syntax highlighting |
| 157 | + WikiEditorEngine.syntaxHighlight( 'generic' ); |
| 158 | + } |
| 159 | + } |
| 160 | + }, |
| 161 | + |
| 162 | + // IE SPECIFIC FN |
| 163 | + metaHandler : function( evt ) { |
| 164 | + keyCode = evt.keyCode; |
| 165 | + |
| 166 | + if ( keyCode == 9 || evt.tabKey ) { |
| 167 | + WikiEditorEngine.snippets(); |
| 168 | + } else if ( ( keyCode == 122 || keyCode == 121 || keyCode == 90 ) && evt.ctrlKey ) { |
| 169 | + // undo and redo |
| 170 | + ( keyCode == 121 || evt.shiftKey ) ? |
| 171 | + WikiEditorEngine.actions.redo() : |
| 172 | + WikiEditorEngine.actions.undo(); |
| 173 | + evt.returnValue = false; |
| 174 | + } else if ( keyCode == 34 || keyCode == 33 ) { |
| 175 | + // handle page up/down for IE |
| 176 | + self.scrollBy( 0, ( keyCode == 34 ) ? 200 : -200 ); |
| 177 | + evt.returnValue = false; |
| 178 | + } else if ( keyCode == 46 || keyCode == 8 ) { |
| 179 | + // save to history when delete or backspace pressed |
| 180 | + WikiEditorEngine.actions.history[WikiEditorEngine.actions.next()] = editor.innerHTML; |
| 181 | + } else if ( ( evt.ctrlKey || evt.metaKey ) && evt.shiftKey && keyCode != 90 ) { |
| 182 | + // shortcuts = ctrl||appleKey+shift+key!=z(undo) |
| 183 | + WikiEditorEngine.shortcuts( keyCode ); |
| 184 | + evt.returnValue = false; |
| 185 | + } else if ( keyCode == 86 && evt.ctrlKey ) { |
| 186 | + // handle paste |
| 187 | + window.clipboardData.setData( 'Text', window.clipboardData.getData( |
| 188 | + 'Text' ).replace( /\t/g, '\u2008' ) ); |
| 189 | + top.setTimeout( function() { |
| 190 | + WikiEditorEngine.syntaxHighlight( 'paste' ); |
| 191 | + }, 10 ); |
| 192 | + } else if ( keyCode == 67 && evt.ctrlKey ) { |
| 193 | + // handle cut |
| 194 | + // window.clipboardData.setData('Text',x[0]); |
| 195 | + // code = window.clipboardData.getData('Text'); |
| 196 | + } |
| 197 | + }, |
| 198 | + |
| 199 | + // put cursor back to its original position after every parsing |
| 200 | + findString : function() { |
| 201 | + if ( gecko ) { |
| 202 | + if ( self.find( cc ) ) |
| 203 | + window.getSelection().getRangeAt( 0 ).deleteContents(); |
| 204 | + } else if ( $.browser.opera ) { |
| 205 | + var sel = window.getSelection(); |
| 206 | + var range = window.document.createRange(); |
| 207 | + var span = window.document.getElementsByTagName( 'span' )[0]; |
| 208 | + |
| 209 | + range.selectNode( span ); |
| 210 | + sel.removeAllRanges(); |
| 211 | + sel.addRange( range ); |
| 212 | + span.parentNode.removeChild( span ); |
| 213 | + // if(self.find(cc)) |
| 214 | + // window.getSelection().getRangeAt(0).deleteContents(); |
| 215 | + } else if ( $.browser.msie ) { |
| 216 | + range = self.document.body.createTextRange(); |
| 217 | + if ( range.findText( cc ) ) { |
| 218 | + range.select(); |
| 219 | + range.text = ''; |
| 220 | + } |
| 221 | + } |
| 222 | + }, |
| 223 | + |
| 224 | + // split big files, highlighting parts of it |
| 225 | + split : function( code, flag ) { |
| 226 | + if ( gecko ) { |
| 227 | + if ( flag == 'scroll' ) { |
| 228 | + this.scrolling = true; |
| 229 | + return code; |
| 230 | + } else { |
| 231 | + this.scrolling = false; |
| 232 | + mid = code.indexOf( cc ); |
| 233 | + if ( mid - 2000 < 0 ) { |
| 234 | + ini = 0; |
| 235 | + end = 4000; |
| 236 | + } else if ( mid + 2000 > code.length ) { |
| 237 | + ini = code.length - 4000; |
| 238 | + end = code.length; |
| 239 | + } else { |
| 240 | + ini = mid - 2000; |
| 241 | + end = mid + 2000; |
| 242 | + } |
| 243 | + code = code.substring( ini, end ); |
| 244 | + return code; |
| 245 | + } |
| 246 | + } else if ( $.browser.opera ) { |
| 247 | + if ( flag == 'scroll' ) { |
| 248 | + this.scrolling = true; |
| 249 | + return code; |
| 250 | + } else { |
| 251 | + this.scrolling = false; |
| 252 | + mid = code.indexOf( '<SPAN>' ); |
| 253 | + if ( mid - 2000 < 0 ) { |
| 254 | + ini = 0; |
| 255 | + end = 4000; |
| 256 | + } else if ( mid + 2000 > code.length ) { |
| 257 | + ini = code.length - 4000; |
| 258 | + end = code.length; |
| 259 | + } else { |
| 260 | + ini = mid - 2000; |
| 261 | + end = mid + 2000; |
| 262 | + } |
| 263 | + code = code.substring( ini, end ); |
| 264 | + return code; |
| 265 | + } |
| 266 | + } else if ( $.browser.msie ) { |
| 267 | + if ( flag == 'scroll' ) { |
| 268 | + this.scrolling = true; |
| 269 | + return code; |
| 270 | + } else { |
| 271 | + this.scrolling = false; |
| 272 | + mid = code.indexOf( cc ); |
| 273 | + if ( mid - 2000 < 0 ) { |
| 274 | + ini = 0; |
| 275 | + end = 4000; |
| 276 | + } else if ( mid + 2000 > code.length ) { |
| 277 | + ini = code.length - 4000; |
| 278 | + end = code.length; |
| 279 | + } else { |
| 280 | + ini = mid - 2000; |
| 281 | + end = mid + 2000; |
| 282 | + } |
| 283 | + code = code.substring( ini, end ); |
| 284 | + return code.substring( code.indexOf( '<P>' ), |
| 285 | + code.lastIndexOf( '</P>' ) + 4 ); |
| 286 | + } |
| 287 | + } |
| 288 | + }, |
| 289 | + |
| 290 | + // GECKO SPECIFIC |
| 291 | + getEditor : function() { |
| 292 | + if ( !document.getElementsByTagName( 'pre' )[0] ) { |
| 293 | + body = document.getElementsByTagName( 'body' )[0]; |
| 294 | + if ( !body.innerHTML ) |
| 295 | + return body; |
| 296 | + if ( body.innerHTML == "<br>" ) |
| 297 | + body.innerHTML = "<pre> </pre>"; |
| 298 | + else |
| 299 | + body.innerHTML = "<pre>" + body.innerHTML + "</pre>"; |
| 300 | + } |
| 301 | + return document.getElementsByTagName( 'pre' )[0]; |
| 302 | + }, |
| 303 | + |
| 304 | + // syntax highlighting parser |
| 305 | + syntaxHighlight : function( flag ) { |
| 306 | + if ( gecko ) { |
| 307 | + if ( flag != 'init' ) { |
| 308 | + window.getSelection().getRangeAt( 0 ).insertNode( |
| 309 | + document.createTextNode( cc ) ); |
| 310 | + } |
| 311 | + editor = WikiEditorEngine.getEditor(); |
| 312 | + o = editor.innerHTML; |
| 313 | + o = o.replace( /<br>/g, '\n' ); |
| 314 | + o = o.replace( /<.*?>/g, '' ); |
| 315 | + x = z = this.split( o, flag ); |
| 316 | + x = x.replace( /\n/g, '<br>' ); |
| 317 | + |
| 318 | + if ( arguments[1] && arguments[2] ) |
| 319 | + x = x.replace( arguments[1], arguments[2] ); |
| 320 | + |
| 321 | + for ( i = 0; i < Language.syntax.length; i++ ) |
| 322 | + x = x.replace( Language.syntax[i].input, |
| 323 | + Language.syntax[i].output ); |
| 324 | + |
| 325 | + editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? |
| 326 | + x : o.split( z ).join( x ); |
| 327 | + if ( flag != 'init' ) |
| 328 | + this.findString(); |
| 329 | + } else if ( $.browser.opera ) { |
| 330 | + if ( flag != 'init' ) { |
| 331 | + var span = document.createElement( 'span' ); |
| 332 | + window.getSelection().getRangeAt( 0 ).insertNode( span ); |
| 333 | + } |
| 334 | + |
| 335 | + o = editor.innerHTML; |
| 336 | + |
| 337 | + o = o.replace( /<(?!span|\/span|br).*?>/gi, '' ); |
| 338 | + x = z = this.split( o, flag ); |
| 339 | + x = x.replace( /\t/g, ' ' ); |
| 340 | + |
| 341 | + if ( arguments[1] && arguments[2] ) |
| 342 | + x = x.replace( arguments[1], arguments[2] ); |
| 343 | + |
| 344 | + for ( i = 0; i < Language.syntax.length; i++ ) |
| 345 | + x = x.replace( Language.syntax[i].input, |
| 346 | + Language.syntax[i].output ); |
| 347 | + |
| 348 | + editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? |
| 349 | + x : o.split( z ).join( x ); |
| 350 | + |
| 351 | + if ( flag != 'init' ) |
| 352 | + this.findString(); |
| 353 | + } else if ( $.browser.msie ) { |
| 354 | + if ( flag != 'init' ) |
| 355 | + document.selection.createRange().text = cc; |
| 356 | + o = editor.innerHTML; |
| 357 | + if ( flag == 'paste' ) { // fix pasted text |
| 358 | + o = o.replace( /<BR>/g, '\r\n' ); |
| 359 | + o = o.replace( /\u2008/g, '\t' ); |
| 360 | + } |
| 361 | + o = o.replace( /<P>/g, '\n' ); |
| 362 | + o = o.replace( /<\/P>/g, '\r' ); |
| 363 | + o = o.replace( /<.*?>/g, '' ); |
| 364 | + o = o.replace( / /g, '' ); |
| 365 | + o = '<PRE><P>' + o + '</P></PRE>'; |
| 366 | + o = o.replace( /\n\r/g, '<P></P>' ); |
| 367 | + o = o.replace( /\n/g, '<P>' ); |
| 368 | + o = o.replace( /\r/g, '<\/P>' ); |
| 369 | + o = o.replace( /<P>(<P>)+/, '<P>' ); |
| 370 | + o = o.replace( /<\/P>(<\/P>)+/, '</P>' ); |
| 371 | + o = o.replace( /<P><\/P>/g, '<P><BR/></P>' ); |
| 372 | + x = z = this.split( o, flag ); |
| 373 | + |
| 374 | + if ( arguments[1] && arguments[2] ) |
| 375 | + x = x.replace( arguments[1], arguments[2] ); |
| 376 | + |
| 377 | + for ( i = 0; i < Language.syntax.length; i++ ) |
| 378 | + x = x.replace( Language.syntax[i].input, |
| 379 | + Language.syntax[i].output ); |
| 380 | + |
| 381 | + editor.innerHTML = this.actions.history[this.actions.next()] = ( flag == 'scroll' ) ? |
| 382 | + x : o.replace( z, x ); |
| 383 | + if ( flag != 'init' ) |
| 384 | + this.findString(); |
| 385 | + } |
| 386 | + }, |
| 387 | + |
| 388 | + getLastWord : function() { |
| 389 | + var rangeAndCaret = WikiEditorEngine.getRangeAndCaret(); |
| 390 | + words = rangeAndCaret[0].substring( rangeAndCaret[1] - 40, |
| 391 | + rangeAndCaret[1] ); |
| 392 | + words = words.replace( /[\s\n\r\);\W]/g, '\n' ).split( '\n' ); |
| 393 | + return words[words.length - 1].replace( /[\W]/gi, '' ).toLowerCase(); |
| 394 | + }, |
| 395 | + |
| 396 | + snippets : function( evt ) { |
| 397 | + var snippets = Language.snippets; |
| 398 | + var trigger = this.getLastWord(); |
| 399 | + for ( var i = 0; i < snippets.length; i++ ) { |
| 400 | + if ( snippets[i].input == trigger ) { |
| 401 | + var content = snippets[i].output.replace( /</g, '<' ); |
| 402 | + content = content.replace( />/g, '>' ); |
| 403 | + if ( content.indexOf( '$0' ) < 0 ) |
| 404 | + content += cc; |
| 405 | + else |
| 406 | + content = content.replace( /\$0/, cc ); |
| 407 | + content = content.replace( /\n/g, '<br>' ); |
| 408 | + var pattern = new RegExp( trigger + cc, 'gi' ); |
| 409 | + if ( !$.browser.msie ) { |
| 410 | + // prevent the tab key from being added |
| 411 | + evt.preventDefault(); |
| 412 | + } |
| 413 | + this.syntaxHighlight( 'snippets', pattern, content ); |
| 414 | + } |
| 415 | + } |
| 416 | + }, |
| 417 | + |
| 418 | + readOnly : function() { |
| 419 | + if ( $.browser.msie ) { |
| 420 | + editor.contentEditable = ( arguments[0] ) ? 'false' : 'true'; |
| 421 | + } else if ( gecko || $.browser.opera ) { |
| 422 | + document.designMode = ( arguments[0] ) ? 'off' : 'on'; |
| 423 | + } |
| 424 | + }, |
| 425 | + |
| 426 | + complete : function( trigger ) { |
| 427 | + window.getSelection().getRangeAt( 0 ).deleteContents(); |
| 428 | + var complete = Language.complete; |
| 429 | + for ( var i = 0; i < complete.length; i++ ) { |
| 430 | + if ( complete[i].input == trigger ) { |
| 431 | + var pattern = new RegExp( '\\' + trigger + cc ); |
| 432 | + var content = complete[i].output.replace( /\$0/g, cc ); |
| 433 | + parent.setTimeout( function() { |
| 434 | + WikiEditorEngine.syntaxHighlight( 'complete', pattern, content ); |
| 435 | + }, 0 ); // wait for char to appear on screen |
| 436 | + } |
| 437 | + } |
| 438 | + }, |
| 439 | + |
| 440 | + getCompleteChars : function() { |
| 441 | + var cChars = ''; |
| 442 | + for ( var i = 0; i < Language.complete.length; i++ ) |
| 443 | + cChars += '|' + Language.complete[i].input; |
| 444 | + return cChars + '|'; |
| 445 | + }, |
| 446 | + |
| 447 | + // not in Opera |
| 448 | + getCompleteEndingChars : function() { |
| 449 | + var cChars = ''; |
| 450 | + for ( var i = 0; i < Language.complete.length; i++ ) |
| 451 | + cChars += '|' + Language.complete[i].output |
| 452 | + .charAt( Language.complete[i].output.length - 1 ); |
| 453 | + return cChars + '|'; |
| 454 | + }, |
| 455 | + |
| 456 | + // also not in opera |
| 457 | + completeEnding : function( trigger ) { |
| 458 | + if ( gecko ) { |
| 459 | + var range = window.getSelection().getRangeAt( 0 ); |
| 460 | + try { |
| 461 | + range.setEnd( range.endContainer, range.endOffset + 1 ) |
| 462 | + } catch ( e ) { |
| 463 | + return false; |
| 464 | + } |
| 465 | + var next_character = range.toString() |
| 466 | + range.setEnd( range.endContainer, range.endOffset - 1 ) |
| 467 | + if ( next_character != trigger ) |
| 468 | + return false; |
| 469 | + else { |
| 470 | + range.setEnd( range.endContainer, range.endOffset + 1 ) |
| 471 | + range.deleteContents(); |
| 472 | + return true; |
| 473 | + } |
| 474 | + } else if ( $.browser.msie ) { |
| 475 | + var range = document.selection.createRange(); |
| 476 | + try { |
| 477 | + range.moveEnd( 'character', 1 ) |
| 478 | + } catch ( e ) { |
| 479 | + return false; |
| 480 | + } |
| 481 | + var next_character = range.text |
| 482 | + range.moveEnd( 'character', -1 ) |
| 483 | + if ( next_character != trigger ) |
| 484 | + return false; |
| 485 | + else { |
| 486 | + range.moveEnd( 'character', 1 ) |
| 487 | + range.text = '' |
| 488 | + return true; |
| 489 | + } |
| 490 | + } |
| 491 | + }, |
| 492 | + |
| 493 | + shortcuts : function() { |
| 494 | + var cCode = arguments[0]; |
| 495 | + if ( cCode == 13 ) |
| 496 | + cCode = '[enter]'; |
| 497 | + else if ( cCode == 32 ) |
| 498 | + cCode = '[space]'; |
| 499 | + else |
| 500 | + cCode = '[' + String.fromCharCode( charCode ).toLowerCase() + ']'; |
| 501 | + for ( var i = 0; i < Language.shortcuts.length; i++ ) |
| 502 | + if ( Language.shortcuts[i].input == cCode ) |
| 503 | + this.insertCode( Language.shortcuts[i].output, false ); |
| 504 | + }, |
| 505 | + |
| 506 | + getRangeAndCaret : function() { |
| 507 | + if ( $.browser.msie ) { |
| 508 | + var range = document.selection.createRange(); |
| 509 | + var caret = Math.abs( range.moveStart( 'character', -1000000 ) + 1 ); |
| 510 | + range = this.getCode(); |
| 511 | + range = range.replace( /\n\r/gi, ' ' ); |
| 512 | + range = range.replace( /\n/gi, '' ); |
| 513 | + return [ range.toString(), caret ]; |
| 514 | + } else if ( !$.browser.msie ) { |
| 515 | + var range = window.getSelection().getRangeAt( 0 ); |
| 516 | + var range2 = range.cloneRange(); |
| 517 | + var node = range.endContainer; |
| 518 | + var caret = range.endOffset; |
| 519 | + range2.selectNode( node ); |
| 520 | + return [ range2.toString(), caret ]; |
| 521 | + } |
| 522 | + }, |
| 523 | + |
| 524 | + insertCode : function( code, replaceCursorBefore ) { |
| 525 | + if ( $.browser.msie ) { |
| 526 | + var repdeb = ''; |
| 527 | + var repfin = ''; |
| 528 | + |
| 529 | + if ( replaceCursorBefore ) { |
| 530 | + repfin = code; |
| 531 | + } else { |
| 532 | + repdeb = code; |
| 533 | + } |
| 534 | + |
| 535 | + if ( typeof document.selection != 'undefined' ) { |
| 536 | + var range = document.selection.createRange(); |
| 537 | + range.text = repdeb + repfin; |
| 538 | + range = document.selection.createRange(); |
| 539 | + range.move( 'character', -repfin.length ); |
| 540 | + range.select(); |
| 541 | + } |
| 542 | + } else if ( !$.browser.msie ) { |
| 543 | + var range = window.getSelection().getRangeAt( 0 ); |
| 544 | + var node = window.document.createTextNode( code ); |
| 545 | + var selct = window.getSelection(); |
| 546 | + var range2 = range.cloneRange(); |
| 547 | + // Insert text at cursor position |
| 548 | + selct.removeAllRanges(); |
| 549 | + range.deleteContents(); |
| 550 | + range.insertNode( node ); |
| 551 | + // Move the cursor to the end of text |
| 552 | + range2.selectNode( node ); |
| 553 | + range2.collapse( replaceCursorBefore ); |
| 554 | + selct.removeAllRanges(); |
| 555 | + selct.addRange( range2 ); |
| 556 | + } |
| 557 | + }, |
| 558 | + |
| 559 | + getCode : function() { |
| 560 | + if ( gecko && !document.getElementsByTagName( 'pre' )[0] || editor.innerHTML == '' ) |
| 561 | + editor = WikiEditorEngine.getEditor(); |
| 562 | + var code = editor.innerHTML; |
| 563 | + code = code.replace( /<br>/g, '\n' ); |
| 564 | + code = code.replace( /\u2009/g, '' ); |
| 565 | + code = code.replace( /<.*?>/g, '' ); |
| 566 | + code = code.replace( /</g, '<' ); |
| 567 | + code = code.replace( />/g, '>' ); |
| 568 | + code = code.replace( /&/gi, '&' ); |
| 569 | + |
| 570 | + if ( $.browser.msie ) { |
| 571 | + code = code.replace( /<\/p>/gi, '\r' ); |
| 572 | + code = code.replace( /<p>/i, '' ); // IE first line fix |
| 573 | + code = code.replace( /<p>/gi, '\n' ); |
| 574 | + code = code.replace( / /gi, '' ); |
| 575 | + } |
| 576 | + return code; |
| 577 | + }, |
| 578 | + |
| 579 | + setCode : function() { |
| 580 | + var code = arguments[0]; |
| 581 | + code = code.replace( /\u2009/gi, '' ); |
| 582 | + code = code.replace( /&/gi, '&' ); |
| 583 | + code = code.replace( /</g, '<' ); |
| 584 | + code = code.replace( />/g, '>' ); |
| 585 | + if ( $.browser.msie ) { |
| 586 | + editor.innerHTML = '<pre>' + code + '</pre>'; |
| 587 | + } else if ( !$.browser.msie ) { |
| 588 | + editor.innerHTML = code; |
| 589 | + } |
| 590 | + if ( gecko && code == '' ) { |
| 591 | + document.getElementsByTagName( 'body' )[0].innerHTML = ''; |
| 592 | + } |
| 593 | + }, |
| 594 | + |
| 595 | + // g |
| 596 | + actions : { |
| 597 | + pos : -1, // actual history position |
| 598 | + history : [], // history vector |
| 599 | + |
| 600 | + undo : function() { |
| 601 | + if ( gecko ) { |
| 602 | + editor = WikiEditorEngine.getEditor(); |
| 603 | + } |
| 604 | + if ( editor.innerHTML.indexOf( cc ) == -1 ) { |
| 605 | + if ( opera || ( gecko && editor.innerHTML != " " ) ) |
| 606 | + window.getSelection().getRangeAt( 0 ).insertNode( |
| 607 | + document.createTextNode( cc ) ); |
| 608 | + if ( $.browser.msie ) { |
| 609 | + document.selection.createRange().text = cc; |
| 610 | + } |
| 611 | + this.history[this.pos] = editor.innerHTML; |
| 612 | + } |
| 613 | + this.pos--; |
| 614 | + if ( typeof ( this.history[this.pos] ) == 'undefined' ) |
| 615 | + this.pos++; // in gecko it was pos ++ with a space. necessary? |
| 616 | + editor.innerHTML = this.history[this.pos]; |
| 617 | + if ( gecko && editor.innerHTML.indexOf( cc ) > -1 ) |
| 618 | + editor.innerHTML += cc; |
| 619 | + WikiEditorEngine.findString(); |
| 620 | + }, |
| 621 | + |
| 622 | + redo : function() { |
| 623 | + // editor = WikiEditorEngine.getEditor(); |
| 624 | + this.pos++; |
| 625 | + if ( typeof ( this.history[this.pos] ) == 'undefined' ) |
| 626 | + this.pos--; |
| 627 | + editor.innerHTML = this.history[this.pos]; |
| 628 | + WikiEditorEngine.findString(); |
| 629 | + }, |
| 630 | + |
| 631 | + next : function() { // get next vector position and clean old ones |
| 632 | + if ( this.pos > 20 ) |
| 633 | + this.history[this.pos - 21] = undefined; |
| 634 | + return ++this.pos; |
| 635 | + } |
| 636 | + } |
639 | 637 | } |
640 | 638 | // end WikiEditorEngine obj |
641 | 639 | |