1

I am trying to copy html text and paste it into an unformatted way in TinyMCE by simply using CTRL + C, CTRL + V.

I have had a hard time to do this as TinyMCE constantly tries to keep the initial formatting. I am using Rails.

Would you know any work around this?

CDspace
  • 2,639
  • 18
  • 30
  • 36
Roman
  • 337
  • 1
  • 3
  • 12
  • What is your initial format? Do you mean the line ending changes ? – Raptor Nov 29 '13 at 06:40
  • For instance there is text I am copying from an HTML page and then copy it to TinyMCE; well, I'd need TinyMCE not to keep the formatting, just to automatically paste as plain text when using CTRL+V. right now it is trying to keep the HTML formatting into TinyMCE – Roman Nov 29 '13 at 06:59
  • TinyMCE is a **text** editor (not a WYSIWYG editor). You expect it displays HTML ? – Raptor Nov 29 '13 at 07:01
  • As mentioned above, I do not want it to display HTML, I need it to remove automatically html format and use plain text when doing CTRL+V – Roman Nov 29 '13 at 18:05
  • there is no magic. Copy piece by piece. – Raptor Nov 30 '13 at 01:20

1 Answers1

0

You may configure tinymce to strip all unwanted tags when pasting.

To do this use the tinymce config param paste_preprocess:

paste_preprocess : function(pl, o) {
    o.content = window.strip_tags( o.content,'<p><div><ul><ol><li><br>' );
},

with the function strip_tags as follows:

// Strips HTML and PHP tags from a string 
// returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
// example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
// returns 2: '<p>Kevin van Zonneveld</p>'
// example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
// returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
// example 4: strip_tags('1 < 5 5 > 1');
// returns 4: '1 < 5 5 > 1'
function strip_tags (str, allowed_tags)
{

    var key = '', allowed = false;
    var matches = [];    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = ''; 
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
                // IE7 Hack
            continue;
        }

        // Save HTML tag
        html = matches[key].toString();
        // Is tag not in allowed list? Remove from str!
        allowed = false;

        // Go through all allowed tags
        for (k in allowed_array) {            // Init
            allowed_tag = allowed_array[k];
            i = -1;

            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
}

You may want to have a closer look at this SO-Thread too: TinyMCE Paste As Plain Text

Community
  • 1
  • 1
Thariama
  • 50,002
  • 13
  • 138
  • 166