Trumbowyg CleanPaste

HTML

<link rel="stylesheet" href="//cdn.rawgit.com/Alex-D/Trumbowyg/v2.5.1/dist/ui/trumbowyg.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="//cdn.rawgit.com/Alex-D/Trumbowyg/v2.5.1/dist/trumbowyg.min.js"></script>
<textarea id="editor1">This is a test.  Copy some large piece of text and 'paste' it after this line.  The cursor will reset to the first character.</textarea>

CSS

body {
  background-color: white;
  
}

JavaScript

/* ===========================================================
 * trumbowyg.cleanpaste.js v1.0
 * Font Clean paste plugin for Trumbowyg
 * http://alex-d.github.com/Trumbowyg
 * ===========================================================
 * Author : Eric Radin
 */

/**
 * This plugin will perform a "cleaning" on any paste, in particular
 * it will clean pasted content of microsoft word document tags and classes.
 */
(function ($) {
    'use strict';

    function reverse(sentString) {
        var theString = '';
        for (var i = sentString.length - 1; i >= 0; i -= 1) {
            theString += sentString.charAt(i);
        }
        return theString;
    }

    function checkValidTags(snippet) {
        var theString = snippet;

        // Replace uppercase element names with lowercase
        theString = theString.replace(/<[^> ]*/g, function (match) {
            return match.toLowerCase();
        });

        // Replace uppercase attribute names with lowercase
        theString = theString.replace(/<[^>]*>/g, function (match) {
            match = match.replace(/ [^=]+=/g, function (match2) {
                return match2.toLowerCase();
            });
            return match;
        });

        // Put quotes around unquoted attributes
        theString = theString.replace(/<[^>]*>/g, function (match) {
            match = match.replace(/( [^=]+=)([^"][^ >]*)/g, '$1\"$2\"');
            return match;
        });

        return theString;
    }

    function cleanIt(htmlBefore, htmlAfter) {
        var matchedHead = '';
        var matchedTail = '';
        var afterStart;
        var afterFinish;
        var newSnippet;

        // we need to extract the inserted block
        for (afterStart = 0; htmlAfter.charAt(afterStart) === htmlBefore.charAt(afterStart); afterStart += 1) {
            matchedHead += htmlAfter.charAt(afterStart);
        }

        // If afterStart is inside a HTML tag, move to opening brace of tag
        for (var i = afterStart;...