<commit_before>// RegExp test cases here: http://regexr.com/3go12

function injectHTML() {
  // select everything in the body except <script>, <style>, <a>, <code>, <pre>
  const elements = document.querySelectorAll('body *:not(script):not(style):not(a):not(code):not(pre)');

  for (let i = 0; i < elements.length; i += 1) {
    if (elements[i].hasChildNodes()) {
      elements[i].childNodes.forEach((el) => {
        if (el.nodeType === Node.TEXT_NODE && el.nodeValue.match(/(\()([A-Z .,!?:"'`\\/&-]{10,100})\w*(\))/ig)) {

          const text = el.nodeValue.substring(el.nodeValue.indexOf('(') + 1, el.nodeValue.indexOf(')'));
          const regEx = new RegExp(text.replace(/[.,!?:"'`\\/&-()]/g, '\\$&')); // escape allowed characters + ()

          // data-bracketless=""  some symbols break layout, replace/change RegExp allowed symbols

          /* eslint-disable no-param-reassign */
          const htmlSafeText = text.replace(/&/g, '&amp;').replace(/"/g, "'");
          el.parentNode.innerHTML = el.parentNode.innerHTML
            .replace(regEx, `<bracket-less data-bracketless="${htmlSafeText}">${text}</bracket-less>`);
          /* eslint-enable no-param-reassign */
        }
      });
    }
  }
  // so that script will be injected once
  return true;
}
injectHTML();

// BUGS
// ignores text: "... (i.e., $99.99), ... (plus taxes) to buy it."
// @ https://github.com/getify/You-Dont-Know-JS/blob/master/up%20%26%20going/ch1.md#values--types

// (see Chapter 2). Loops (see "Loops")
// incorrectly grabs text: "see Chapter 2", and "). Loops ("
// @ https://github.com/getify/You-Dont-Know-JS/blob/master/up%20%26%20going/ch1.md#loops and go up
<commit_msg>Prepare for use with options and clean up<commit_after>// RegExp test cases here: http://regexr.com/3gqin

function injectHTML() {
  // select everything in the body except <script>, <style>, <a>, <code>, <pre>
  const elements = document.querySelectorAll('body *:not(script):not(style):not(a):not(code):not(pre)');

  const userLowerLimit = 10;
  const userUpperLimit = 100;
  // eslint-disable-next-line no-useless-escape
  const generatedRegexTest = `(\\()([A-Z .,!?:"'\`\\\/&-]{${userLowerLimit},${userUpperLimit}})\\w*(\\))`;
  // generatedRegexTest =  /(\()([A-Z .,!?:"'`\\\/&-]{num,num})\w*(\))/gi
  const inBracketsRegex = new RegExp(generatedRegexTest, 'gi');

  for (let i = 0; i < elements.length; i += 1) {
    if (elements[i].hasChildNodes()) {
      elements[i].childNodes.forEach((el) => {
        if (el.nodeType === Node.TEXT_NODE && el.nodeValue.match(inBracketsRegex)) {
          const text = el.nodeValue.substring(el.nodeValue.indexOf('(') + 1, el.nodeValue.indexOf(')'));

          // escape allowed characters and "(", ")" to make it RegExp safe
          const matchedTextRegex = new RegExp(text.replace(/[.,!?:"'`\\/&-()]/g, '\\$&'));

          // replace symbols that break html layout: data-bracketless=""this" breaks"
          const htmlSafeText = text.replace(/&/g, '&amp;').replace(/"/g, "'");

          // eslint-disable-next-line no-param-reassign
          el.parentNode.innerHTML = el.parentNode.innerHTML
            .replace(matchedTextRegex, `<bracket-less data-bracketless="${htmlSafeText}">${text}</bracket-less>`);
        }
      });
    }
  }
  // so that script will be injected once
  return true;
}
injectHTML();

// RegExp BUGS
// ignores text: "... (i.e., $99.99), ... (plus taxes) to buy it."
// @ https://github.com/getify/You-Dont-Know-JS/blob/master/up%20%26%20going/ch1.md#values--types

// (see Chapter 2). Loops (see "Loops")
// incorrectly grabs text: "see Chapter 2", and "). Loops ("
// @ https://github.com/getify/You-Dont-Know-JS/blob/master/up%20%26%20going/ch1.md#loops and go up

{'fraction_non_alphanumeric': 0.21267454350161116, 'fraction_numerical': 0.01611170784103115, 'mean_word_length': 3.0577342047930283, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 4, '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': '483656', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1256, 'n_words': 254}