"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

/*
 * Copyright (C) 2017 - present Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* this class is a solution to CNVS-37129. this solution will create DOM
 * MutationObserver to all tables created in tinymce. it does this by hooking
 * the tinymce editor object's "AddVisual" method. this method gets called
 * often and is called internally by tinymce in a way that hooking it will
 * allow us to catch all cases where tables are created.
 * the hook simply looks at all tables currently created in the tinymce
 * editor and adds a MutationObserver on that table for when any subtree
 * changes are made. when this occurs, that code will figure out if there are
 * any '<td><iframe>' conditions in the table. if there are, the code inserts
 * a div to make it '<td><div><iframe>'
 * ultimately, we need to get this fixed in tinymce, but this hack will resolve
 * the customer issue now in the short term. 
 */
var IframesTableFix =
/*#__PURE__*/
function () {
  function IframesTableFix() {
    (0, _classCallCheck2.default)(this, IframesTableFix);
  }

  (0, _createClass2.default)(IframesTableFix, [{
    key: "getHackedTables",
    value: function getHackedTables(editor) {
      return editor.hackedTables || [];
    }
  }, {
    key: "setHackedTables",
    value: function setHackedTables(editor, hackedTables) {
      editor.hackedTables = hackedTables;
    }
  }, {
    key: "cleanHackedTables",
    value: function cleanHackedTables(editor) {
      var hackedTables = this.getHackedTables(editor);
      var tables = editor.dom.select("table");
      this.setHackedTables(editor, hackedTables.filter(function (t) {
        return tables.indexOf(t) > -1;
      }));
    }
  }, {
    key: "isTableHacked",
    value: function isTableHacked(editor, table) {
      this.cleanHackedTables(editor);
      return this.getHackedTables(editor).indexOf(table) > -1;
    }
  }, {
    key: "addHackedTable",
    value: function addHackedTable(editor, table) {
      this.getHackedTables(editor).push(table);
    }
  }, {
    key: "fixIframes",
    value: function fixIframes(editor) {
      var tds = editor && editor.dom && editor.dom.select ? editor.dom.select("td") : [];
      var brokenTds = [];
      tds.forEach(function (td) {
        var spanChildren = [].slice.call(td.children).filter(function (n) {
          return n.tagName === "SPAN" && n.getAttribute("data-mce-object") === "iframe";
        });

        if (spanChildren.length > 0) {
          if (brokenTds.indexOf(td) === -1) {
            td.innerHTML = "<div>".concat(td.innerHTML, "</div>");
            brokenTds.push(td);
          }
        }
      });
    }
  }, {
    key: "addMutationObserverToTables",
    value: function addMutationObserverToTables(editor, MutationObserver) {
      var _this = this;

      var tables = editor && editor.dom && editor.dom.select ? editor.dom.select("table").filter(function (t) {
        return !_this.isTableHacked(editor, t);
      }) : [];

      if (tables.length > 0) {
        var mo = new MutationObserver(function () {
          _this.fixIframes(editor);
        });

        for (var i = tables.length - 1; i >= 0; i--) {
          var table = tables[i];
          mo.observe(table, {
            childList: true,
            subtree: true
          });
          this.addHackedTable(editor, table);
        }
      }

      this.fixIframes(editor);
    }
  }, {
    key: "hookAddVisual",
    value: function hookAddVisual(editor, MutationObserver) {
      var _this2 = this;

      var addVisual = editor.addVisual.bind(editor);

      editor.addVisual = function newAddVisual(elm) {
        _this2.addMutationObserverToTables(editor, MutationObserver);

        addVisual(elm);
      }.bind(editor);

      this.addMutationObserverToTables(editor, MutationObserver);
    }
  }]);
  return IframesTableFix;
}();

exports.default = IframesTableFix;