-
ederkley
-
-
-
Joined on 06-01-2012
-
-
Posts 9
-
-
|
Sample - HTML filter to only keep basic format
I thought I would share this for anyone looking to do something similar. The idea here is to be able to paste in content and keep only basic formatting such as new lines, bold, italic, underline and lists. Anything else such as fonts, font-size, links etc are dropped. This allows content from multiple sources to be easily combined without playing with formatting. The code is cobbled together from various sources and I'm pretty new to JavaScript and DOM so I wouldn't be surprised if there are better ways to do this. Cheers - function RichTextEditor_OnPasteFilter(rteeditor, info) {
- var html = info.Arguments[0];
- var cmd = info.Arguments[1];
- var i;
- var attr;
- var newElement;
- var node, nodeChild;
-
-
- var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async = false;
- html = '<span>' + html + '</span>'
- xmlDoc.loadXML(html);
-
- if (xmlDoc.hasChildNodes()) {
-
-
-
- var walkDOM = function (node, func) {
- if (node.attributes) {
- func(node);
- }
- node = node.firstChild;
- while (node) {
- walkDOM(node, func);
- node = node.nextSibling;
- }
- }
-
-
- walkDOM(xmlDoc.firstChild, function AddBoldNode(node) {
- attr = node.getAttribute("style");
- if (attr) {
- attr = attr.replace(" ", "").toLowerCase();
- if (attr.indexOf("font-weight:bold") >= 0 || attr.indexOf("font-weight:700") >= 0 || attr.indexOf("font-weight:800") >= 0 || attr.indexOf("font-weight:900") >= 0) {
- newElement = xmlDoc.createElement('b');
- nodeChild = node.firstChild;
- node.replaceChild(newElement, nodeChild);
- newElement.appendChild(nodeChild);
- }
- }
- });
-
-
- walkDOM(xmlDoc.firstChild, function AddUnderlineNode(node) {
- attr = node.getAttribute("style");
- if (attr) {
- attr = attr.replace(" ", "").toLowerCase();
- if (attr.indexOf("text-decoration:underline") >= 0) {
- newElement = xmlDoc.createElement('u');
- nodeChild = node.firstChild;
- node.replaceChild(newElement, nodeChild);
- newElement.appendChild(nodeChild);
- }
- }
- });
-
-
- walkDOM(xmlDoc.firstChild, function AddItalicNode(node) {
- attr = node.getAttribute("style");
- if (attr) {
- attr = attr.replace(" ", "").toLowerCase();
- if (attr.indexOf("font-style:italic") >= 0) {
- newElement = xmlDoc.createElement('i');
- nodeChild = node.firstChild;
- node.replaceChild(newElement, nodeChild);
- newElement.appendChild(nodeChild);
- }
- }
- });
-
-
- walkDOM(xmlDoc.firstChild, function CleanComplexAttr(node) {
- switch (node.nodeName) {
- case "ul": case "ol": case "li": case "dl": case "p": case "div":
-
- for (i = node.attributes.length; i-- > 0; ) {
- node.removeAttributeNode(node.attributes[i]);
- }
- break;
- default:
- }
- });
-
-
- html = xmlDoc.xml;
- }
-
-
- html = html.replace(/<ol>/gim, "<ol>");
- html = html.replace(/<ul>/gim, "<ul>");
- html = html.replace(/<\/ol>/gim, "</ol>");
- html = html.replace(/<\/ul>/gim, "</ul>");
- html = html.replace(/<li>/gim, "<li>");
- html = html.replace(/<\/li>/gim, "</li>");
- html = html.replace(/<br>/gim, "<br>");
- html = html.replace(/<br\s\/>/gim, "<br>");
- html = html.replace(/<br\/>/gim, "<br>");
- html = html.replace(/<p>/gim, "<p>");
- html = html.replace(/<\/p>/gim, "</p>");
- html = html.replace(/<b>/gim, "<b>");
- html = html.replace(/<\/b>/gim, "</b>");
- html = html.replace(/<strong>/gim, "<b>");
- html = html.replace(/<\/strong>/gim, "</b>");
- html = html.replace(/<i>/gim, "<i>");
- html = html.replace(/<\/i>/gim, "</i>");
- html = html.replace(/<emphasis>/gim, "<i>");
- html = html.replace(/<\/emphasis>/gim, "</i>");
- html = html.replace(/<u>/gim, "<u>");
- html = html.replace(/<\/u>/gim, "</u>");
- html = html.replace(/<dl>/gim, "<dl>");
- html = html.replace(/<\/dl>/gim, "</dl>");
- html = html.replace(/<dd>/gim, "<dd>");
- html = html.replace(/<\/dd>/gim, "</dd>");
- html = html.replace(/<dt>/gim, "<dt>");
- html = html.replace(/<\/dt>/gim, "</dt>");
- html = html.replace(/<div>/gim, "<div>");
- html = html.replace(/<\/div>/gim, "</div>");
-
-
- html = html.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gim, "");
- html = html.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gim, "");
-
-
- html = html.replace(/<(?:.|\s)*?>/gim, "");
-
-
- html = html.replace(/\<p\>/g, "<p>");
- html = html.replace(/\<\/p\>/g, "</p>");
- html = html.replace(/\<br\>/g, "<br>");
-
- html = html.replace(/\<b\>/g, "<b>");
- html = html.replace(/\<\/b\>/g, "</b>");
- html = html.replace(/\<i\>/g, "<i>");
- html = html.replace(/\<\/i\>/g, "</i>");
- html = html.replace(/\<u\>/g, "<u>");
- html = html.replace(/\<\/u\>/g, "</u>");
- html = html.replace(/\<li\>/g, "<li>");
- html = html.replace(/\<\/li\>/g, "</li>");
- html = html.replace(/\<ol\>/g, "<ol>");
- html = html.replace(/\<\/ol\>/g, "</ol>");
- html = html.replace(/\<ul\>/g, "<ul style='list-style-type:disc'>");
- html = html.replace(/\<\/ul\>/g, "</ul>");
- html = html.replace(/\<dl\>/g, "<dl>");
- html = html.replace(/\<\/dl\>/g, "</dl>");
- html = html.replace(/\<dd\>/g, "<dd>");
- html = html.replace(/\<\/dd\>/g, "</dd>");
- html = html.replace(/\<dt\>/g, "<dt>");
- html = html.replace(/\<\/dt\>/g, "</dt>");
- html = html.replace(/\<div\>/g, "<div>");
- html = html.replace(/\<\/div\>/g, "</div>");
-
-
- html = html.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\n\n");
-
-
- html = html.replace(/ +(?= )/g, " ");
-
- info.ReturnValue = html;
- }
|
|