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