Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // constant
- // --------------------
- // Needed for personalization tags
- const BEGIN = "<" + "%";
- const END = "%" + ">";
- // stringDefined
- // --------------------
- // Check if a values is set or a field is set
- function stringDefined(str) {
- return str != undefined && str != null && str != "" && str != 0
- }
- // Check if content is empty
- function getSomeOrEmpty(str) {
- if (stringDefined(str)) {
- return str;
- }
- return "";
- }
- // removeHTMLTags
- // --------------
- // Transforms HTML content into Text
- // Very useful when you use a wysiwyg editor in a content template to generate
- // the text version
- // It actually convert some HTML entities, removes most HTML tags except the following:
- // * <br> -> \n
- // * <p> -> \n
- // * <li> -> -
- // * <a href="http://url.com">My Link </a> -> My Link (http://url.com)
- // [in] sContentArg : the HTML content
- // [out] returns the text
- //
- function removeHTMLTags(sContentArg) {
- var sContent = ""+sContentArg
- // These tags require special attention
- sContent = sContent.replace(/<a\s.*?href\s*=\s*"(.*?)".*?>(.*?)<\/a>/ig, '$2 ($1)');
- sContent = sContent.replace(/<li\s.*?>/ig, '- ');
- sContent = sContent.replace(/<br\s*\/?>/ig, '\n');
- sContent = sContent.replace(/<p(\s+.*?)?>/ig, '\n');
- // Replace common entities
- sContent = sContent.replace(/&/ig, '&');
- sContent = sContent.replace(/</ig, '<');
- sContent = sContent.replace(/>/ig, '>');
- sContent = sContent.replace(/"/ig, '"');
- sContent = sContent.replace(/@/ig, '@');
- sContent = sContent.replace(/©/ig, '(c)');
- sContent = sContent.replace(/®/ig, '(R)');
- sContent = sContent.replace(/ /ig, ' ');
- // remove all other html tag
- sContent = sContent.replace(/\<[\w\/\?][\w\s\"\'\:\\\/\-\%\?\&\;\,\.\=\!\#]*\>/gi,"")
- //remove inline style
- sContent = sContent.replace(/<.*style=".*".*>/gi,"");
- return sContent
- }
- // formatRichTextFields
- // --------------------
- // For use on fields using the Neolane Rich Text editor.
- //
- // * Checks the field for bad Word Processor markup.
- // * Removes some of the markup created by the Rich Text editor, that are either
- // bad for Email Rendering or will mess up the Email Content Template design.
- // * Replaces the useful parts of the Rich Text markup with more Email Client/XHTML
- // friendly alternatives.
- function formatRichTextFields(sContentArg) {
- var sContent = ""+sContentArg
- // First check if text is pasted directly from MS Word or similar
- if((sContent.search("Mso") != -1) || (sContent.search("mso-") != -1) || (sContent.search("class\=") != -1) || (sContent.search("MARGIN") != -1) || (sContent.search(" 0cm") != -1)){
- logError ("HTML Error:\nIt seems you have pasted text into the Content Text field directly from\nMicrosoft Word, Outlook or a similar software. The text contains formattings\nthat will make the Email rendering bad in a lot of Email clients.\n\nPlease, before pasting text into the Content Text field, always paste all\ntext into Notepad (or similar) first, copy the text from there, and then\npaste into the Content Text field.\n\n\nDetailed Error:");
- }
- //Remove <pre> tags
- sContent = sContent.replace(/<pre>]*>/ig, '').replace(/<\/pre>/ig, '');
- //Correct <br />, <a>, <em> and <strong>
- sContent = sContent.replace(/<br>/ig, '<br />');
- sContent = sContent.replace(/<a/ig, '<a').replace(/<\/a>/ig, '</a>');
- sContent = sContent.replace(/<em/ig, '<em').replace(/<\/em>/ig, '</em>');
- sContent = sContent.replace(/<strong/ig, '<strong').replace(/<\/strong>/ig, '</strong>');
- //Replace custom headers with <strong>
- sContent = sContent.replace(/<h1>/ig, '<strong>').replace(/<\/h1>/ig, '</strong><br />');
- sContent = sContent.replace(/<h2>/ig, '<strong>').replace(/<\/h2>/ig, '</strong><br />');
- sContent = sContent.replace(/<h3>/ig, '<strong>').replace(/<\/h3>/ig, '</strong><br />');
- sContent = sContent.replace(/<h4>/ig, '<strong>').replace(/<\/h4>/ig, '</strong><br />');
- sContent = sContent.replace(/<h5>/ig, '<strong>').replace(/<\/h5>/ig, '</strong><br />');
- sContent = sContent.replace(/<h6>/ig, '<strong>').replace(/<\/h6>/ig, '</strong><br />');
- //Remove the last <p> and </p> with no replacement
- sContent = sContent.replace(/<p[^>]*>$/i, '').replace(/<\/p>$/i, '');
- //Replace other <p> and </p>'s with two line breaks
- sContent = sContent.replace(/<p[^>]*>/ig, '').replace(/<\/p>/ig, '<br /><br />');
- //Remove <ul> and <ol> tags, put <br /> after
- sContent = sContent.replace(/<ul>/ig, '').replace(/<\/ul>/ig, '<br />');
- sContent = sContent.replace(/<ol>/ig, '').replace(/<\/ol>/ig, '<br />');
- //Remove </li>, and replace last <li> with • only
- sContent = sContent.replace(/<\/li>/ig, '');
- sContent = sContent.replace(/<li>([^\n]*)$/ig, '• $1');
- //Replace <li> and with • and line break after
- sContent = sContent.replace(/<li>([^\n]*)/ig, '• $1<br />');
- //Remove font face=xyz and size=n
- sContent = sContent.replace(/ face\=[^>]*>/ig, '>');
- sContent = sContent.replace(/ size\=(\d+)/ig, '');
- //Replace <font color=#xnxnxn> and </font> with <span style="color:#xnxnxn"> and </span>
- sContent = sContent.replace(/ color\=#([^>]*)/ig, ' style=\"color:#$1\;\"');
- //Replace <font> with <span>
- sContent = sContent.replace(/<font/ig, '<span').replace(/<\/font>/ig, '</span>');
- return sContent
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement