Created
October 9, 2013 17:28
-
-
Save vladimir-s-snippets/6904995 to your computer and use it in GitHub Desktop.
TinyMCE: parsing pasted html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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