Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. // constant
  2. // --------------------
  3. // Needed for personalization tags
  4. const BEGIN = "<" + "%";
  5. const END = "%" + ">";
  6.  
  7. // stringDefined
  8. // --------------------
  9. // Check if a values is set or a field is set
  10. function stringDefined(str) {
  11. return str != undefined && str != null && str != "" && str != 0
  12. }
  13.  
  14. // Check if content is empty
  15. function getSomeOrEmpty(str) {
  16. if (stringDefined(str)) {
  17. return str;
  18. }
  19. return "";
  20. }
  21.  
  22. // removeHTMLTags
  23. // --------------
  24. // Transforms HTML content into Text
  25. // Very useful when you use a wysiwyg editor in a content template to generate
  26. // the text version
  27. // It actually convert some HTML entities, removes most HTML tags except the following:
  28. // * <br> -> \n
  29. // * <p> -> \n
  30. // * <li> -> -
  31. // * <a href="http://url.com">My Link </a> -> My Link (http://url.com)
  32. // [in] sContentArg : the HTML content
  33. // [out] returns the text
  34. //
  35. function removeHTMLTags(sContentArg) {
  36. var sContent = ""+sContentArg
  37.  
  38. // These tags require special attention
  39. sContent = sContent.replace(/<a\s.*?href\s*=\s*"(.*?)".*?>(.*?)<\/a>/ig, '$2 ($1)');
  40. sContent = sContent.replace(/<li\s.*?>/ig, '- ');
  41. sContent = sContent.replace(/<br\s*\/?>/ig, '\n');
  42. sContent = sContent.replace(/<p(\s+.*?)?>/ig, '\n');
  43.  
  44. // Replace common entities
  45. sContent = sContent.replace(/&amp;/ig, '&');
  46. sContent = sContent.replace(/&lt;/ig, '<');
  47. sContent = sContent.replace(/&gt;/ig, '>');
  48. sContent = sContent.replace(/&quot;/ig, '"');
  49. sContent = sContent.replace(/&#064;/ig, '@');
  50. sContent = sContent.replace(/&copy;/ig, '(c)');
  51. sContent = sContent.replace(/&reg;/ig, '(R)');
  52. sContent = sContent.replace(/&nbsp;/ig, ' ');
  53.  
  54. // remove all other html tag
  55. sContent = sContent.replace(/\<[\w\/\?][\w\s\"\'\:\\\/\-\%\?\&\;\,\.\=\!\#]*\>/gi,"")
  56.  
  57. //remove inline style
  58. sContent = sContent.replace(/<.*style=".*".*>/gi,"");
  59.  
  60. return sContent
  61. }
  62.  
  63. // formatRichTextFields
  64. // --------------------
  65. // For use on fields using the Neolane Rich Text editor.
  66. //
  67. // * Checks the field for bad Word Processor markup.
  68. // * Removes some of the markup created by the Rich Text editor, that are either
  69. // bad for Email Rendering or will mess up the Email Content Template design.
  70. // * Replaces the useful parts of the Rich Text markup with more Email Client/XHTML
  71. // friendly alternatives.
  72.  
  73. function formatRichTextFields(sContentArg) {
  74. var sContent = ""+sContentArg
  75.  
  76. // First check if text is pasted directly from MS Word or similar
  77. if((sContent.search("Mso") != -1) || (sContent.search("mso-") != -1) || (sContent.search("class\=") != -1) || (sContent.search("MARGIN") != -1) || (sContent.search(" 0cm") != -1)){
  78. 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:");
  79. }
  80.  
  81. //Remove <pre> tags
  82. sContent = sContent.replace(/<pre>]*>/ig, '').replace(/<\/pre>/ig, '');
  83.  
  84. //Correct <br />, <a>, <em> and <strong>
  85. sContent = sContent.replace(/<br>/ig, '<br />');
  86. sContent = sContent.replace(/<a/ig, '<a').replace(/<\/a>/ig, '</a>');
  87. sContent = sContent.replace(/<em/ig, '<em').replace(/<\/em>/ig, '</em>');
  88. sContent = sContent.replace(/<strong/ig, '<strong').replace(/<\/strong>/ig, '</strong>');
  89.  
  90. //Replace custom headers with <strong>
  91. sContent = sContent.replace(/<h1>/ig, '<strong>').replace(/<\/h1>/ig, '</strong><br />');
  92. sContent = sContent.replace(/<h2>/ig, '<strong>').replace(/<\/h2>/ig, '</strong><br />');
  93. sContent = sContent.replace(/<h3>/ig, '<strong>').replace(/<\/h3>/ig, '</strong><br />');
  94. sContent = sContent.replace(/<h4>/ig, '<strong>').replace(/<\/h4>/ig, '</strong><br />');
  95. sContent = sContent.replace(/<h5>/ig, '<strong>').replace(/<\/h5>/ig, '</strong><br />');
  96. sContent = sContent.replace(/<h6>/ig, '<strong>').replace(/<\/h6>/ig, '</strong><br />');
  97.  
  98. //Remove the last <p> and </p> with no replacement
  99. sContent = sContent.replace(/<p[^>]*>$/i, '').replace(/<\/p>$/i, '');
  100.  
  101. //Replace other <p> and </p>'s with two line breaks
  102. sContent = sContent.replace(/<p[^>]*>/ig, '').replace(/<\/p>/ig, '<br /><br />');
  103.  
  104. //Remove <ul> and <ol> tags, put <br /> after
  105. sContent = sContent.replace(/<ul>/ig, '').replace(/<\/ul>/ig, '<br />');
  106. sContent = sContent.replace(/<ol>/ig, '').replace(/<\/ol>/ig, '<br />');
  107.  
  108. //Remove </li>, and replace last <li> with &bull; only
  109. sContent = sContent.replace(/<\/li>/ig, '');
  110. sContent = sContent.replace(/<li>([^\n]*)$/ig, '&bull; $1');
  111.  
  112. //Replace <li> and with &bull; and line break after
  113. sContent = sContent.replace(/<li>([^\n]*)/ig, '&bull; $1<br />');
  114.  
  115. //Remove font face=xyz and size=n
  116. sContent = sContent.replace(/ face\=[^>]*>/ig, '>');
  117. sContent = sContent.replace(/ size\=(\d+)/ig, '');
  118.  
  119. //Replace <font color=#xnxnxn> and </font> with <span style="color:#xnxnxn"> and </span>
  120. sContent = sContent.replace(/ color\=#([^>]*)/ig, ' style=\"color:#$1\;\"');
  121.  
  122. //Replace <font> with <span>
  123. sContent = sContent.replace(/<font/ig, '<span').replace(/<\/font>/ig, '</span>');
  124.  
  125. return sContent
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement