So I finally find a way to do that !
You can do this with a mix of Telerik stripping using : StripFormattingOptions="Css, Font, Span, ConvertWordLists"
and using some javascript on HTML pasting OnClientPasteHtml="onClientPasteHtml".
Here is the code that will strip the unwanted tags
function onClientPasteHtml(editor, args) {
var commandName = args.get_commandName();
var value = args.get_value();
if (commandName == "Paste") {
//create a div, set the html content to it,
// remove style attribute, remove non pertinent tags.
var div = document.createElement("DIV");
Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, value);
var nostyle = $(div)[0];
// replace unwanted tags
var strippedHtml = $(nostyle).html().replace(/<\/?([a-z]+)[^>]*>/gi, function (match, tag) {
// any of these is valid
tag = tag.toLowerCase();
return (tag === "em" || tag === "ol" ||
tag === "sub" || tag === "sup" || tag === "ul" || tag === "li"|| tag === "br" || tag === "i") ? match : "";
});
// attributes gone
Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, strippedHtml);
removeAttributes($(div)[0]);
strippedHtml = $(div).html();
args.set_value(strippedHtml);
}
}
function removeAttributes(el) {
while (el.attributes.length > 0) {
el.removeAttribute(el.attributes[0].name);
}
if (el.childNodes.length > 0) {
for (var child in el.childNodes) {
if (el.childNodes[child].nodeType == 1)
removeAttributes(el.childNodes[child]);
}
}
}
Also we added a validator base on dtd XHTML validation that will tell us if the copy cut did not forget any tags (such as the opening and closing tag for a list).
For that we used the xhtml1-transitional.dtd :
protected bool ValidateMaisRespProjectText()
{
string html = txtJobMainResp.GetHtml(EditorStripHtmlOptions.None);
XHtmlErrors = new List<string>();
XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
// Create a local reference for validation11.
string xhtmlDtdFile = HttpContext.Current.Server.MapPath("DTD/xhtml1-transitional.dtd");
string newDoctype = string.Format("<!DOCTYPE html SYSTEM \"file://{0}\">", xhtmlDtdFile);
// add doc type validation + root element to make it valid.
html = newDoctype + Environment.NewLine + "<html><head><title>title</title></head><body><div>" + html + "</div></body></html>";
XmlReader reader = XmlReader.Create(new System.IO.StringReader(html), settings);
try
{
while (reader.Read())
{
}
}
catch (Exception ex)
{
XHtmlErrors.Add(ex.Message);
txtJobMainRespValidator1.IsValid = false;
txtJobMainRespValidator1.Text = "* The information entered has invalid HTML content. (DEBUG: " + XHtmlErrors.FirstOrDefault() + ")";
}
finally
{
if (reader != null)
reader.Close();
}
if (XHtmlErrors.Count != 0)
{
txtJobMainRespValidator1.IsValid = false;
txtJobMainRespValidator1.Text = "* The information entered has invalid HTML content. (DEBUG: " + XHtmlErrors.FirstOrDefault() + ")";
}
return XHtmlErrors.Count == 0;
}
private void ValidationEventHandler(object sender, ValidationEventArgs e)
{
XHtmlErrors.Add(string.Format("({0}) {1} - [Line: {2}, Char: {3}]", e.Severity, e.Message, e.Exception.LineNumber, e.Exception.LinePosition));
}
The validation adds the formatting errors to a list. Then you just have to display the messages you want in your page.
Hope this will help
(And if you find some enhancement to that solution don't hesitate to tell me :) )