1
0

trumbowyg.cleanpaste.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Remove multiple spaces
  52. html.replace(/\s+/g, ' ');
  53. // strip &nbsp; -cgCraft
  54. html = html.replace(/^\s*|\s*$/g, '');
  55. // Strip out unaccepted attributes
  56. html = html.replace(/<[^>]*>/g, function (match) {
  57. match = match.replace(/ ([^=]+)="[^"]*"/g, function (match2, attributeName) {
  58. if (['alt', 'href', 'src', 'title'].indexOf(attributeName) !== -1) {
  59. return match2;
  60. }
  61. return '';
  62. });
  63. return match;
  64. });
  65. // Final clean out for MS Word crud
  66. html = html.replace(/<\?xml[^>]*>/g, '');
  67. html = html.replace(/<[^ >]+:[^>]*>/g, '');
  68. html = html.replace(/<\/[^ >]+:[^>]*>/g, '');
  69. // remove unwanted tags
  70. html = html.replace(/<(div|span|style|meta|link).*?>/gi, '');
  71. return html;
  72. }
  73. // clean editor
  74. // this will clean the inserted contents
  75. // it does a compare, before and after paste to determine the
  76. // pasted contents
  77. $.extend(true, $.trumbowyg, {
  78. plugins: {
  79. cleanPaste: {
  80. init: function (trumbowyg) {
  81. trumbowyg.pasteHandlers.push(function (pasteEvent) {
  82. setTimeout(function () {
  83. try {
  84. trumbowyg.saveRange();
  85. var clipboardData = (pasteEvent.originalEvent || pasteEvent).clipboardData,
  86. pastedData = clipboardData.getData('Text'),
  87. node = trumbowyg.doc.getSelection().focusNode,
  88. range = trumbowyg.doc.createRange(),
  89. cleanedPaste = cleanIt(pastedData.trim()),
  90. newNode = $(cleanedPaste)[0] || trumbowyg.doc.createTextNode(cleanedPaste);
  91. if (trumbowyg.$ed.html() === '') {
  92. // simply append if there is no content in editor
  93. trumbowyg.$ed[0].appendChild(newNode);
  94. } else {
  95. // insert pasted content behind last focused node
  96. range.setStartAfter(node);
  97. range.setEndAfter(node);
  98. trumbowyg.doc.getSelection().removeAllRanges();
  99. trumbowyg.doc.getSelection().addRange(range);
  100. trumbowyg.range.insertNode(newNode);
  101. }
  102. // now set cursor right after pasted content
  103. range = trumbowyg.doc.createRange();
  104. range.setStartAfter(newNode);
  105. range.setEndAfter(newNode);
  106. trumbowyg.doc.getSelection().removeAllRanges();
  107. trumbowyg.doc.getSelection().addRange(range);
  108. // prevent defaults
  109. pasteEvent.stopPropagation();
  110. pasteEvent.preventDefault();
  111. // save new node as focused node
  112. trumbowyg.saveRange();
  113. trumbowyg.syncCode();
  114. trumbowyg.$c.trigger('tbwchange');
  115. } catch (c) {
  116. }
  117. }, 0);
  118. });
  119. }
  120. }
  121. }
  122. });
  123. })(jQuery);