0
votes

I try do simple thing:

Copy HTML from TinyMCE editor to textarea (programmatically)...

How i copy:

        tinymce_apply_function(function(editor){
            $(editor.getElement()).html(editor.getContent());
        });

Description: Here i copy HTML from TinyMCE to <textarea>. I need it, because after it, $('form').serializeArray(); will executed, and all fields from form will saved to database

About tinymce_apply_function():

/**
 * applies function for all editors
 * @param par_callback
 */
function tinymce_apply_function(par_callback) {
    if (common.tinymce) {
        for (var i = 0; i < tinyMCE.editors.length; i++) {
            par_callback(tinyMCE.editors[i]);
        }
    }
}

Description: just apply some function for all TinyMCE editors (page may have more than one TinyMCE editor)

And finally i do:

  1. Load saved HTML from database

  2. Put code to <textarea>

  3. Put code from <textarea> to TinyMCE

    $.each(data, function(i, field){
        var tmp_el = $('[name=' + field.name + ']');
        if (tmp_el.length) {
            if (tmp_el.prop("tagName") == 'TEXTAREA') {
                tmp_el.html(field.value);
                if (common.tinymce && typeof(tmp_el.attr('id')) !== 'undefined' && typeof(tinyMCE.get(tmp_el.attr('id'))) !== 'undefined') {
                    tinyMCE.get(tmp_el.attr('id')).setContent(tmp_el.html());
                }
                // ...
                // other code is not important
    

My problem:

When i get HTML from TinyMCE, then this code looks like:

<p>&lt;p&gt;&lt;strong&gt;_..-~*"````"*~-._ &lt; { (: BOLD :) } &gt;&lt;/strong&gt; &lt;br&gt;&lt;/p&gt;&lt;p&gt;&nbsp;_____&lt;br&gt;[_____]&lt;br data-mce-bogus="1"&gt;&lt;/p&gt;</p>

i.e. HTML has been encoded (why?) And because of it, when i put this "code" to TinyMCE back, it looks like 'plain' text :

enter image description here

What i doing wrong?

p.s. sorry for bad english, i still learn it

1

1 Answers

0
votes

I found solution. (yes, i know what that method have some (ex.:XSS) security issues so, if you know solution, what better than my - please, post it here :) )

tinyMCE.get(tmp_el.attr('id')).setContent($('<div/>').html(tmp_el.html()).text());

i.e. I used

$('<div/>').html(tmp_el.html()).text()

instead

tmp_el.html()

(p.s. tmp_el - textarea)