'use strict';

var warningChecker = new WarningChecker(WARNINGS);

var editableDivCount = 0;

var contentEditableDivs = [];

var observer = new MutationObserver(function(mutations) {
  if (mutations[0]) {
    mutations.forEach(function(mutation){
      if (mutation.type != 'characterData' && mutation.target.hasAttribute('contentEditable')) {
        id = mutation.target.id;
        if (id) {
          var targetDiv = document.getElementById(id);
          // generate input event to fire checkForWarnings again
          var inputEvent = new Event('input', {
              bubbles: true,
              cancelable: true,
          });
          targetDiv.dispatchEvent(inputEvent);
        }
      }
    });
  }
});

var addObserver = function() {
  warningChecker.addWarnings(this.parentNode);
  observer.observe(this, {characterData: true, subtree: true, childList: true,  attributes: true});
}

var removeObserver = function() {
  warningChecker.removeWarnings(this.parentNode);
  observer.disconnect();
}

var checkForWarnings = function() {
  warningChecker.removeWarnings(this.parentNode);
  warningChecker.addWarnings(this.parentNode);
}

var applyEventListeners = function(id) {
  var targetDiv = document.getElementById(id);
  targetDiv.addEventListener('focus', addObserver);
  targetDiv.addEventListener('input', checkForWarnings);
  targetDiv.addEventListener('blur', removeObserver);
}

var documentObserver = new MutationObserver(function(mutations) {
  var divCount = getEditableDivs().length;
  if (divCount != editableDivCount) {
    editableDivCount = divCount;
    var id;
    if (mutations[0]) {
      mutations.forEach(function(mutation){
        if (mutation.type == 'childList' && mutation.target.hasAttribute('contentEditable')) {
          id = mutation.target.id;
          if (id) {
            applyEventListeners(id);
          }
        }
      });
    }
  }
});

function getEditableDivs() {
  return document.querySelectorAll('div[contentEditable=true]');
}

documentObserver.observe(document, {characterData: true, subtree: true, childList: true});

@@ -59,17 +59,16 @@
 NINGS);%0A
-%0A
 var edit
@@ -90,39 +90,8 @@
 0;%0A%0A
-var contentEditableDivs = %5B%5D;%0A%0A
 var 
@@ -198,32 +198,33 @@
 nction(mutation)
+ 
 %7B%0A      if (muta
@@ -1623,16 +1623,17 @@
 utation)
+ 
 %7B%0A      
@@ -1993,37 +1993,16 @@
 ument, %7B
-characterData: true, 
 subtree:

Remove characterData from documentObserver, remove unused var, formatting cleanup