/*!

* froala_editor v2.3.3 (https://www.froala.com/wysiwyg-editor)
* License https://froala.com/wysiwyg-editor/terms/
* Copyright 2014-2016 Froala Labs
*/

(function (factory) {

if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
    // Node/CommonJS
    module.exports = function( root, jQuery ) {
        if ( jQuery === undefined ) {
            // require('jQuery') returns a factory that requires window to
            // build a jQuery instance, we normalize how we use modules
            // that require this pattern but the window provided is a noop
            // if it's defined (how jquery works)
            if ( typeof window !== 'undefined' ) {
                jQuery = require('jquery');
            }
            else {
                jQuery = require('jquery')(root);
            }
        }
        factory(jQuery);
        return jQuery;
    };
} else {
    // Browser globals
    factory(jQuery);
}

}(function ($) {

'use strict';

// Extend defaults.
$.extend($.FE.DEFAULTS, {
  entities: '&<>"'¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿŒœŠšŸƒˆ˜ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩαβγδεζηθικλμνξοπρςστυφχψωϑϒϖ   ‌‍‎‏–—‘’‚“”„†‡•…‰′″‹›‾⁄€ℑ℘ℜ™ℵ←↑→↓↔↵⇐⇑⇒⇓⇔∀∂∃∅∇∈∉∋∏∑−∗√∝∞∠∧∨∩∪∫∴∼≅≈≠≡≤≥⊂⊃⊄⊆⊇⊕⊗⊥⋅⌈⌉⌊⌋⟨⟩◊♠♣♥♦'
});

$.FE.PLUGINS.entities = function (editor) {
  var _reg_exp;
  var _map;

  function _process(el) {
    var text = el.textContent;
    if (text.match(_reg_exp)) {
      var new_text = '';
      for (var j = 0; j < text.length; j++) {
        if (_map[text[j]]) new_text += _map[text[j]];
        else new_text += text[j];
      }
      el.textContent = new_text;
    }
  }

  function _encode (el) {
    if (el && ['STYLE', 'SCRIPT'].indexOf(el.tagName) >= 0) return true;

    var contents = editor.node.contents(el);

    for (var i = 0; i < contents.length; i++) {
      if (contents[i].nodeType == Node.TEXT_NODE) {
        _process(contents[i]);
      }
      else {
        _encode(contents[i]);
      }
    }

    if (el.nodeType == Node.TEXT_NODE) _process(el);
  }

  /**
   * Encode entities.
   */
  function _encodeEntities (html) {
    if (html.length === 0) return '';

    return editor.clean.exec(html, _encode);
  }

  /*
   * Initialize.
   */
  function _init () {
    if (editor.opts.htmlSimpleAmpersand) {
      editor.opts.entities = editor.opts.entities.replace('&amp;', '');
    }

    // Do escape.
    var entities_text = $('<div>').html(editor.opts.entities).text();
    var entities_array = editor.opts.entities.split(';');
    _map = {};
    _reg_exp = '';
    for (var i = 0; i < entities_text.length; i++) {
      var chr = entities_text.charAt(i);
      _map[chr] = entities_array[i] + ';';
      _reg_exp += '\\' + chr + (i < entities_text.length - 1 ? '|' : '');
    }
    _reg_exp = new RegExp('(' + _reg_exp + ')', 'g');

    editor.events.on('html.get', _encodeEntities, true);
  }

  return {
    _init: _init
  }
}

}));