<commit_before>import {getConfig} from '../config'
import {
  fuzzyMatches,
  matches,
  makeNormalizer,
  queryAllByAttribute,
  makeFindQuery,
  makeSingleQuery,
} from './all-utils'
import {queryAllByText} from './text'

function queryAllLabelsByText(
  container,
  text,
  {exact = true, trim, collapseWhitespace, normalizer} = {},
) {
  const matcher = exact ? matches : fuzzyMatches
  const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
  return Array.from(container.querySelectorAll('label')).filter(label => {
    let textToMatch = label.textContent

    // The children of a textarea are part of `textContent` as well. We
    // need to remove them from the string so we can match it afterwards.
    Array.from(label.querySelectorAll('textarea')).forEach(textarea => {
      textToMatch = textToMatch.replace(textarea.value, '')
    })

    // The children of a select are also part of `textContent`, so we
    // need also to remove their text.
    Array.from(label.querySelectorAll('select')).forEach(select => {
      textToMatch = textToMatch.replace(select.textContent, '')
    })

    return matcher(textToMatch, label, text, matchNormalizer)
  })
}

function queryAllByLabelText(
  container,
  text,
  {selector = '*', exact = true, collapseWhitespace, trim, normalizer} = {},
) {
  const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
  const labels = queryAllLabelsByText(container, text, {
    exact,
    normalizer: matchNormalizer,
  })
  const labelledElements = labels
    .reduce((matchedElements, label) => {
      const elementsForLabel = []
      if (label.control) {
        elementsForLabel.push(label.control)
      }
      /* istanbul ignore if */
      if (label.getAttribute('for')) {
        // we're using this notation because with the # selector we would have to escape special characters e.g. user.name
        // see https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector#Escaping_special_characters
        // <label for="someId">text</label><input id="someId" />

        // .control support has landed in jsdom (https://github.com/jsdom/jsdom/issues/2175)
        elementsForLabel.push(
          container.querySelector(`[id="${label.getAttribute('for')}"]`),
        )
      }
      if (label.getAttribute('id')) {
        // <label id="someId">text</label><input aria-labelledby="someId" />
        container
          .querySelectorAll(`[aria-labelledby~="${label.getAttribute('id')}"]`)
          .forEach(element => elementsForLabel.push(element))
      }
      if (label.childNodes.length) {
        // <label>text: <input /></label>
        const formControlSelector =
          'button, input, meter, output, progress, select, textarea'
        label
          .querySelectorAll(formControlSelector)
          .forEach(element => elementsForLabel.push(element))
      }
      return matchedElements.concat(elementsForLabel)
    }, [])
    .filter(element => element !== null)
    .concat(queryAllByAttribute('aria-label', container, text, {exact}))

  const possibleAriaLabelElements = queryAllByText(container, text, {
    exact,
    normalizer: matchNormalizer,
  })

  const ariaLabelledElements = possibleAriaLabelElements.reduce(
    (allLabelledElements, nextLabelElement) => {
      const labelId = nextLabelElement.getAttribute('id')<commit_msg>fix(allByLabelText): forEach on NodeList is not supported in edge (#487)<commit_after>import {getConfig} from '../config'
import {
  fuzzyMatches,
  matches,
  makeNormalizer,
  queryAllByAttribute,
  makeFindQuery,
  makeSingleQuery,
} from './all-utils'
import {queryAllByText} from './text'

function queryAllLabelsByText(
  container,
  text,
  {exact = true, trim, collapseWhitespace, normalizer} = {},
) {
  const matcher = exact ? matches : fuzzyMatches
  const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
  return Array.from(container.querySelectorAll('label')).filter(label => {
    let textToMatch = label.textContent

    // The children of a textarea are part of `textContent` as well. We
    // need to remove them from the string so we can match it afterwards.
    Array.from(label.querySelectorAll('textarea')).forEach(textarea => {
      textToMatch = textToMatch.replace(textarea.value, '')
    })

    // The children of a select are also part of `textContent`, so we
    // need also to remove their text.
    Array.from(label.querySelectorAll('select')).forEach(select => {
      textToMatch = textToMatch.replace(select.textContent, '')
    })

    return matcher(textToMatch, label, text, matchNormalizer)
  })
}

function queryAllByLabelText(
  container,
  text,
  {selector = '*', exact = true, collapseWhitespace, trim, normalizer} = {},
) {
  const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
  const labels = queryAllLabelsByText(container, text, {
    exact,
    normalizer: matchNormalizer,
  })
  const labelledElements = labels
    .reduce((matchedElements, label) => {
      const elementsForLabel = []
      if (label.control) {
        elementsForLabel.push(label.control)
      }
      /* istanbul ignore if */
      if (label.getAttribute('for')) {
        // we're using this notation because with the # selector we would have to escape special characters e.g. user.name
        // see https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector#Escaping_special_characters
        // <label for="someId">text</label><input id="someId" />

        // .control support has landed in jsdom (https://github.com/jsdom/jsdom/issues/2175)
        elementsForLabel.push(
          container.querySelector(`[id="${label.getAttribute('for')}"]`),
        )
      }
      if (label.getAttribute('id')) {
        // <label id="someId">text</label><input aria-labelledby="someId" />
        Array.from(
          container.querySelectorAll(
            `[aria-labelledby~="${label.getAttribute('id')}"]`,
          ),
        ).forEach(element => elementsForLabel.push(element))
      }
      if (label.childNodes.length) {
        // <label>text: <input /></label>
        const formControlSelector =
          'button, input, meter, output, progress, select, textarea'
        Array.from(
          label.querySelectorAll(formControlSelector),
        ).forEach(element => elementsForLabel.push(element))
      }
      return matchedElements.concat(elementsForLabel)
    }, [])
    .filter(element => element !== null)
    .concat(queryAllByAttribute('aria-label', container, text, {exact}))

  const possibleAriaLabelElements = queryAllByText(container, text, {
    exact,
    normalizer: matchNormalizer,
  })

  const ariaLabelledElements = possibleAriaLabelElements.reduce(
    (allLabelledElements, nextLabelElement) => {
      const labelId = nextLabelElement.getAttribute('id')
{'fraction_non_alphanumeric': 0.12700495771361914, 'fraction_numerical': 0.0016039661708953047, 'mean_word_length': 3.3967948717948717, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 37, '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': '1520886', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1944, 'n_words': 437}