<commit_before>/**
 * @fileoverview Event helper functions.
 */

goog.provide('treesaver.events');

goog.require('treesaver.array');
goog.require('treesaver.constants');
goog.require('treesaver.debug'); // forEach

/**
 * Create an event and fire it
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {Object=} data
 */
treesaver.events.fireEvent = function(obj, type, data) {
  var e = document.createEvent('UIEvents'),
      cur,
      val;

  // TODO: Test cancelling
  e.initEvent(type, false, true);
  // Copy provided data into event object
  if (data) {
    for (cur in data) {
      e[cur] = data[cur];
    }
  }

  return obj.dispatchEvent(e);
};

/**
 * Add an event listener to an element
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {!function()|Object} fn
 */
treesaver.events.addListener = function(obj, type, fn) {
  // Help out while debugging, but don't pay the performance hit
  // for a try/catch in production
  if (goog.DEBUG) {
    try {
      obj.addEventListener(type, fn, false);
    }
    catch(ex) {
      treesaver.debug.error('Could not add ' + type + ' listener to: ' + obj);
      treesaver.debug.error('Exception ' + ex);
    }
  }
  else {
    obj.addEventListener(type, fn, false);
  }
};

/**
 * Remove an event listener from an element
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {!function()|Object} fn
 */
treesaver.events.removeListener = function(obj, type, fn) {
  // Help out with debugging, but only in debug
  if (goog.DEBUG) {
    try {
      obj.removeEventListener(type, fn, false);
    }
    catch (ex) {
      treesaver.debug.error('Could not remove ' + type + ' listener from: ' + obj);
      treesaver.debug.error('Exception ' + ex);
    }
  }
  else {
    obj.removeEventListener(type, fn, false);
  }
};

// Need to patch functions for IE
// HACK / TODO: IE9 beta has a Frankenstein model where addEventListener is present,
// but 'handleEvent' delegation does not work. Adding an IE-only exception for now.
// If IE9 ships with this annoying bug, will have revisit. Reference:
// https://connect.microsoft.com/IE/feedback/details/574560/addeventlistener-should-support-objects-implementing-the-eventlistener-interface
if (SUPPORT_IE && (!('addEventListener' in document) || document.documentMode === 9)) {<commit_msg>Remove hack that used IE event model in IE9. Hurray MSFT for fixing that Beta bug<commit_after>/**
 * @fileoverview Event helper functions.
 */

goog.provide('treesaver.events');

goog.require('treesaver.array');
goog.require('treesaver.constants');
goog.require('treesaver.debug'); // forEach

/**
 * Create an event and fire it
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {Object=} data
 */
treesaver.events.fireEvent = function(obj, type, data) {
  var e = document.createEvent('UIEvents'),
      cur,
      val;

  // TODO: Test cancelling
  e.initEvent(type, false, true);
  // Copy provided data into event object
  if (data) {
    for (cur in data) {
      e[cur] = data[cur];
    }
  }

  return obj.dispatchEvent(e);
};

/**
 * Add an event listener to an element
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {!function()|Object} fn
 */
treesaver.events.addListener = function(obj, type, fn) {
  // Help out while debugging, but don't pay the performance hit
  // for a try/catch in production
  if (goog.DEBUG) {
    try {
      obj.addEventListener(type, fn, false);
    }
    catch(ex) {
      treesaver.debug.error('Could not add ' + type + ' listener to: ' + obj);
      treesaver.debug.error('Exception ' + ex);
    }
  }
  else {
    obj.addEventListener(type, fn, false);
  }
};

/**
 * Remove an event listener from an element
 *
 * @param {!*} obj
 * @param {!string} type
 * @param {!function()|Object} fn
 */
treesaver.events.removeListener = function(obj, type, fn) {
  // Help out with debugging, but only in debug
  if (goog.DEBUG) {
    try {
      obj.removeEventListener(type, fn, false);
    }
    catch (ex) {
      treesaver.debug.error('Could not remove ' + type + ' listener from: ' + obj);
      treesaver.debug.error('Exception ' + ex);
    }
  }
  else {
    obj.removeEventListener(type, fn, false);
  }
};

// Need to patch functions for IE
if (SUPPORT_IE && (!('addEventListener' in document))) {
{'fraction_non_alphanumeric': 0.15090354376906828, 'fraction_numerical': 0.002346866932644919, 'mean_word_length': 3.12984496124031, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4847467', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1351, 'n_words': 389}