openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

trumbowyg.cleanpaste.js (3661B)


      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 
     13 (function ($) {
     14     'use strict';
     15 
     16     function checkValidTags(snippet) {
     17         var theString = snippet;
     18 
     19         // Replace uppercase element names with lowercase
     20         theString = theString.replace(/<[^> ]*/g, function (match) {
     21             return match.toLowerCase();
     22         });
     23 
     24         // Replace uppercase attribute names with lowercase
     25         theString = theString.replace(/<[^>]*>/g, function (match) {
     26             match = match.replace(/ [^=]+=/g, function (match2) {
     27                 return match2.toLowerCase();
     28             });
     29             return match;
     30         });
     31 
     32         // Put quotes around unquoted attributes
     33         theString = theString.replace(/<[^>]*>/g, function (match) {
     34             match = match.replace(/( [^=]+=)([^"][^ >]*)/g, '$1\"$2\"');
     35             return match;
     36         });
     37 
     38         return theString;
     39     }
     40 
     41     function cleanIt(html) {
     42         // first make sure all tags and attributes are made valid
     43         html = checkValidTags(html);
     44 
     45         // Replace opening bold tags with strong
     46         html = html.replace(/<b(\s+|>)/g, '<strong$1');
     47         // Replace closing bold tags with closing strong
     48         html = html.replace(/<\/b(\s+|>)/g, '</strong$1');
     49 
     50         // Replace italic tags with em
     51         html = html.replace(/<i(\s+|>)/g, '<em$1');
     52         // Replace closing italic tags with closing em
     53         html = html.replace(/<\/i(\s+|>)/g, '</em$1');
     54 
     55         // strip out comments -cgCraft
     56         html = html.replace(/<!(?:--[\s\S]*?--\s*)?>\s*/g, '');
     57 
     58         // strip out &nbsp; -cgCraft
     59         html = html.replace(/&nbsp;/gi, ' ');
     60         // strip out extra spaces -cgCraft
     61         html = html.replace(/ <\//gi, '</');
     62 
     63         while (html.indexOf('  ') !== -1) {
     64             html = html.split('  ').join(' ');
     65         }
     66 
     67         // strip &nbsp; -cgCraft
     68         html = html.replace(/^\s*|\s*$/g, '');
     69 
     70         // Strip out unaccepted attributes
     71         html = html.replace(/<[^>]*>/g, function (match) {
     72             match = match.replace(/ ([^=]+)="[^"]*"/g, function (match2, attributeName) {
     73                 if (['alt', 'href', 'src', 'title'].indexOf(attributeName) !== -1) {
     74                     return match2;
     75                 }
     76                 return '';
     77             });
     78             return match;
     79         });
     80 
     81         // Final cleanout for MS Word crud
     82         html = html.replace(/<\?xml[^>]*>/g, '');
     83         html = html.replace(/<[^ >]+:[^>]*>/g, '');
     84         html = html.replace(/<\/[^ >]+:[^>]*>/g, '');
     85 
     86         // remove unwanted tags
     87         html = html.replace(/<(div|span|style|meta|link).*?>/gi, '');
     88 
     89         return html;
     90     }
     91 
     92     // clean editor
     93     // this will clean the inserted contents
     94     // it does a compare, before and after paste to determine the
     95     // pasted contents
     96     $.extend(true, $.trumbowyg, {
     97         plugins: {
     98             cleanPaste: {
     99                 init: function (trumbowyg) {
    100                     trumbowyg.pasteHandlers.push(function () {
    101                         try {
    102                             trumbowyg.$ed.html(cleanIt(trumbowyg.$ed.html()));
    103                         } catch (c) {
    104                         }
    105                     });
    106                 }
    107             }
    108         }
    109     });
    110 })(jQuery);
    111 
    112