<commit_before>/*!
 * jQuery replaceText
 *
 * Copyright 2015 <NAME> <<EMAIL>>
 * This program is distributed under the terms of the MIT license.
 * Please see the LICENSE file for details.
 */
(function($){
  /**
   * Replace substrings with HTML.
   *
   * @param search A string or regular expression.
   * @param replace A string, or a DOM node, or a jQuery object,
   *                or an array of any of the above,
   *                or a function that returns any of the above.
   *                The function will receive the group captures as arguments.
   * @return the original jQuery object.
   */
  $.fn.replaceText = function(search, replace) {
    // This will be /undefined|/ for strings, with 0 groups.
    var capturing = RegExp(search.source + '|').exec('').length - 1;
    // Convert a static replacement value into a function that returns it.
    var rep = (typeof replace === 'function') ? replace : function() { return replace; };

    return this.each(function() {
      var remove = [];
      for (var node = this.firstChild; node; node = node.nextSibling)
        if (node.nodeType == document.TEXT_NODE)
          if (textNode(node, search, rep, capturing))
            remove.push(node);
      $(remove).remove();
    });
  }

  /**
   * Helper function for processing a text node.
   *
   * The new content is inserted into the node's parent just before the node,
   * leaving everything after it unchanged to preserve the sibling traversal.
   *
   * @param node The text node being processed.
   * @param search The original search argument.
   * @param replace The replacing function.
   * @param capturing The number of capturing groups of the search pattern.
   * @return true or false, depending on whether any matches were found.
   */
  var textNode = function(node, search, replace, capturing) {
    var tokens = node.nodeValue.split(search);
    if (tokens.length < 2) return false;

    // Render the matches and concatenate everything.
    var output = [];
    for (var i = 0; i+capturing+1 < tokens.length; i += capturing+1) {
      var child = replace.apply(this, tokens.slice(i+1, i+1+capturing)) || '';
      output = output.concat(tokens[i], child);
    }
    output.push(tokens[tokens.length-1]);

    // Combine runs of strings into text nodes.
    var nodes = [];
    var text = '';
    for (var i in output) {
      if (typeof output[i] === 'string') text += output[i];
      else {
        if (text) nodes.push(document.createTextNode(text));
        nodes.push(output[i].clone());
        text = '';
      }
    }
    if (text) nodes.push(document.createTextNode(text));

    // Prepend all nodes to the given node, which will be marked for removal.
    return $(node).before(nodes) && true;
  };
})(jQuery);<commit_msg>Fix #2: Only clone as needed. Also clone events.<commit_after>/*!
 * jQuery replaceText
 *
 * Copyright 2015 <NAME> <<EMAIL>>
 * This program is distributed under the terms of the MIT license.
 * Please see the LICENSE file for details.
 */
(function($){
  /**
   * Replace substrings with HTML.
   *
   * @param search A string or regular expression.
   * @param replace A string, or a DOM node, or a jQuery object,
   *                or an array of any of the above,
   *                or a function that returns any of the above.
   *                The function will receive the group captures as arguments.
   * @return the original jQuery object.
   */
  $.fn.replaceText = function(search, replace) {
    // This will be /undefined|/ for strings, with 0 groups.
    var capturing = RegExp(search.source + '|').exec('').length - 1;
    // Convert a static replacement value into a function that returns it.
    var rep = (typeof replace === 'function') ? replace : function() { return replace; };

    return this.each(function() {
      var remove = [];
      for (var node = this.firstChild; node; node = node.nextSibling)
        if (node.nodeType == document.TEXT_NODE)
          if (textNode(node, search, rep, capturing, typeof replace !== 'function'))
            remove.push(node);
      $(remove).remove();
    });
  }

  /**
   * Helper function for processing a text node.
   *
   * The new content is inserted into the node's parent just before the node,
   * leaving everything after it unchanged to preserve the sibling traversal.
   *
   * @param node The text node being processed.
   * @param search The original search argument.
   * @param replace The replacing function.
   * @param capturing The number of capturing groups of the search pattern.
   * @param clone Whether the replaced elements must be cloned before insertion.
   * @return true or false, depending on whether any matches were found.
   */
  var textNode = function(node, search, replace, capturing, clone) {
    var tokens = node.nodeValue.split(search);
    if (tokens.length < 2) return false;

    // Render the matches and concatenate everything.
    var output = [];
    for (var i = 0; i+capturing+1 < tokens.length; i += capturing+1) {
      var child = replace.apply(this, tokens.slice(i+1, i+1+capturing)) || '';
      output = output.concat(tokens[i], child);
    }
    output.push(tokens[tokens.length-1]);

    // Combine runs of strings into text nodes.
    var nodes = [];
    var text = '';
    for (var i in output) {
      if (typeof output[i] === 'string') text += output[i];
      else {
        if (text) nodes.push(document.createTextNode(text));
        nodes.push(clone ? output[i].clone(true) : output[i]);
        text = '';
      }
    }
    if (text) nodes.push(document.createTextNode(text));

    // Prepend all nodes to the given node, which will be marked for removal.
    return $(node).before(nodes) && true;
  };
})(jQuery);
{'fraction_non_alphanumeric': 0.11832460732984293, 'fraction_numerical': 0.004712041884816754, 'mean_word_length': 2.702196382428941, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '1419516', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1572, 'n_words': 614}