trumbowyg.cleanpaste.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* ===========================================================
  2. * trumbowyg.cleanpaste.js v1.0
  3. * Font Clean paste plugin for Trumbowyg
  4. * http://alex-d.github.com/Trumbowyg
  5. * ===========================================================
  6. * Authors : Eric Radin
  7. * Todd Graham (slackwalker)
  8. *
  9. * This plugin will perform a "cleaning" on any paste, in particular
  10. * it will clean pasted content of microsoft word document tags and classes.
  11. */
  12. (function ($) {
  13. 'use strict';
  14. function checkValidTags(snippet) {
  15. var theString = snippet;
  16. // Replace uppercase element names with lowercase
  17. theString = theString.replace(/<[^> ]*/g, function (match) {
  18. return match.toLowerCase();
  19. });
  20. // Replace uppercase attribute names with lowercase
  21. theString = theString.replace(/<[^>]*>/g, function (match) {
  22. match = match.replace(/ [^=]+=/g, function (match2) {
  23. return match2.toLowerCase();
  24. });
  25. return match;
  26. });
  27. // Put quotes around unquoted attributes
  28. theString = theString.replace(/<[^>]*>/g, function (match) {
  29. match = match.replace(/( [^=]+=)([^"][^ >]*)/g, '$1\"$2\"');
  30. return match;
  31. });
  32. return theString;
  33. }
  34. function cleanIt(html) {
  35. // first make sure all tags and attributes are made valid
  36. html = checkValidTags(html);
  37. // Replace opening bold tags with strong
  38. html = html.replace(/<b(\s+|>)/g, '<strong$1');
  39. // Replace closing bold tags with closing strong
  40. html = html.replace(/<\/b(\s+|>)/g, '</strong$1');
  41. // Replace italic tags with em
  42. html = html.replace(/<i(\s+|>)/g, '<em$1');
  43. // Replace closing italic tags with closing em
  44. html = html.replace(/<\/i(\s+|>)/g, '</em$1');
  45. // strip out comments -cgCraft
  46. html = html.replace(/<!(?:--[\s\S]*?--\s*)?>\s*/g, '');
  47. // strip out &nbsp; -cgCraft
  48. html = html.replace(/&nbsp;/gi, ' ');
  49. // strip out extra spaces -cgCraft
  50. html = html.replace(/ <\//gi, '</');
  51. while (html.indexOf(' ') !== -1) {
  52. html = html.split(' ').join(' ');
  53. }
  54. // strip &nbsp; -cgCraft
  55. html = html.replace(/^\s*|\s*$/g, '');
  56. // Strip out unaccepted attributes
  57. html = html.replace(/<[^>]*>/g, function (match) {
  58. match = match.replace(/ ([^=]+)="[^"]*"/g, function (match2, attributeName) {
  59. if (['alt', 'href', 'src', 'title'].indexOf(attributeName) !== -1) {
  60. return match2;
  61. }
  62. return '';
  63. });
  64. return match;
  65. });
  66. // Final cleanout for MS Word crud
  67. html = html.replace(/<\?xml[^>]*>/g, '');
  68. html = html.replace(/<[^ >]+:[^>]*>/g, '');
  69. html = html.replace(/<\/[^ >]+:[^>]*>/g, '');
  70. // remove unwanted tags
  71. html = html.replace(/<(div|span|style|meta|link).*?>/gi, '');
  72. return html;
  73. }
  74. // clean editor
  75. // this will clean the inserted contents
  76. // it does a compare, before and after paste to determine the
  77. // pasted contents
  78. $.extend(true, $.trumbowyg, {
  79. plugins: {
  80. cleanPaste: {
  81. init: function (trumbowyg) {
  82. trumbowyg.pasteHandlers.push(function () {
  83. setTimeout(function () {
  84. try {
  85. trumbowyg.$ed.html(cleanIt(trumbowyg.$ed.html()));
  86. } catch (c) {
  87. }
  88. }, 0);
  89. });
  90. }
  91. }
  92. }
  93. });
  94. })(jQuery);