Skip to content

Instantly share code, notes, and snippets.

@vladimir-s-snippets
Created October 9, 2013 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimir-s-snippets/6904995 to your computer and use it in GitHub Desktop.
Save vladimir-s-snippets/6904995 to your computer and use it in GitHub Desktop.
TinyMCE: parsing pasted html
$('#myEditor').tinymce({
...
content_css: '/path_to/styles.css',
paste_preprocess : function(pl, o) {
// Content string containing the HTML from the clipboard
o.content = strip_tags(o.content);
},
...
});
function strip_tags(input, allowed) {
if (allowed === undefined) {
allowed = '<table><tr><tbody><td><th><caption><object><a><h1><h2><h3><h4><h5><h6><p><br><ul><ol><li>';
}
//Taken from https://github.com/kvz/phpjs/blob/master/functions/strings/strip_tags.js
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment