1function parse_wikitext(id) {
2    if (ckgedit_dwedit_reject) {
3        var dom = GetE('ebut_cancel');
4        dom.click();
5        return true;
6    }
7    var useComplexTables = getComplexTables();
8
9
10    function fontConflict() {
11
12        var regex = /\>\s+(\*\*|__|\/\/|'')\s+_\s+\1\s+<\/font>/gm;
13        results = results.replace(regex, function(m) {
14            m = m.replace(/\s+/g, "");
15            return m;
16        });
17
18        regex = new RegExp("\\>(.*?)(\\]\\]\\<\\/font\\>)|(\\<\\/font\\>\\]\\])", "gm");
19        if (results.match(regex)) return true;
20
21        regex = new RegExp("(\\{\\{(.*?)\\.\\w{2,4})\\|\\<font");
22        if (results.match(regex)) return true;
23
24        regex = new RegExp("\\{\\{(.*?)\\.\\w{2,4}\\|[:\\w\\-\\.\\s]+\\<\\/font");
25        if (results.match(regex)) return true;
26
27        regex = new RegExp('\\>\\{\\{(.*?)\\.\\w+\\<\\/font\\>\\b', 'gm');
28        if (results.match(regex)) return true;
29
30        return false;
31
32    }
33    /**
34       table debugging code;
35    */
36    function check_rowspans(rows, start_row, ini) {
37        var tmp = new Array();
38        for (var i = start_row; i < rows.length; i++) {
39            for (var col = 0; col < rows[i].length; col++) {
40                if (rows[i][col].rowspan > 0) {
41                    var _text = rows[i][col].text;
42                    tmp.push({
43                        row: i,
44                        column: col,
45                        spans: rows[i][col].rowspan,
46                        text: _text
47                    });
48                    if (!ini) break;
49                }
50            }
51        }
52        return tmp;
53    }
54
55    function insert_rowspan(row, col, spans, rows, shift) {
56
57        var prev_colspans = rows[row][col].colspan ? rows[row][col].colspan : 0;
58        rows[row][col].rowspan = 0;
59        for (i = 0; i < spans - 1; i++) {
60            //debug_row(rows,row,col,"insert_rowspan start");
61            rows[++row].splice(col, 0, {
62                type: 'td',
63                rowspan: 0,
64                colspan: prev_colspans,
65                prev_colspan: prev_colspans,
66                text: " ::: "
67            });
68
69        }
70    }
71
72
73
74    function reorder_span_rows(rows) {
75        var tmp_start = check_rowspans(rows, 0, true);
76        var num_spans = tmp_start.length;
77        if (!num_spans) return false;
78
79
80        var row = tmp_start[0].row;
81        var col = tmp_start[0].column;
82        insert_rowspan(row, col, tmp_start[0].spans, rows);
83
84        num_spans--;
85        for (var i = 0; i < num_spans; i++) {
86            row++;
87            var tmp = check_rowspans(rows, row, false);
88            if (tmp.length) {
89                insert_rowspan(tmp[0].row, tmp[0].column, tmp[0].spans, rows);
90            }
91        }
92        return true;
93
94    }
95
96    function insert_table(rows) {
97        if (!useComplexTables) return;
98        for (var i = 0; i < rows.length; i++) {
99            if (!reorder_span_rows(rows)) break;;
100        }
101
102        results += "\n";
103        for (var i = 0; i < rows.length; i++) {
104            results += "\n";
105            for (var col = 0; col < rows[i].length; col++) {
106                var type = rows[i][col].type == 'td' ? '|' : '^';
107                results += type;
108                var align = rows[i][col].align ? rows[i][col].align : false;
109                if (align == 'center' || align == 'right') {
110                    results += "  ";
111                }
112
113                results += rows[i][col].text;
114                if (align == 'center' || align == 'left') {
115                    results += "  ";
116                }
117
118                if (rows[i][col].colspan) {
119                    for (var n = 0; n < rows[i][col].colspan - 1; n++) {
120                        results += type;
121                    }
122                }
123            }
124
125            results += '|';
126
127        }
128    }
129
130
131    window.dwfckTextChanged = false;
132    if (id != 'bakup') draft_delete();
133    var line_break = "\nL_BR_K  \n";
134    var markup = {
135        'b': '**',
136        'i': '//',
137        'em': '//',
138        'u': '__',
139        'br': line_break,
140        'strike': '<del>',
141        'del': '<del>',
142        's': '<del>',
143        p: "\n\n",
144        'a': '[[',
145        'img': '\{\{',
146        'strong': '**',
147        'h1': "\n====== ",
148        'h2': "\n===== ",
149        'h3': "\n==== ",
150        'h4': "\n=== ",
151        'h5': "\n== ",
152        'td': "|",
153        'th': "^",
154        'tr': " ",
155        'table': "\n\n",
156        'ol': "  - ",
157        'ul': "  * ",
158        'li': "",
159        'code': "\'\'",
160        'pre': "\n<",
161        'hr': "\n\n----\n\n",
162        'sub': '<sub>',
163        'font': "",
164        'blockquote': '<blockquote>',
165        'sup': '<sup>',
166        'div': "\n\n",
167        'span': "\n",
168        'dl': "\n",
169        'dd': "\n",
170        'dt': "\n"
171    };
172    var markup_end = {
173        'del': '</del>',
174        's': '</del>',
175        'strike': '</del>',
176        'p': " ",
177        'br': " ",
178        'a': ']]',
179        'img': '\}\}',
180        'h1': " ======\n",
181        'h2': " =====\n",
182        'h3': " ====\n",
183        'h4': " ===\n",
184        'h5': " ==\n",
185        'td': " ",
186        'th': " ",
187        'tr': "|\n",
188        'ol': " ",
189        'ul': " ",
190        'li': "\n",
191        'pre': "\n</",
192        'sub': '</sub>',
193        'sup': '</sup> ',
194        'div': "\n\n",
195        'p': "\n\n",
196        'font': "</font>",
197        'span': " ",
198        'blockquote': '</blockquote>',
199    };
200
201    markup['temp_u'] = "CKGE_TMP_u";
202    markup['temp_strong'] = "CKGE_TMP_strong";
203    markup['temp_em'] = "CKGE_TMP_em";
204    markup['temp_i'] = "CKGE_TMP_i";
205    markup['temp_b'] = "CKGE_TMP_b";
206    markup['temp_del'] = "CKGE_TMP_del";
207    markup['temp_strike'] = "CKGE_TMP_strike";
208    markup['temp_code'] = "CKGE_TMP_code";
209    markup['temp_sup'] = "CKGE_TMP_sup";
210    markup['temp_csup'] = "CKGE_TMP_csup";
211    markup['temp_sub'] = "CKGE_TMP_sub";
212    markup['temp_csub'] = "CKGE_TMP_csub";
213    markup['temp_del'] = "CKGE_TMP_del";
214    markup['temp_cdel'] = "CKGE_TMP_cdel";
215    markup['temp_strike'] = "CKGE_TMP_del";
216    markup['temp_cstrike'] = "CKGE_TMP_cdel";
217    markup['temp_s'] = "CKGE_TMP_del";
218    markup['temp_cs'] = "CKGE_TMP_cdel";
219
220    var $FORMAT_SUBST = {
221        'CKGE_TMP_b': '**',
222        'CKGE_TMP_strong': '**',
223        'CKGE_TMP_em': '\/\/',
224        'CKGE_TMP_u': '__',
225        'CKGE_TMP_sup': '<sup>',
226        'CKGE_TMP_sub': '<sub>',
227        'CKGE_TMP_cdel': '</del>',
228        'CKGE_TMP_csub': '</sub>',
229        'CKGE_TMP_csup': '</sup>',
230        'CKGE_TMP_del': '<del>',
231        'CKGE_TMP_strike': '<del>',
232        'CKGE_TMP_code': "\'\'"
233    };
234
235    markup['blank'] = "";
236    markup['fn_start'] = '((';
237    markup['fn_end'] = '))';
238    markup['row_span'] = ":::";
239    markup['p_insert'] = '_PARA__TABLE_INS_';
240    markup['format_space'] = '_FORMAT_SPACE_';
241    markup['pre_td'] = '<'; //removes newline from before < which corrupts table
242    var format_chars = {
243        'strong': true,
244        'b': true,
245        'i': true,
246        'em': true,
247        'u': true,
248        'del': true,
249        'strike': true,
250        'code': true,
251        'sup': true,
252        'sub': true,
253        's': true
254    };
255
256    var results = "";
257    var HTMLParser_LBR = false;
258    var HTMLParser_PRE = false;
259    var HTMLParser_Geshi = false;
260    var HTMLParser_TABLE = false;
261    var HTMLParser_COLSPAN = false;
262    var HTMLParser_FORMAT_SPACE = false;
263    var HTMLParser_MULTI_LINE_PLUGIN = false;
264    var HTMLParser_NOWIKI = false;
265    var HTMLFormatInList = false;
266    var HTMLAcroInList = false;
267    var HTML_InterWiki = false;
268    var HTMLParserFont = false;
269    HTMLLinkInList = false;
270    var HTMLParserFontInfix = false;
271    var CurrentTable;
272
273    var HTMLParserTopNotes = new Array();
274    var HTMLParserBottomNotes = new Array();
275    var HTMLParserOpenAngleBracket = false;
276    var HTMLParserParaInsert = markup['p_insert'];
277
278    var geshi_classes = '(br|co|coMULTI|es|kw|me|nu|re|st|sy)[0-9]';
279    String.prototype.splice = function(idx, rem, s) {
280        return (this.slice(0, idx) + s + this.slice(idx + Math.abs(rem)));
281    };
282    String.frasl = new RegExp("⁄\|&frasl;\|&#8260;\|&#x2044;", 'g');
283    geshi_classes = new RegExp(geshi_classes);
284    HTMLParser(CKEDITOR.instances.wiki__text.getData(), {
285        attribute: "",
286        link_title: "",
287        link_class: "",
288        image_link_type: "",
289        td_align: "",
290        in_td: false,
291        td_colspan: 0,
292        td_rowspan: 0,
293        rowspan_col: 0,
294        last_column: -1,
295        row: 0,
296        col: 0,
297        td_no: 0,
298        tr_no: 0,
299        current_row: false,
300        in_table: false,
301        in_multi_plugin: false,
302        is_rowspan: false,
303        list_level: 0,
304        prev_list_level: -1,
305        list_started: false,
306        xcl_markup: false,
307        in_link: false,
308        link_formats: new Array(),
309        last_tag: "",
310        code_type: false,
311        in_endnotes: false,
312        is_smiley: false,
313        geshi: false,
314        downloadable_code: false,
315        export_code: false,
316        code_snippet: false,
317        downloadable_file: "",
318        external_mime: false,
319        in_header: false,
320        curid: false,
321        format_in_list: false,
322        prev_li: new Array(),
323        link_only: false,
324        in_font: false,
325        using_fonts: false,
326        interwiki: false,
327        bottom_url: false,
328        font_family: "inherit",
329        font_size: "inherit",
330        font_weight: "inherit",
331        font_color: "inherit",
332        font_bgcolor: "inherit",
333        font_style: "inherit",
334        is_mediafile: false,
335        end_nested: false,
336        mfile: false,
337
338        backup: function(c1, c2) {
339            var c1_inx = results.lastIndexOf(c1); // start position of chars to delete
340            var c2_inx = results.indexOf(c2, c1_inx); // position of expected next character
341            if (c1_inx == -1 || c2_inx == -1) return;
342            if (c1.length + c2_inx == c2_inx) {
343                var left_side = results.substring(0, c1_inx); //from 0 up to but not including c1
344                var right_side = results.substring(c2_inx); //from c2 to end of string
345                results = left_side + right_side;
346                return true;
347            }
348            return false;
349        },
350        is_iwiki: function(class_name, title) {
351            var iw_type = class_name.match(/iw_(\w+\.?\w{0,12})/);
352            var iw_title = title.split(/\/\//);
353            var interwiki_label = iw_title[iw_title.length - 1];
354            if (!interwiki_label.match(/oIWIKIo.*?cIWIKIc/)) {
355                interwiki_label = 'oIWIKIo' + interwiki_label + 'cIWIKIc';
356            }
357            interwiki_label = interwiki_label.replace(/^.*?oIWIKIo/, 'oIWIKIo');
358            interwiki_label = interwiki_label.replace(/cIWIKIc.*/, 'cIWIKIc');
359            iw_type[1] = iw_type[1].replace(/_(\w{2})/g, "." + "$1");
360            this.attr = iw_type[1] + '>' + decodeURIComponent(interwiki_label);
361            HTML_InterWiki = true;
362            this.interwiki = true;
363        },
364        start: function(tag, attrs, unary) {
365            /**   if table debugging code:
366            this_debug = this.dbg;
367            */
368            if (markup[tag]) {
369                if (format_chars[tag] && this.in_link) {
370                    this.link_formats.push(tag);
371                    return;
372                }
373                if (format_chars[tag] && (this.in_font || this.in_header)) {
374                    results += " ";
375                    var t = 'temp_' + tag;
376                    results += markup[t];
377                    results += " ";
378                    return;
379                } else if (tag == 'acronym') {
380                    return;
381                }
382                if (format_chars[tag] && this.in_endnotes) {
383                    if (tag == 'sup') return;
384                }
385                if (tag == 'ol' || tag == 'ul') {
386                    this.prev_list_level = this.list_level;
387                    this.list_level++;
388                    if (this.list_level == 1) this.list_started = false;
389                    if (this.list_started) this.prev_li.push(markup['li']);
390                    markup['li'] = markup[tag];
391
392                    return;
393                } else if (!this.list_level) {
394                    markup['li'] = "";
395                    this.prev_li = new Array();
396                }
397
398                this.is_mediafile = false;
399
400                if (tag == 'img') {
401                    var img_size = "?";
402                    var width;
403                    var height;
404                    var style = false;
405                    var img_align = '';
406                    var alt = "";
407                    var from_clipboard = false;
408                    this.is_smiley = false;
409                    this.in_link = false;
410                }
411
412                if (tag == 'a') {
413                    var local_image = true;
414                    var type = "";
415                    this.xcl_markup = false; // set to false in end() as well, double sure
416                    this.in_link = true;
417                    this.link_pos = results.length;
418                    this.link_formats = new Array();
419                    this.footnote = false;
420                    var bottom_note = false;
421                    this.id = "";
422                    this.external_mime = false;
423                    var media_class = false;
424                    this.export_code = false;
425                    this.code_snippet = false;
426                    this.downloadable_file = "";
427                    var qs_set = false;
428                    this.link_only = false;
429                    save_url = "";
430                    this.interwiki = false;
431                    this.bottom_url = false;
432                    this.link_title = false;
433                    var interwiki_title = "";
434                    var interwiki_class = "";
435                }
436
437                if (tag == 'p') {
438                    this.in_link = false;
439                    if (this.in_table) {
440                        tag = 'p_insert';
441                        HTMLParser_TABLE = true;
442                    }
443                }
444
445
446                if (tag == 'table') {
447                    this.td_no = 0;
448                    this.tr_no = 0;
449                    this.in_table = true;
450                    this.is_rowspan = false;
451                    this.row = -1;
452                    this.rows = new Array();
453                    CurrentTable = this.rows;
454                    this.table_start = results.length;
455                } else if (tag == 'tr') {
456                    this.tr_no++;
457                    this.td_no = 0;
458                    this.col = -1;
459                    this.row++;
460                    this.rows[this.row] = new Array();
461                    this.current_row = this.rows[this.row];
462                } else if (tag == 'td' || tag == 'th') {
463                    this.td_no++;
464                    this.col++;
465                    this.current_row[this.col] = {
466                        type: tag,
467                        rowspan: 0,
468                        colspan: 0,
469                        text: ""
470                    };
471                    this.cell_start = results.length;
472                    this.current_cell = this.current_row[this.col];
473                    if (this.td_rowspan && this.rowspan_col == this.td_no && this.td_no != this.last_column) {
474                        this.is_rowspan = true;
475                        this.td_rowspan--;
476                    } else {
477                        this.is_rowspan = false;
478                    }
479
480
481                }
482
483
484                var matches;
485                this.attr = false;
486                this.format_tag = false;
487
488                if (format_chars[tag]) this.format_tag = true;
489                var dwfck_note = false;
490
491                for (var i = 0; i < attrs.length; i++) {
492
493                    // if(!confirm(tag + ' ' + attrs[i].name + '="' + attrs[i].escaped + '"')) exit;
494                    if (tag == 'td' || tag == 'th') {
495                        if (attrs[i].name == 'colspan') {
496                            this.current_row[this.col].colspan = attrs[i].value;
497                        }
498                        if (attrs[i].name == 'class') {
499                            if ((matches = attrs[i].value.match(/(left|center|right)/))) {
500                                this.current_row[this.col].align = matches[1];
501                            }
502                        }
503                        if (attrs[i].name == 'rowspan') {
504                            this.current_row[this.col].rowspan = attrs[i].value
505                        }
506                    }
507                    if (attrs[i].escaped == 'u' && tag == 'em') {
508                        tag = 'u';
509                        this.attr = 'u'
510                        break;
511                    }
512
513                    if (tag == 'div') {
514                        if (attrs[i].name == 'class' && attrs[i].value == 'footnotes') {
515                            tag = 'blank';
516                            this.in_endnotes = true;
517                        }
518                        break;
519                    }
520                    if (tag == 'dl' && attrs[i].name == 'class' && attrs[i].value == 'file') {
521                        this.downloadable_code = true;
522                        HTMLParser_Geshi = true;
523                        return;
524                    }
525                    if (tag == 'span' && attrs[i].name == 'class') {
526                        if (attrs[i].value == 'np_break') return;
527                    }
528
529                    if (tag == 'span' && attrs[i].name == 'class') {
530                        if (attrs[i].value == 'curid') {
531                            this.curid = true;
532                            return;
533                        }
534                        if (attrs[i].value == 'multi_p_open') {
535                            this.in_multi_plugin = true;
536                            HTMLParser_MULTI_LINE_PLUGIN = true;
537                            return;
538                        }
539                        if (attrs[i].value == 'multi_p_close') {
540                            this.in_multi_plugin = false;
541                            return;
542                        }
543                        if (attrs[i].value.match(geshi_classes)) {
544                            tag = 'blank';
545                            this.geshi = true;
546                            break;
547                        }
548                    }
549
550
551                    if (tag == 'span' && !ckgedit_xcl_styles) {
552                        if (attrs[i].name == 'style') {
553                            if (!this.in_font) results += "__STYLE__";
554                            this.in_font = true;
555                            this.using_fonts = true;
556                            matches = attrs[i].value.match(/font-family:\s*([\w\-\s,]+);?/);
557                            if (matches) {
558                                this.font_family = matches[1];
559                            }
560
561                            //matches = attrs[i].value.match(/font-size:\s*(\d+(\w+|%));?/);
562                            matches = attrs[i].value.match(/font-size:\s*(.*)/);
563                            if (matches) {
564                                matches[1] = matches[1].replace(/;/, "");
565                                this.font_size = matches[1];
566                            }
567                            matches = attrs[i].value.match(/font-weight:\s*(\w+);?/);
568                            if (matches) {
569                                this.font_weight = matches[1];
570                            }
571                            matches = attrs[i].value.match(/.*?color:\s*(.*)/);
572                            var bgcolor_found = false;
573                            if (matches) {
574                                matches[1] = matches[1].replace(/;/, "");
575                                if (matches[0].match(/background/)) {
576                                    this.font_bgcolor = matches[1];
577                                } else {
578                                    this.font_color = matches[1];
579                                }
580                            }
581                            if (!bgcolor_found) { //catch MS Word which uses background:color-name instead of background-color:color-name
582                                matches = attrs[i].value.match(/background:\s*(\w+)/);
583                                if (matches && matches[0].match(/background/)) {
584                                    this.font_bgcolor = matches[1];
585                                }
586
587                            }
588                        }
589
590                    }
591                    if (tag == 'td' || tag == 'th') {
592                        if (tag == 'td') {
593                            results = results.replace(/\^$/, '|');
594                        }
595                        this.in_td = true;
596                        if (attrs[i].name == 'align') {
597                            this.td_align = attrs[i].escaped;
598
599                        } else if (attrs[i].name == 'class') {
600                            matches = attrs[i].value.match(/\s*(\w+)align/);
601                            if (matches) {
602                                this.td_align = matches[1];
603                            }
604                        } else if (attrs[i].name == 'colspan') {
605                            HTMLParser_COLSPAN = true;
606                            this.td_colspan = attrs[i].escaped;
607                        } else if (attrs[i].name == 'rowspan') {
608                            this.td_rowspan = attrs[i].escaped - 1;
609                            this.rowspan_col = this.td_no;
610                        }
611
612                        HTMLParser_TABLE = true;
613                    }
614
615                    if (tag == 'a') {
616
617                        //             if(!confirm(attrs[i].name + '="' + attrs[i].escaped + '"')) exit;
618                        if (attrs[i].name == 'title') {
619                            this.link_title = attrs[i].escaped;
620                            if (interwiki_class) {
621                                interwiki_title = attrs[i].escaped;
622                            } else this.link_title = this.link_title.replace(/\s+.*$/, "");
623                        } else if (attrs[i].name == 'class') {
624                            if (attrs[i].value.match(/fn_top/)) {
625                                this.footnote = true;
626                            } else if (attrs[i].value.match(/fn_bot/)) {
627                                bottom_note = true;
628                            } else if (attrs[i].value.match(/mf_(png|gif|jpg|jpeg)/i)) {
629                                this.link_only = true;
630                            } else if (attrs[i].value.match(/interwiki/)) {
631                                attrs[i].value = attrs[i].value.replace(/\./g, '_');
632                                this.link_class = attrs[i].value;
633                                continue;
634                            }
635
636                            this.link_class = attrs[i].escaped;
637                            media_class = this.link_class.match(/mediafile/);
638                        } else if (attrs[i].name == 'id') {
639                            this.id = attrs[i].value;
640                        } else if (attrs[i].name == 'type') {
641                            type = attrs[i].value;
642                        } else if (attrs[i].name == 'href' && !this.code_type) {
643                            var http = attrs[i].escaped.match(/https*:\/\//) ? true : false;
644                            if (http) save_url = attrs[i].escaped;
645                            if (attrs[i].escaped.match(/\/lib\/exe\/detail.php/)) {
646                                this.image_link_type = 'detail';
647                            } else if (attrs[i].escaped.match(/exe\/fetch.php/)) {
648                                this.image_link_type = 'direct';
649                            }
650
651                            if (this.link_class && this.link_class.match(/media/) && !this.link_title) {
652                                var link_find = attrs[i].escaped.match(/media=(.*)/);
653                                if (link_find) this.link_title = link_find[1];
654                            }
655                            // required to distinguish external images from external mime types
656                            // that are on the wiki which also use {{url}}
657                            var media_type = attrs[i].escaped.match(/fetch\.php.*?media=.*?\.(png|gif|jpg|jpeg)$/i);
658                            if (media_type) media_type = media_type[1];
659
660                            if (attrs[i].escaped.match(/^https*:/)) {
661                                this.attr = attrs[i].escaped;
662                                local_image = false;
663                            }
664                            if (attrs[i].escaped.match(/^ftp:/)) {
665                                this.attr = attrs[i].escaped;
666                                local_image = false;
667                            } else if (attrs[i].escaped.match(/do=export_code/)) {
668                                this.export_code = true;
669                            } else if (attrs[i].escaped.match(/^nntp:/)) {
670                                this.attr = attrs[i].escaped;
671                                local_image = false;
672                            } else if (attrs[i].escaped.match(/^mailto:/)) {
673                                this.attr = attrs[i].escaped.replace(/mailto:/, "");
674                                local_image = false;
675                            } else if (attrs[i].escaped.match(/m-files/)) {
676                                this.attr = attrs[i].escaped;
677                                this.mfile = attrs[i].escaped;
678                                local_image = false;
679                            } else if (attrs[i].escaped.match(/^file:/)) { //samba share
680                                var url = attrs[i].value.replace(/file:[\/]+/, "");
681                                url = url.replace(/[\/]/g, '\\');
682                                url = '\\\\' + url;
683                                this.attr = url;
684                                local_image = false;
685                            }
686                            // external mime types after they've been saved first time
687                            else if (http && !media_type && (matches = attrs[i].escaped.match(/fetch\.php(.*)/))) {
688                                if (matches[1].match(/media=/)) {
689                                    elems = matches[1].split(/=/);
690                                    this.attr = elems[1];
691                                } else { // nice urls
692                                    matches[1] = matches[1].replace(/^\//, "");
693                                    this.attr = matches[1];
694                                }
695
696                                if (typeof config_animal !== 'undefined') {
697                                    var regex = new RegExp(config_animal + '\/file\/(.*)');
698                                    matches = attrs[i].escaped.match(regex);
699                                    if (matches && matches[1]) this.attr = matches[1];
700                                    if (this.attr) this.attr = this.attr.replace(/\//g, ':');
701                                }
702                                local_image = false;
703
704                                this.attr = decodeURIComponent ? decodeURIComponent(this.attr) : unescape(this.attr);
705                                if (!this.attr.match(/^:/)) {
706                                    this.attr = ':' + this.attr;
707                                }
708                                this.external_mime = true;
709                            } else {
710                                local_image = false;
711
712                                matches = attrs[i].escaped.match(/doku.php\?id=(.*)/);
713                                if (matches && save_url) {
714                                    var rx = DOKU_BASE + 'doku.php';
715                                    if (!attrs[i].escaped.match(rx)) {
716                                        this.link_class == 'urlextern';
717                                        this.attr = save_url;
718                                        matches = null;
719                                    }
720                                }
721                                if (!matches) {
722                                    matches = attrs[i].escaped.match(/doku.php\/(.*)/);
723                                }
724                                /* previously saved internal link with query string
725                                  requires initial ? to be recognized by DW. In Anteater and later */
726                                if (matches) {
727                                    if (!matches[1].match(/\?/) && matches[1].match(/&amp;/)) {
728                                        qs_set = true;
729                                        matches[1] = matches[1].replace(/&amp;/, '?')
730                                    }
731                                }
732                                if (matches && matches[1]) {
733                                    if (!matches[1].match(/^:/)) {
734                                        this.attr = ':' + matches[1];
735                                    } else {
736                                        this.attr = matches[1];
737                                    }
738
739                                    if (this.attr.match(/\.\w+$/)) { // external mime's first access
740                                        if (type && type == 'other_mime') {
741                                            this.external_mime = true;
742                                        } else {
743                                            for (var n = i + 1; n < attrs.length; n++) {
744                                                if (attrs[n].value.match(/other_mime/))
745                                                    this.external_mime = true;
746                                                break;
747                                            }
748
749                                        }
750                                    }
751
752                                } else {
753                                    matches = attrs[i].value.match(/\\\\/); // Windows share
754                                    if (matches) {
755                                        this.attr = attrs[i].escaped;
756                                        local_image = false;
757                                    }
758                                }
759                            }
760
761                            if (this.link_class == 'media') {
762                                if (attrs[i].value.match(/http:/)) {
763                                    local_image = false;
764                                }
765                            }
766
767                            if (!this.attr && this.link_title) {
768
769                                if (matches = this.link_class.match(/media(.*)/)) {
770                                    this.link_title = decodeURIComponent(safe_convert(this.link_title));
771                                    this.attr = this.link_title;
772                                    var m = matches[1].split(/_/);
773                                    if (m && m[1]) {
774                                        media_type = m[1];
775                                    } else if (m) {
776                                        media_type = m[0];
777                                    } else media_type = 'mf';
778                                    if (!this.attr.match(/^:/) && !this.attr.match(/^https?\:/)) {
779                                        this.attr = ':' + this.attr.replace(/^\s+/, "");
780                                    }
781                                    this.external_mime = true;
782                                    local_image = false;
783                                }
784                            }
785
786                            if (this.attr.match && this.attr.match(/%[a-fA-F0-9]{2}/) && (matches = this.attr.match(/userfiles\/file\/(.*)/))) {
787                                matches[1] = matches[1].replace(/\//g, ':');
788                                if (!matches[1].match(/^:/)) {
789                                    matches[1] = ':' + matches[1];
790                                }
791                                this.attr = decodeURIComponent ? decodeURIComponent(matches[1]) : unescape(matches[1]);
792                                this.attr = decodeURIComponent ? decodeURIComponent(this.attr) : unescape(this.attr);
793                                this.external_mime = true;
794
795                            } else if (this.attr && this.attr.match(/%[a-fA-F0-9]{2}/)) {
796                                this.attr = decodeURIComponent(this.attr);
797                                this.attr = decodeURIComponent(this.attr);
798                            }
799
800                            // alert('title: ' + this.link_title + '  class: ' + this.link_class + ' export: ' +this.export_code);
801                            if (this.link_title && this.link_title.match(/Snippet/)) this.code_snippet = true;
802
803                            /* anchors to current page without prefixing namespace:page */
804                            if (attrs[i].value.match(/^#/) && this.link_class.match(/wikilink/)) {
805                                this.attr = attrs[i].value;
806                                this.link_title = false;
807                            }
808
809                            /* These two conditions catch user_rewrite not caught above */
810                            if (this.link_class.match(/wikilink/) && this.link_title) {
811                                this.external_mime = false;
812                                if (!this.attr) {
813                                    this.attr = this.link_title;
814                                }
815                                if (!this.attr.match(/^:/)) {
816                                    this.attr = ':' + this.attr;
817                                }
818                                if (this.attr.match(/\?.*?=/)) {
819                                    var elems = this.attr.split(/\?/);
820                                    elems[0] = elems[0].replace(/\//g, ':');
821                                    this.attr = elems[0] + '?' + elems[1];
822                                } else {
823                                    this.attr = this.attr.replace(/\//g, ':');
824                                }
825
826                                /* catch query strings attached to internal links for .htacess nice urls  */
827                                if (!qs_set && attrs[i].name == 'href') {
828                                    if (!this.attr.match(/\?.*?=/) && !attrs[i].value.match(/doku.php/)) {
829                                        var qs = attrs[i].value.match(/(\?.*)$/);
830                                        if (qs && qs[1]) this.attr += qs[1];
831                                    }
832
833                                }
834                            } else if (this.link_class.match(/mediafile/) && this.link_title && !this.attr) {
835                                this.attr = this.link_title;
836                                this.external_mime = true;
837
838                                if (!this.attr.match(/^:/)) {
839                                    this.attr = ':' + this.attr;
840                                }
841                            } else if (this.link_class.match(/interwiki/)) {
842                                interwiki_class = this.link_class;
843                            }
844
845                            if (this.link_class == 'urlextern' && !this.mfile && save_url) {
846                                this.attr = save_url;
847                                this.external_mime = false; // prevents external links to images from being converted to image links
848                            }
849                            if (this.in_endnotes) {
850                                if (this.link_title) {
851                                    this.bottom_url = this.link_title; //save for bottom urls
852                                } else if (this.attr) {
853                                    this.bottom_url = this.attr;
854                                }
855                            }
856                            this.link_title = "";
857                            this.link_class = "";
858                            //  break;
859                        }
860                    }
861
862                    if (interwiki_class && interwiki_title) {
863                        this.is_iwiki(interwiki_class, interwiki_title);
864                        interwiki_class = "";
865                        interwiki_title = "";
866                    }
867
868                    if (tag == 'sup') {
869                        if (attrs[i].name == 'class') {
870                            matches = attrs[i].value.split(/\s+/);
871                            if (matches[0] == 'dwfcknote') {
872                                this.attr = matches[0];
873                                tag = 'blank';
874                                if (oDokuWiki_FCKEditorInstance.oinsertHtmlCodeObj.notes[matches[1]]) {
875                                    dwfck_note = '((' + oDokuWiki_FCKEditorInstance.oinsertHtmlCodeObj.notes[matches[1]] + '))';
876                                }
877                                break;
878                            }
879                        }
880                    }
881
882                    if (tag == 'pre') {
883                        if (attrs[i].name == 'class') {
884
885                            var elems = attrs[i].escaped.split(/\s+/);
886                            if (elems.length > 1) {
887                                this.attr = attrs[i].value;
888                                this.code_type = elems[0];
889                            } else {
890                                this.attr = attrs[i].escaped;
891                                this.code_type = this.attr;
892                            }
893                            if (this.downloadable_code) {
894                                this.attr = this.attr.replace(/\s*code\s*/, "");
895                                this.code_type = 'file';
896                            }
897                            HTMLParser_PRE = true;
898                            if (this.in_table) tag = 'pre_td';
899                            break;
900                        }
901
902                    } else if (tag == 'img') {
903                        if (attrs[i].name == 'alt') {
904                            alt = attrs[i].value;
905                        }
906                        if (attrs[i].name == 'type') {
907                            this.image_link_type = attrs[i].value;
908                        }
909
910                        if (attrs[i].name == 'src') {
911                            //  alert(attrs[i].name + ' = ' + attrs[i].value + ',  fnencode=' + oDokuWiki_FCKEditorInstance.dwiki_fnencode);
912
913                            var src = "";
914                            // fetched by fetch.php
915                            if (matches = attrs[i].escaped.match(/fetch\.php.*?(media=.*)/)) {
916                                var elems = matches[1].split('=');
917                                src = elems[1];
918                                if (matches = attrs[i].escaped.match(/(media.*)/)) {
919                                    var elems = matches[1].split('=');
920                                    var uri = elems[1];
921                                    src = decodeURIComponent ? decodeURIComponent(uri) : unescape(uri);
922                                }
923                                if (!src.match(/https?:/) && !src.match(/^:/)) src = ':' + src;
924                            } else if (attrs[i].escaped.match(/https?:\/\//)) {
925                                src = attrs[i].escaped;
926                                src = src.replace(/\?.*?$/, "");
927                            }
928                            // url rewrite 1
929                            else if (matches = attrs[i].escaped.match(/\/_media\/(.*)/)) {
930                                var elems = matches[1].split(/\?/);
931                                src = elems[0];
932                                src = src.replace(/\//g, ':');
933                                if (!src.match(/^:/)) src = ':' + src;
934                            }
935                            // url rewrite 2
936                            else if (matches = attrs[i].escaped.match(/\/lib\/exe\/fetch.php\/(.*)/)) {
937                                var elems = matches[1].split(/\?/);
938                                src = elems[0];
939                                if (!src.match(/^:/)) src = ':' + src;
940                            } else {
941                                // first insertion from media mananger
942                                matches = attrs[i].escaped.match(/^.*?\/userfiles\/image\/(.*)/);
943                                if (!matches && typeof config_animal !== 'undefined') {
944                                    var regex = new RegExp(config_animal + '\/image\/(.*)$');
945                                    matches = attrs[i].escaped.match(regex);
946                                }
947                                if (!matches) { // windows style
948                                    var regex = doku_base + 'data/media/';
949                                    regex = regex.replace(/([\/\\])/g, "\\$1");
950                                    regex = '^.*?' + regex + '(.*)';
951                                    regex = new RegExp(regex);
952                                    matches = attrs[i].escaped.match(regex);
953                                }
954                                if (matches && matches[1]) {
955                                    src = matches[1].replace(/\//g, ':');
956                                    src = ':' + src;
957                                } else {
958                                    src = decodeURIComponent ? decodeURIComponent(attrs[i].escaped) : unescape(attrs[i].escaped);
959                                    if (src.search(/data:image.*?;base64/) > -1) {
960                                        from_clipboard = true;
961                                    }
962                                }
963
964                            }
965                            if (src && src.match(/lib\/images\/smileys/)) {
966                                // src = 'http://' + window.location.host + src;
967                                this.is_smiley = true;
968                            }
969                            this.attr = src;
970                            if (this.attr && this.attr.match && this.attr.match(/%[a-fA-F0-9]{2}/)) {
971                                this.attr = decodeURIComponent(safe_convert(this.attr));
972                                this.attr = decodeURIComponent(safe_convert(this.attr));
973                            }
974
975
976
977                        } // src end
978                        else if (attrs[i].name == 'width' && !style) {
979                            width = attrs[i].value;
980
981                        } else if (attrs[i].name == 'height' && !style) {
982                            height = attrs[i].value;
983                        } else if (attrs[i].name == 'style') {
984                            var match = attrs[i].escaped.match(/width:\s*(\d+)/);
985                            if (match) {
986                                width = match[1];
987                                var match = attrs[i].escaped.match(/height:\s*(\d+)/);
988                                if (match) height = match[1];
989                            }
990                        } else if (attrs[i].name == 'align' || attrs[i].name == 'class') {
991                            if (attrs[i].escaped.match(/(center|middle)/)) {
992                                img_align = 'center';
993                            } else if (attrs[i].escaped.match(/right/)) {
994                                img_align = 'right';
995                            } else if (attrs[i].escaped.match(/left/)) {
996                                img_align = 'left';
997                            } else {
998                                img_align = '';
999                            }
1000                        }
1001                    } // End img
1002                } // End Attributes Loop
1003
1004                if (this.is_smiley) {
1005                    if (alt) {
1006                        results += alt + ' ';
1007                        alt = "";
1008                    }
1009                    this.is_smiley = false;
1010                    return;
1011                }
1012                if (this.link_only) tag = 'img';
1013                if (tag == 'br') {
1014                    if (this.in_multi_plugin) {
1015                        results += "\n";
1016                        return;
1017                    }
1018
1019                    if (!this.code_type) {
1020                        HTMLParser_LBR = true;
1021                    } else if (this.code_type) {
1022                        results += "\n";
1023                        return;
1024                    }
1025
1026                    if (this.in_table) {
1027                        results += HTMLParserParaInsert;
1028                        return;
1029                    }
1030                    if (this.list_started) {
1031                        results += '_LIST_EOFL_'; /* enables newlines in lists:   abc \\def */
1032                    } else {
1033                        results += '\\\\  ';
1034                        return;
1035                    }
1036                } else if (tag.match(/^h(\d+|r)/)) {
1037                    var str_len = results.length;
1038                    if (tag.match(/h(\d+)/)) {
1039                        this.in_header = true;
1040                    }
1041                    if (str_len) {
1042                        if (results.charCodeAt(str_len - 1) == 32) {
1043                            results = results.replace(/\x20+$/, "");
1044                        }
1045                    }
1046                } else if (this.last_col_pipes) {
1047                    if (format_chars[tag]) results += markup[tag];
1048                    tag = 'blank';
1049                } else if (dwfck_note) {
1050                    results += dwfck_note;
1051                    return;
1052                }
1053
1054                if (tag == 'b' || tag == 'i' && this.list_level) {
1055                    if (results.match(/(\/\/|\*)(\x20)+/)) {
1056                        results = results.replace(/(\/\/|\*)(\x20+)\-/, "$1\n" + "$2-");
1057                    }
1058                }
1059
1060                if (tag == 'li' && this.list_level) {
1061                    if (this.list_level == 1 & !this.list_started) {
1062                        results += "\n";
1063                        this.list_started = true;
1064                    }
1065                    results = results.replace(/[\x20]+$/, "");
1066
1067                    for (var s = 0; s < this.list_level; s++) {
1068                        // this handles format characters at the ends of list lines
1069                        if (results.match(/_FORMAT_SPACE_\s*$/)) {
1070                            results = results.replace(/_FORMAT_SPACE_\s*$/, "\n");
1071                        }
1072                        if (this.list_level > 1) {
1073                            results += '  ';
1074                        }
1075                    }
1076
1077                    if (this.prev_list_level > 0 && markup['li'] == markup['ol']) {
1078                        this.prev_list_level = -1;
1079                    }
1080                }
1081                if (tag == 'a' && this.list_level) {
1082                    HTMLLinkInList = true;
1083                }
1084                if (tag == 'a' && local_image) {
1085                    this.xcl_markup = true;
1086                    return;
1087                } else if (tag == 'a' && (this.export_code || this.code_snippet)) {
1088                    return;
1089                } else if (tag == 'a' && this.footnote) {
1090                    tag = 'fn_start';
1091                } else if (tag == 'a' && bottom_note) {
1092                    HTMLParserTopNotes.push(this.id);
1093                } else if (tag == 'a' && this.external_mime) {
1094                    if (this.in_endnotes) {
1095                        this.link_class = 'media';
1096                        return;
1097                    }
1098
1099                    if (media_class && media_class == 'mediafile') {
1100                        results += markup['img'];
1101                        results += this.attr + '|';
1102                        this.is_mediafile = true;
1103                    }
1104
1105                    return;
1106                } else if (this.in_font) {
1107                    if (tag == 'a') {
1108                        results = results.replace(/__STYLE__/, '[[' + this.attr + '|');
1109                        this.in_font = false;
1110                    }
1111                    return;
1112                    /* <font 18pt:bold,italic/garamond;;color;;background_color>  */
1113                }
1114
1115                if (this.in_endnotes && tag == 'a') return;
1116                if (this.code_type && tag == 'span') tag = 'blank';
1117                if (this.mfile && !this.attr) {
1118                    this.attr = this.mfile;
1119                }
1120                results += markup[tag]; // Set tag
1121
1122                if (tag == 'td' || tag == 'th' || (this.last_col_pipes && this.td_align == 'center')) {
1123                    if (this.is_rowspan) {
1124                        results += markup['row_span'] + ' | ';
1125                        this.is_rowspan = false;
1126                    }
1127                    if (this.td_align == 'center' || this.td_align == 'right') {
1128                        results += '  ';
1129                    }
1130
1131                } else if (tag == 'a' && this.attr) {
1132                    this.attr = this.attr.replace(/%7c/, '%257c');
1133                    results += this.attr + '|';
1134                } else if (tag == 'img') {
1135                    var link_type = this.image_link_type;
1136                    this.image_link_type = "";
1137                    if (this.link_only) link_type = 'link_only';
1138                    if (!link_type || from_clipboard) {
1139                        link_type = 'nolink';
1140                    } else if (link_type == 'detail') {
1141                        link_type = "";
1142                    }
1143
1144                    if (link_type == 'link_only') {
1145                        img_size = '?linkonly';
1146                    } else if (link_type) {
1147                        img_size += link_type + '&';
1148                    }
1149                    if (width && height) {
1150                        img_size += width + 'x' + height;
1151                    } else if (width) {
1152                        img_size += width;
1153                    } else if (!link_type) {
1154                        img_size = "";
1155                    }
1156                    if (img_align && img_align != 'left') {
1157                        results += '  ';
1158                    }
1159                    this.attr += img_size;
1160                    if (img_align == 'center' || img_align == 'left') {
1161                        this.attr += '  ';
1162                    }
1163                    if (alt) {
1164                        results += this.attr + '|' + alt + '}}';
1165                    } else results += this.attr + '}}';
1166                    this.attr = 'src';
1167                } else if (tag == 'pre' || tag == 'pre_td') {
1168                    if (this.downloadable_file) this.attr += ' ' + this.downloadable_file;
1169                    if (!this.attr) this.attr = 'code';
1170                    results += this.attr + '>';
1171                    this.downloadable_file = "";
1172                    this.downloadable_code = false;
1173                }
1174
1175            } // if markup tag
1176        },
1177
1178        end: function(tag) {
1179
1180            if (format_chars[tag] && (this.in_font || this.in_header)) {
1181                results += " ";
1182                if (tag == 'sup' || tag == 'sub' || tag == 'del' || tag == 'strike' || tag == 's') {
1183                    var t = 'temp_c' + tag;
1184                } else var t = 'temp_' + tag;
1185                results += markup[t];
1186                results += " ";
1187                return;
1188            }
1189            if (this.in_endnotes && tag == 'a') return;
1190            if (this.in_link && format_chars[current_tag] && this.link_formats.length) {
1191                return;
1192            } else if (tag == 'a' && !this.link_formats.length) this.in_link = false;
1193
1194            if (this.link_only) {
1195                this.link_only = false;
1196                return;
1197            }
1198
1199            if (!markup[tag]) return;
1200
1201            if (tag == 'sup' && this.attr == 'dwfcknote') {
1202                return;
1203            }
1204            if (this.is_smiley) {
1205                this.is_smiley = false;
1206                if (tag != 'li') return;
1207            }
1208            if (tag == 'span' && this.in_font && !ckgedit_xcl_styles) {
1209                tag = 'font';
1210                //<font 18pt/garamond;;color;;background_color>
1211                var font_str = '<font ' + this.font_size + '/' + this.font_family + ';;' + this.font_color + ';;' + this.font_bgcolor + ">";
1212                var inherits = font_str.match(/(inherit)/g);
1213                if (inherits && inherits.length < 3) HTMLParserFontInfix = true;
1214                var font_start = results.lastIndexOf('__STYLE__');
1215                results = results.splice(font_start, 9, font_str);
1216                results = results.replace(/_FORMAT_SPACE_<font/m, "<font");
1217                this.font_size = 'inherit';
1218                this.font_family = 'inherit';
1219                this.font_color = 'inherit';
1220                this.font_bgcolor = 'inherit';
1221                this.in_font = false;
1222                HTMLParserFont = true;
1223                results = results.replace(/__STYLE__/g, "");
1224            }
1225            if (tag == 'span' && this.curid) {
1226                this.curid = false;
1227                return;
1228            }
1229            if (tag == 'dl' && this.downloadable_code) {
1230                this.downloadable_code = false;
1231                return;
1232            }
1233            if (useComplexTables && (tag == 'td' || tag == 'th')) {
1234                this.current_cell.text = results.substring(this.cell_start);
1235                this.current_cell.text = this.current_cell.text.replace(/:::/gm, "");
1236                this.current_cell.text = this.current_cell.text.replace(/^[\s\|\^]+/, "");
1237            }
1238            if (tag == 'a' && (this.export_code || this.code_snippet)) {
1239                this.export_code = false;
1240                this.code_snippet = false;
1241                return;
1242            }
1243
1244            if (this.code_type && tag == 'span') tag = 'blank';
1245            var current_tag = tag;
1246            if (this.footnote) {
1247                tag = 'fn_end';
1248                this.footnote = false;
1249            } else if (tag == 'a' && this.xcl_markup) {
1250                this.xcl_markup = false;
1251                return;
1252            } else if (tag == 'table') {
1253                this.in_table = false;
1254                if (useComplexTables) {
1255                    results = results.substring(0, this.table_start);
1256                    insert_table(this.rows);
1257                }
1258            }
1259
1260            if (tag == 'p' && this.in_table) {
1261                tag = 'p_insert';
1262                HTMLParser_TABLE = true;
1263            }
1264            if (this.geshi) {
1265                this.geshi = false;
1266                return;
1267            }
1268
1269            if (tag == 'code' && !this.list_started) { // empty code markup corrupts results
1270                if (results.match(/''\s*$/m)) {
1271                    results = results.replace(/''\s*$/, "\n");
1272                    return;
1273                }
1274
1275            } else if (tag == 'a' && this.attr == 'src') {
1276                // if local image without link content, as in <a . . .></a>, delete link markup
1277                if (this.backup('\[\[', '\{')) return;
1278            }
1279
1280            if (this.end_nested) {
1281                this.end_nested = false;
1282                return; // prevent newline from being inserted between end of nested list and return to previous nested level
1283            }
1284
1285            if (tag == 'ol' || tag == 'ul') {
1286                this.list_level--;
1287                if (!this.list_level) this.format_in_list = false;
1288                if (this.prev_li.length) {
1289                    markup['li'] = this.prev_li.pop();
1290                    this.end_nested = true;
1291                    return;
1292                }
1293                tag = "\n\n";
1294            } else if (tag == 'a' && this.external_mime) {
1295                this.external_mime = false;
1296                if (this.is_mediafile) {
1297                    tag = '}} ';
1298                } else return;
1299
1300            } else if (tag == 'pre') {
1301                tag = markup_end[tag];
1302                if (this.code_type) {
1303                    tag += this.code_type + ">";
1304                } else {
1305                    var codeinx = results.lastIndexOf('code');
1306                    var fileinx = results.lastIndexOf('file');
1307                    if (fileinx > codeinx) {
1308                        this.code_type = 'file';
1309                    } else this.code_type = 'code';
1310                    tag += this.code_type + ">";
1311                }
1312                this.code_type = false;
1313
1314            } else if (markup_end[tag]) {
1315                tag = markup_end[tag];
1316            } else if (this.attr == 'u' && tag == 'em') {
1317                tag = 'u';
1318            } else if (tag == 'acronym') {} else {
1319                tag = markup[tag];
1320            }
1321
1322            if (current_tag == 'tr') {
1323                if (this.last_col_pipes) {
1324                    tag = "\n";
1325                    this.last_col_pipes = "";
1326                }
1327
1328                if (this.td_rowspan && this.rowspan_col == this.td_no + 1) {
1329                    this.is_rowspan = false;
1330                    this.last_column = this.td_no;
1331                    this.td_rowspan--;
1332                    tag = '|' + markup['row_span'] + "|\n";
1333                }
1334            } else if (current_tag == 'td' || current_tag == 'th') {
1335                this.last_col_pipes = "";
1336                this.in_td = false;
1337            } else if (current_tag.match(/h\d+/)) {
1338                this.in_header = false;
1339            }
1340
1341
1342            if (markup['li']) {
1343                if (results.match(/\n$/) && !this.list_level) {
1344                    tag = "";
1345                }
1346
1347            }
1348
1349
1350            if (this.in_endnotes && current_tag == 'sup') {
1351                return
1352            }
1353            results += tag;
1354
1355            if (format_chars[current_tag]) {
1356                if (this.list_level) {
1357                    this.format_in_list = true;
1358                    HTMLFormatInList = true;
1359                }
1360                results += markup['format_space'];
1361                HTMLParser_FORMAT_SPACE = markup['format_space'];
1362            }
1363            this.last_tag = current_tag;
1364
1365            if (this.td_colspan && !useComplexTables) {
1366                if (this.td_align == 'center') results += ' ';
1367                var _colspan = "|";
1368                if (current_tag == 'th')
1369                    _colspan = '^';
1370                var colspan = _colspan;
1371                for (var i = 1; i < this.td_colspan; i++) {
1372                    colspan += _colspan;
1373                }
1374                this.last_col_pipes = colspan;
1375                results += colspan;
1376                this.td_colspan = false;
1377            } else if (this.td_align == 'center') {
1378                results += ' ';
1379                this.td_align = '';
1380            }
1381
1382            if (current_tag == 'a' && this.link_formats.length) {
1383                var end_str = results.substring(this.link_pos);
1384                var start_str = results.substring(0, this.link_pos);
1385                var start_format = "";
1386                var end_format = "";
1387                for (var i = 0; i < this.link_formats.length; i++) {
1388                    var fmt = markup[this.link_formats[i]];
1389                    var endfmt = markup_end[this.link_formats[i]] ? markup_end[this.link_formats[i]] : fmt;
1390                    start_format += markup[this.link_formats[i]];
1391                    end_format = endfmt + end_format;
1392                }
1393
1394                start_str += start_format;
1395                end_str += end_format;
1396                results = start_str + end_str;
1397                this.link_formats = new Array();
1398                this.in_link = false;
1399            } else if (current_tag == 'a') {
1400                this.link_formats = new Array();
1401                this.in_link = false;
1402
1403            }
1404        },
1405
1406        chars: function(text) {
1407            text = text.replace(/\t/g, "    ");
1408            if (this.code_type == 'code') {
1409                text = text.replace(/(\n?|\s+)\\/gm, "$1CBL__Bksl");
1410            }
1411            if (text.match(/~~START_HTML_BLOCK~~/)) {
1412                text = text.replace(/~~START_HTML_BLOCK~~\n*/, "~~START_HTML_BLOCK~~\n<code>\n");
1413            }
1414            if (text.match(/~~CLOSE_HTML_BLOCK~~/)) {
1415                text = text.replace(/~~CLOSE_HTML_BLOCK~~\n*/gm, "\n</code>\n\n~~CLOSE_HTML_BLOCK~~\n\n");
1416            }
1417
1418            /*interwiki frasl refactoring*/
1419            if (this.interwiki) {
1420                //    text = text.replace(String.frasl,"\/");
1421            }
1422            if (this.interwiki && results.match(/>\w+\s*\|$/)) {
1423                this.interwiki = false;
1424                if (this.attr) {
1425                    results += text;
1426                } else {
1427                    results = results.replace(/>\w+\s*\|$/, '>' + text);
1428                }
1429                return;
1430            }
1431            if (this.in_multi_plugin) {
1432                text = text.replace('&lt; ', '&lt;');
1433            }
1434            text = text.replace(/&#39;/g, "'"); //replace single quote entities with single quotes
1435            text = text.replace(/^(&gt;)+/, function(match, quotes) {
1436                return (match.replace(/(&gt;)/g, "\__QUOTE__"));
1437            });
1438            text = text.replace(/&not;ags/g, '&notags'); //replace &not entity in include notags param
1439            //adjust spacing on multi-formatted strings
1440            results = results.replace(/([\/\*_])_FORMAT_SPACE_([\/\*_]{2})_FORMAT_SPACE_$/, "$1$2@@_SP_@@");
1441            if (text.match(/^&\w+;/)) {
1442                results = results.replace(/_FORMAT_SPACE_\s*$/, ""); // remove unwanted space after character entity
1443            }
1444
1445            if (this.link_only) {
1446                if (text) {
1447                    replacement = '|' + text + '}} ';
1448                    results = results.replace(/\}\}\s*$/, replacement);
1449                }
1450                return;
1451            }
1452            if (!this.code_type) {
1453                if (!this.last_col_pipes) {
1454                    text = text.replace(/\x20{6,}/, "   ");
1455                    text = text.replace(/^(&nbsp;)+\s*$/, '_FCKG_BLANK_TD_');
1456                    text = text.replace(/(&nbsp;)+/, ' ');
1457                }
1458
1459                if (this.format_tag) {
1460                    if (!this.list_started || this.in_table) text = text.replace(/^\s+/, '@@_SP_@@');
1461                } else if (this.last_tag == 'a') {
1462                    text = text.replace(/^\s{2,}/, " ");
1463                } else if (!this.using_fonts) text = text.replace(/^\s+/, '');
1464
1465                if (text.match(/nowiki&gt;/)) {
1466                    HTMLParser_NOWIKI = true;
1467                }
1468
1469                if (this.format_in_list || (HTMLParserFont && this.list_started)) {
1470                    text = text.replace(/^[\n\s]+$/g, '');
1471                    if (text.match(/\n{2,}\s{1,}/)) {
1472                        text = text.replace(/\n{2,}/, "\n");
1473                    }
1474                }
1475
1476                if (this.in_td && !text) {
1477                    //      text = "_FCKG_BLANK_TD_";
1478                    this.in_td = false;
1479                }
1480            } else {
1481                text = text.replace(/&lt;\s/g, '<');
1482                text = text.replace(/\s&gt;/g, '>');
1483                var geshi = text.match(/^\s*geshi:\s+(.*)$/m);
1484                if (geshi) {
1485                    results = results.replace(/<(code|file)>\s*$/, '<' + "$1" + ' ' + geshi[1] + '>');
1486                    text = text.replace(geshi[0], "");
1487                }
1488
1489            }
1490
1491
1492            if (this.attr && this.attr == 'dwfcknote') {
1493                if (text.match(/fckgL\d+/)) {
1494                    return;
1495                }
1496                if (text.match(/^[\-,:;!_]/)) {
1497                    results += text;
1498                } else {
1499                    results += ' ' + text;
1500                }
1501                return;
1502            }
1503
1504
1505
1506            if (this.downloadable_code && (this.export_code || this.code_snippet)) {
1507                this.downloadable_file = text;
1508                return;
1509            }
1510
1511            /* remove space between link end markup and following punctuation */
1512            if (this.last_tag == 'a' && text.match(/^[\.,;\:\!]/)) {
1513                results = results.replace(/\s$/, "");
1514            }
1515
1516            if (this.in_header) {
1517                text = text.replace(/---/g, '&mdash;');
1518                text = text.replace(/--/g, '&ndash;');
1519            }
1520            if (this.list_started) {
1521                results = results.replace(/_LIST_EOFL_\s*L_BR_K\s*$/, '_LIST_EOFL_');
1522            }
1523            if (!this.code_type) { // keep special character literals outside of code block
1524                // don't touch samba share or Windows path backslashes
1525                if (!results.match(/\[\[\\\\.*?\|$/) && !text.match(/\w:(\\(\w?))+/)) {
1526                    if (!text.match(/\\\\[\w\.\-\_]+\\[\w\.\-\_]+/)) {
1527                        text = text.replace(/([\\])/g, '%%$1%%');
1528                    }
1529                    text = text.replace(/([\*])/g, '_CKG_ASTERISK_');
1530                }
1531            }
1532
1533            if (this.in_endnotes && HTMLParserTopNotes.length) {
1534
1535                if (text.match(/\w/) && !text.match(/^\s*\d\)\s*$/)) {
1536                    text = text.replace(/\)\s*$/, "_FN_PAREN_C_");
1537                    var index = HTMLParserTopNotes.length - 1;
1538                    if (this.bottom_url) {
1539                        if (this.link_class && this.link_class == 'media') {
1540                            text = '{{' + this.bottom_url + '|' + text + '}}';
1541                        } else text = '[[' + this.bottom_url + '|' + text + ']]';
1542                    }
1543                    if (HTMLParserBottomNotes[HTMLParserTopNotes[index]]) {
1544                        text = text.replace('(', 'L_PARgr');
1545                        text = text.replace(')', 'R_PARgr');
1546                        HTMLParserBottomNotes[HTMLParserTopNotes[index]] += ' ' + text;
1547                    } else {
1548                        text = text.replace('(', 'L_PARgr');
1549                        text = text.replace(')', 'R_PARgr');
1550                        HTMLParserBottomNotes[HTMLParserTopNotes[index]] = text;
1551                    }
1552                }
1553                this.bottom_url = false;
1554                return;
1555            }
1556
1557
1558            if (text && text.length) {
1559                results += text;
1560            }
1561            // remove space between formatted character entity and following character string
1562            results = results.replace(/(&\w+;)\s*([\*\/_]{2})_FORMAT_SPACE_(\w+)/, "$1$2$3");
1563
1564            if (this.list_level && this.list_level > 1) {
1565                results = results.replace(/(\[\[.*?\]\])([ ]+[\*\-].*)$/, " $1\n$2");
1566            }
1567
1568            try { // in case regex throws error on dynamic regex creation
1569                var regex = new RegExp('([\*\/\_]{2,})_FORMAT_SPACE_([\*\/\_]{2,})(' + RegExp.escape(text) + ')$');
1570                if (results.match(regex)) {
1571                    // remove left-over space inside multiple format sequences
1572                    results = results.replace(regex, "$1$2$3");
1573                }
1574            } catch (ex) {}
1575
1576            if (!HTMLParserOpenAngleBracket) {
1577                if (text.match(/&lt;/)) {
1578                    HTMLParserOpenAngleBracket = true;
1579                }
1580            }
1581        },
1582
1583        comment: function(text) {
1584            // results += "<!--" + text + "-->";
1585        },
1586
1587        dbg: function(text, heading) {
1588            if (text.replace) {
1589                text = text.replace(/^\s+/g, "");
1590                text = text.replace(/^\n$/g, "");
1591                text = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1592                if (!text) return;
1593            }
1594            if (heading) {
1595                heading = '<b>' + heading + "</b>\n";
1596            }
1597            HTMLParser_DEBUG += heading + text + "\n__________\n";
1598        }
1599
1600    });
1601
1602
1603    /*
1604      we allow escaping of troublesome characters in plugins by enclosing them withinback slashes, as in \*\
1605      the escapes are removed here together with any DW percent escapes
1606   */
1607
1608    results = results.replace(/(\[\[\\\\)(.*?)\]\]/gm, function(match, brackets, block) {
1609        block = block.replace(/\\/g, "_SMB_");
1610        return brackets + block + ']]';
1611    });
1612
1613    results = results.replace(/%%\\%%/g, '_ESC_BKSLASH_');
1614    results = results.replace(/%*\\%*([^\w\\]{1})%*\\%*/g, "$1");
1615
1616    results = results.replace(/_SMB_/g, "\\");
1617
1618    results = results.replace(/(\s*={2,}).*?CKGE_TMP_(\w+)(.*?).*?CKGE_TMP_c?\2.*?\1/gm, function(m, tag) { //remove formats from headers
1619        m = m.replace(/CKGE_TMP_\w+/gm, "");
1620        var v = jQuery("#formatdel").val();
1621        if (!v) {
1622            jQuery('#dw__editform').append('<input type="hidden" id="formatdel" name="formatdel" value="del" />');
1623        }
1624        return m;
1625    });
1626    results = results.replace(/\s?(CKGE_TMP_\w+)\s?/gm, function(m, tag) {
1627        if ($FORMAT_SUBST[tag]) return $FORMAT_SUBST[tag];
1628        return m;
1629    });
1630
1631    results = results.replace(/(\s*={2,})(.*?)(\[\[|\{\{)(.*?)(\]\]|\}\})(.*?)\1/gm, function(m, h_markup, whatever, bracket_1, inner, bracket_2, end_str) {
1632        end_str = end_str.replace(/\[\[(.*?)\|(.*?)\]\]/g, "$2");
1633        end_str = end_str.replace(/\{\{(.*?)\|(.*?)\}\}/g, "$2");
1634        m = h_markup + " " + whatever + " " + inner.replace(/.*?\|(.*?)/, "$1") + " " + end_str + " " + h_markup;
1635        var v = jQuery("#formatdel").val();
1636        if (!v) {
1637            jQuery('#dw__editform').append('<input type="hidden" id="formatdel" name="formatdel" value="del" />');
1638        }
1639        return m;
1640    });
1641
1642    if (id == 'test') {
1643        if (!HTMLParser_test_result(results)) return;
1644    }
1645
1646    results = results.replace(/\{ \{ rss&gt;Feed:/mg, '{{rss&gt;http://');
1647    results = results.replace(/\{ \{ rss&gt;sFeed:/mg, '{{rss&gt;https://')
1648    results = results.replace(/~ ~ (NOCACHE|NOTOC)~ ~/mg, '~~' + "$1" + '~~');
1649    if (HTML_InterWiki) {
1650        var ReplaceLinkMatch = function(tag, link) {
1651            tag_1 = tag.replace(/oIWIKIo(.*)cIWIKIc/, "$1");
1652            if (tag_1 == link) return true;
1653            link = link.replace(/\s/, '%20');
1654            return (link == tag_1);
1655        };
1656        results = results.replace(/\[\[(\w+\.?\w{0,12})>(.*?)\|(.*?)\]\]/gm, function(match, id, iw_replace, link_text) {
1657            if (iw_replace == 'oIWIKIocIWIKIc') iw_replace = link_text;
1658
1659            if ((iw_replace == 'oIWIKIo' + link_text.replace(/\s/, '%20') + 'cIWIKIc') || (iw_replace == link_text) || ReplaceLinkMatch(iw_replace, link_text)) {
1660                link_text = "";
1661            } else {
1662                link_text = "|" + link_text;
1663            }
1664
1665            return ('[[' + id + '>' + iw_replace + link_text + ']]');
1666        });
1667    }
1668
1669    results = results.replace(/>.*?oIWIKIo(.*?)cIWIKIc/mg, '>' + "$1");
1670
1671    if (HTMLParser_FORMAT_SPACE) {
1672        if (HTMLParser_COLSPAN) {
1673            results = results.replace(/\s*([\|\^]+)((\W\W_FORMAT_SPACE_)+)/gm, function(match, pipes, format) {
1674                format = format.replace(/_FORMAT_SPACE_/g, "");
1675                return (format + pipes);
1676            });
1677        }
1678        results = results.replace(/&quot;/g, '"');
1679        var regex = new RegExp(HTMLParser_FORMAT_SPACE + '([\\-]{2,})', "g");
1680        results = results.replace(regex, " $1");
1681
1682        results = results.replace(/\]\](\*\*|\/\/|\'\'|__|<\/del>)_FORMAT_SPACE_/, "]]$1@@_SP_@@");
1683
1684        var regex = new RegExp("(&amp;|\\W|\\w|\\d)(\\*\\*|\\/\\/|\\'\\'|__|<\/del>)+" + HTMLParser_FORMAT_SPACE + '(\\w|\\d)', "g");
1685        results = results.replace(regex, "$1$2$3");
1686
1687        var regex = new RegExp(HTMLParser_FORMAT_SPACE + '@@_SP_@@', "g");
1688        results = results.replace(regex, ' ');
1689
1690        //spacing around entities with double format characters
1691        results = results.replace(/([\*\/_]{2})@@_SP_@@(&\w+;)/g, "$1 $2");
1692
1693        results = results.replace(/\n@@_SP_@@\n/g, '');
1694        results = results.replace(/@@_SP_@@\n/g, '');
1695        results = results.replace(/@@_SP_@@/g, ' ');
1696        var regex = new RegExp(HTMLParser_FORMAT_SPACE + '([^\\)\\]\\}\\{\\-\\.,;:\\!\?"\x94\x92\u201D\u2019' + "'" + '])', "g");
1697        results = results.replace(regex, " $1");
1698        regex = new RegExp(HTMLParser_FORMAT_SPACE, "g");
1699        results = results.replace(regex, '');
1700
1701        if (HTMLFormatInList) {
1702            /* removes extra newlines from lists */
1703            results = results.replace(/^(\s+[\-\*_]\s*)([\*\/_\']{2})(.*?)(\2)([^\n]*)\n+/gm,
1704                function(match, list_type, format, text, list_type_close, rest) {
1705                    return (list_type + format + text + list_type_close + rest + "\n");
1706                });
1707        }
1708    }
1709
1710    /* Fixes for links in lists*/
1711    if (HTMLLinkInList) {
1712        /*fix for links in lists at ends of lines, which cause loss of line-feeds
1713            results = results.replace(/(\]\]|\}\})(\s+)(\*|-)/mg,
1714                    function(match,link,spaces,type) {
1715                     spaces =  spaces.replace(/\n/,"");
1716                    return (link + "\n" + spaces+type);
1717              });
1718            /*fix for links in lists before ends of lines, which cause extra  line-feeds */
1719        results = results.replace(/(\*|-).*?(\[\[|\{\{).*?(\]\]|\}\})([\s\w\/\-\x3A-\x40\x5B-\x60\x7B-\x7F,;\>\<\&]+)\n\n/mg,
1720            function(all, type, b, c, tail) {
1721                all = all.replace(/[\n]$/, "");
1722                return (all);
1723            });
1724
1725    }
1726
1727    var line_break_final = "\\\\";
1728    if (HTMLParser_LBR) {
1729        results = results.replace(/(L_BR_K)+/g, line_break_final);
1730        results = results.replace(/L_BR_K/gm, line_break_final);
1731        results = results.replace(/(\\\\)\s+/gm, "$1 \n");
1732    }
1733
1734    if (HTMLParser_PRE) {
1735        results = results.replace(/\s+<\/(code|file)>/g, "\n</" + "$1" + ">");
1736        if (HTMLParser_Geshi) {
1737            results = results.replace(/\s+;/mg, ";");
1738            results = results.replace(/&lt;\s+/mg, "<");
1739            results = results.replace(/\s+&gt;/mg, ">");
1740
1741        }
1742    }
1743
1744    if (HTMLParser_TABLE) {
1745        results += "\n" + line_break_final + "\n";
1746        var regex = new RegExp(HTMLParserParaInsert, "g");
1747        results = results.replace(regex, ' ' + line_break_final + ' ');
1748
1749        // fix for colspans which have had text formatting which cause extra empty cells to be created
1750        results = results.replace(/(\||\^)[ ]+(\||\^)\s$/g, "$1\n");
1751        results = results.replace(/(\||\^)[ ]+(\||\^)/g, "$1");
1752    }
1753    // prevents valid empty td/th cells from being removed above
1754    results = results.replace(/_FCKG_BLANK_TD_/g, " ");
1755
1756    if (HTMLParserOpenAngleBracket) {
1757        results = results.replace(/\/\/&lt;\/\/\s*/g, '&lt;');
1758    }
1759
1760    if (HTMLParserFont) // HTMLParserFont start
1761    {
1762
1763        String.prototype.font_link_reconcile = function(v) {
1764
1765            if (v == 1) {
1766                regex = /\[\[(.*?)(<font[^\>]+>)([^<]+(\]\])?)[^\>]+\/font>\s*(\]\])/gm;
1767            } else regex = /(<font[^\>\{]+>)\{\{(:?.*?)\|(:?.*?)<\/font>/gm;
1768
1769
1770            return (
1771                this.replace(
1772                    regex,
1773                    function(m, a, b, c) {
1774                        a = a.replace(/\n/gm, "");
1775                        a = a.replace(/\s/gm, "");
1776                        a = a.replace(/[\[\]\{\}]/g, "");
1777                        a = a.replace(/\|/g, "");
1778                        c = c.replace(/\n/gm, "");
1779                       // c = c.replace(/\s/gm, "");
1780                        c = c.replace(/[\[\]\}\{]/g, "");
1781                        if (v == 1)
1782                            c = '[[' + a + '|' + c + ']]';
1783                        else c = '{{' + b + '|' + c + '}}';
1784
1785                        var val = prompt(LANG.plugins.ckgedit.font_err_1 + "\n" + c + "\n" + LANG.plugins.ckgedit.font_err_2);
1786                        if (val == null) {
1787                            if (ckgedit_to_dwedit) {
1788                                ckgedit_to_dwedit = false;
1789                                return c;
1790                            } else throw new Error(LANG.plugins.ckgedit.font_err_throw);
1791                        }
1792                        if (val) return val;
1793                        return c;
1794                    }
1795                )
1796            );
1797        }
1798        if (HTMLParserFontInfix) {
1799            results = results.replace(/<\/font>\s{1}/gm, "</font>");
1800        }
1801
1802        if (fontConflict()) {
1803            if (confirm(LANG.plugins.ckgedit.font_conflict)) return;
1804            var v = jQuery("#fontdel").val();
1805            if (!v) {
1806                jQuery('#dw__editform').append('<input type="hidden" id="fontdel" name="fontdel" value="del" />');
1807            }
1808        }
1809        results = results.font_link_reconcile(1);
1810        results = results.font_link_reconcile(2);
1811
1812        var regex = /\>\s+(\*\*|__|\/\/|'')\s+_\s+\1\s+<\/font>/gm;
1813        results = results.replace(regex, function(m) {
1814            m = m.replace(/\s+/g, "");
1815            return m;
1816        });
1817
1818        results = results.replace(/\[\[(.*?)\|(<font[^\>]+>)(.*?)(<\/font>)\s*(\]\])\s*/gm, function(match, a, b, c) {
1819            match = '[[' + a + '|' + c + ']]';
1820            var v = jQuery("#fontdel").val();
1821            if (!v) {
1822                jQuery('#dw__editform').append('<input type="hidden" id="fontdel" name="fontdel" value="del" />');
1823            }
1824            return match;
1825        });
1826
1827        results = results.replace(/(\s*={2,})\s*(.*?)(<font[^\>]+>)(.*?)(<\/font>)(.*?)\s*\1/gm, function(match) {
1828            match = match.replace(/<\/font>/g, " ");
1829            match = match.replace(/<font.*?>/g, " ");
1830            var v = jQuery("#formatdel").val();
1831            if (!v) {
1832                jQuery('#dw__editform').append('<input type="hidden" id="formatdel" name="formatdel" value="del" />');
1833            }
1834            return match;
1835        });
1836
1837    } // HTMLParserFont end
1838
1839    if (HTMLParserTopNotes.length) {
1840        results = results.replace(/<sup>\(\(\){2,}\s*<\/sup>/g, "");
1841        results = results.replace(/\(\(+(\d+)\)\)+/, "(($1))");
1842        for (var i in HTMLParserBottomNotes) { // re-insert DW's bottom notes at text level
1843            var matches = i.match(/_(\d+)/);
1844            var pattern = new RegExp('(\<sup\>)*[\(]+' + matches[1] + '[\)]+(<\/sup>)*');
1845            HTMLParserBottomNotes[i] = HTMLParserBottomNotes[i].replace(/(\d+)_FN_PAREN_C_/, "");
1846            results = results.replace(pattern, '((' + HTMLParserBottomNotes[i].replace(/_FN_PAREN_C_/g, ") ") + '))');
1847        }
1848        results = results.replace(/<sup><\/sup>/g, "");
1849        results = results.replace(/((<sup>\(\(\d+\)\)\)?<\/sup>))/mg, function(fn) {
1850            if (!fn.match(/p>\(\(\d+/)) {
1851                return "";
1852            }
1853            return fn;
1854        });
1855
1856    }
1857
1858    results = results.replace(/(={3,}.*?)(\{\{.*?\}\})(.*?={3,})/g, "$1$3\n\n$2");
1859    // remove any empty footnote markup left after section re-edits
1860    results = results.replace(/(<sup>)*\s*\[\[\s*\]\]\s*(<\/sup>)*\n*/g, "");
1861    // remove piled up sups with ((notes))
1862
1863    results = results.replace(/<sup>\s*\(\(\d+\)\)\s*<\/sup>/mg, "");
1864
1865    if (HTMLParser_MULTI_LINE_PLUGIN) {
1866        results = results.replace(/<\s+/g, '<');
1867        results = results.replace(/&lt;\s+/g, '<');
1868    }
1869
1870    if (HTMLParser_NOWIKI) {
1871        /* any characters escaped by DW %%<char>%% are replaced by NOWIKI_<char>
1872         <char> is restored in save.php
1873     */
1874        var nowiki_escapes = '%'; //this technique allows for added chars to attach to NOWIKI_$1_
1875        var regex = new RegExp('([' + nowiki_escapes + '])', "g");
1876
1877        results = results.replace(/(&lt;nowiki&gt;)(.*?)(&lt;\/nowiki&gt;)/mg,
1878            function(all, start, mid, close) {
1879                mid = mid.replace(/%%(.)%%/mg, "NOWIKI_$1_");
1880                return start + mid.replace(regex, "NOWIKI_$1_") + close;
1881            });
1882    }
1883
1884    results = results.replace(/__SWF__(\s*)\[*/g, "{{$1");
1885    results = results.replace(/\|.*?\]*(\s*)__FWS__/g, "$1}}");
1886    results = results.replace(/(\s*)__FWS__/g, "$1}}");
1887    results = results.replace(/\n{3,}/g, '\n\n');
1888    results = results.replace(/_LIST_EOFL_/gm, " " + line_break_final + " ");
1889
1890    if (useComplexTables) {
1891        if (results.indexOf('~~COMPLEX_TABLES~~') == -1) {
1892            results += "~~COMPLEX_TABLES~~\n";
1893        }
1894    }
1895    if (!useComplexTables) {
1896        results = results.replace(/~~COMPLEX_TABLES~~/gm, "");
1897    }
1898    results = results.replace(/_CKG_ASTERISK_/gm, '*');
1899    results = results.replace(/_ESC_BKSLASH_/g, '\\');
1900    results = results.replace(/divalNLine/gm, "\n");
1901    if (id == 'test') {
1902        if (HTMLParser_test_result(results)) {
1903            alert(results);
1904        }
1905        return;
1906    }
1907
1908    var dwform = GetE('dw__editform');
1909    dwform.elements.fck_wikitext.value = results;
1910
1911    if (id == 'bakup') {
1912        return;
1913    }
1914    if (id) {
1915        var dom = GetE(id);
1916        dom.click();
1917        return true;
1918    }
1919}
1920
1921jQuery(document).ready(function() {
1922    var edit__summary = false;
1923    jQuery(document).on("keypress", "input#edit__summary", function(e) {
1924        if (e.which == 13) {
1925            edit__summary = true;
1926            jQuery("#save_button").trigger("mousedown");
1927        }
1928    });
1929
1930    jQuery("#ebut_test").mousedown(function() {
1931        parse_wikitext('test');
1932    });
1933
1934    jQuery("#ebtn__delete").click(function() {
1935        if (edit__summary) {
1936            edit__summary = false;
1937            return;
1938        }
1939        return confirm(JSINFO['confirm_delete']);
1940    });
1941
1942    jQuery("#ebtn__delete").mouseup(function() {
1943        draft_delete();
1944    });
1945
1946    jQuery("#ebtn__dwedit").click(function() {
1947        ckgedit_to_dwedit = true;
1948        setDWEditCookie(2, this);
1949        parse_wikitext('edbtn__save');
1950        this.form.submit();
1951    });
1952
1953    jQuery("#ebtn__fbswitch").click(function() {
1954        if (getCookie('ckgFbOpt') == 'dokuwiki') {
1955            document.cookie = 'ckgFbOpt=ckgedit;SameSite=Lax';
1956        } else {
1957            document.cookie = 'ckgFbOpt=dokuwiki;SameSite=Lax';
1958        }
1959        parse_wikitext('edbtn__save');
1960        this.form.submit();
1961    });
1962
1963    jQuery("#ckgedit_draft_btn").click(function() {
1964        ckgedit_get_draft();
1965    });
1966    jQuery("#backup_button").click(function() {
1967        renewLock(true);
1968    });
1969    jQuery("#revert_to_prev_btn").click(function() {
1970        revert_to_prev();
1971    });
1972
1973    jQuery("#no_styling_btn").click(function() {
1974        this.form.styling.value = "no_styles";
1975        this.form.prefix.value = "";
1976        this.form.suffix.value = "";
1977        this.form.rev.value = "";
1978    });
1979
1980    jQuery("#ebut_cancel").mouseup(function() {
1981        if (this.form.template && this.form.template.value == 'tpl') return;
1982        if (window.dwfckTextChanged) return;
1983        draft_delete();
1984    });
1985    jQuery("#save_button").mousedown(function() {
1986        if (this.form.template && this.form.template.value == 'tpl') window.dwfckTextChanged = true;
1987        if (!window.dwfckTextChanged && !JSINFO['cg_rev']) {
1988            ckgedit_dwedit_reject = true;
1989            parse_wikitext('ebut_cancel');
1990        } else {
1991            parse_wikitext('edbtn__save');
1992        }
1993    });
1994});