TinyMCE 粘贴为纯文本 [英] TinyMCE Paste As Plain Text
本文介绍了TinyMCE 粘贴为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是网络上 RTE 的常见问题之一.你能指导我如何:
This is one of the common issue with RTEs on web. Could you please guide me through how to:
- 粘贴为纯文本
- 保留 HTML 但删除 WORD/HTML 样式
我想直接在paste上做(paste_preprocess回调),不打开Paste插件提供的对话框.
I want to do it directly on paste (paste_preprocess callback), without opening the dialogs provided by Paste plugins.
有什么想法/经验吗?
谢谢,
伊姆兰
推荐答案
这是我为了粘贴纯文本所做的工作.
This is what i do to get paste plain text.
1.paste_preprocess 设置(在 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.函数strip_tags(在主文档上)
// 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屋!
查看全文