TinyMCE粘贴为纯文本 [英] TinyMCE Paste As Plain Text

查看:232
本文介绍了TinyMCE粘贴为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是网络上RTE的常见问题之一。请引导我如何:


  1. 粘贴为普通文本

  2. 保留HTML但删除WORD / HTML样式

我想直接在粘贴(paste_preprocess回调)上执行此操作,而无需打开Paste提供的对话框插件。



任何想法/经验?

谢谢,

解决方案

这就是我所做的粘贴纯文本。



1。 paste_preprocess设置(在tinymce init中)

  paste_preprocess:function(pl,o){
//例如:保留粗体,斜体,下划线和段落
//o.content = strip_tags(o.content,'< b>< u>< i>< p>');

//移除所有标签=>纯文本
o.content = strip_tags(o.content,'');
},

2。函数strip_tags(在主文档中)

  //从字符串中剥离HTML和PHP标签
//返回1:'Kevin< b> van< / b> < i> Zonneveld< i>'
//例2:strip_tags('< p> Kevin< img src =someimage.pngonmouseover =someFunction()> van< i> ; Zonneveld< / i>< / p>','< p>');
//返回2:'< p> Kevin van Zonneveld< / p>'
//示例3:strip_tags(< a href ='http://kevin.vanzonneveld.net' > Kevin van Zonneveld< / a>,< a>);
//返回3:'< a href ='http://kevin.vanzonneveld.net'> Kevin van Zonneveld< / a>'
//示例4:strip_tags('1< ; 5 5> 1');
//返回4:'1< 5 5> 1'
函数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);
};
//构建允许标记关联数组
if(allowed_tags){
allowed_array = allowed_tags.match(/([a-zA-Z0-9] +)/ gi);
}
str + ='';

//匹配标记
matches = str.match(/(< \ /?[\S] [^>]>)/ gi);
//通过所有的HTML标签
(key in matches){
if(isNaN(key)){
// IE7 Hack
continue;
}

//保存HTML标记
html = matches [key] .toString();
//标签不在允许列表中吗?从str删除!
allowed = false;

//遍历所有允许的标签
for(k in allowed_array){// Init
allowed_tag = allowed_array [k];
i = -1; (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);}

//确定
if(i == 0){allowed = true;
休息; $!

$ b if(!allowed){
str = replacer(html,,str); //自定义替换。没有regexing
}
}
return str;
}


This is one of the common issue with RTEs on web. Could you please guide me through how to:

  1. Paste as the PLAIN TEXT
  2. Retain the HTML but remove the WORD/HTML styling

I want to do it directly on paste (paste_preprocess callback), without opening the dialogs provided by Paste plugins.

Any thoughts/experiences ?

Thanks,

Imran

解决方案

This is what i do to get paste plain text.

1. paste_preprocess setting (in tinymce init)

paste_preprocess : function(pl, o) {
  //example: keep bold,italic,underline and paragraphs
  //o.content = strip_tags( o.content,'<b><u><i><p>' );

  // remove all tags => plain text
  o.content = strip_tags( o.content,'' );
},

2. function strip_tags (on the main document)

// 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;
}

这篇关于TinyMCE粘贴为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆