<commit_before>/**
 * Walks the dom tree from the given node up until it finds a match
 * Designed for optimal performance.
 *
 * @param {Element} node The from which to check the parent nodes
 * @param {Object} matchingSet Object to match against (possible properties: nodeName, className, classRegExp)
 * @param {Number} [levels] How many parents should the function check up from the current node (defaults to 50)
 * @return {null|Element} Returns the first element that matched the desiredNodeName(s)
 * @example
 *    var listElement = wysihtml5.dom.getParentElement(document.querySelector("li"), { nodeName: ["MENU", "UL", "OL"] });
 *    // ... or ...
 *    var unorderedListElement = wysihtml5.dom.getParentElement(document.querySelector("li"), { nodeName: "UL" });
 *    // ... or ...
 *    var coloredElement = wysihtml5.dom.getParentElement(myTextNode, { nodeName: "SPAN", className: "wysiwyg-color-red", classRegExp: /wysiwyg-color-[a-z]/g });
 */
import lang from "wysihtml5/lang";

function _isSameNodeName(nodeName, desiredNodeNames) {
  if (!desiredNodeNames || !desiredNodeNames.length) {
    return true;
  }

  if (typeof(desiredNodeNames) === "string") {
    return nodeName === desiredNodeNames;
  } else {
    return lang.array(desiredNodeNames).contains(nodeName);
  }
}

function _isElement(node) {
  return node.nodeType === Node.ELEMENT_NODE;
}

function _hasClassName(element, className, classRegExp) {
  var classNames = (element.className || "").match(classRegExp) || [];
  if (!className) {
    return !!classNames.length;
  }
  return classNames[classNames.length - 1] === className;
}

function _while(node, options, callback) {
  var levels = options.levels || 50
  var until;
  if (options.until && options.until.contains(node)) {
    until = options.until;
  } else {
    until = node.ownerDocument.body;
  }
  var results;
  while (levels-- && node && node !== until) {
    results = callback(node)
    if (results !== undefined) {
      return results;
    }
    node = node.parentNode;
  }<commit_msg>Fix get parent element crashing bug<commit_after>/**
 * Walks the dom tree from the given node up until it finds a match
 * Designed for optimal performance.
 *
 * @param {Element} node The from which to check the parent nodes
 * @param {Object} matchingSet Object to match against (possible properties: nodeName, className, classRegExp)
 * @param {Number} [levels] How many parents should the function check up from the current node (defaults to 50)
 * @return {null|Element} Returns the first element that matched the desiredNodeName(s)
 * @example
 *    var listElement = wysihtml5.dom.getParentElement(document.querySelector("li"), { nodeName: ["MENU", "UL", "OL"] });
 *    // ... or ...
 *    var unorderedListElement = wysihtml5.dom.getParentElement(document.querySelector("li"), { nodeName: "UL" });
 *    // ... or ...
 *    var coloredElement = wysihtml5.dom.getParentElement(myTextNode, { nodeName: "SPAN", className: "wysiwyg-color-red", classRegExp: /wysiwyg-color-[a-z]/g });
 */
import lang from "wysihtml5/lang";

function _isSameNodeName(nodeName, desiredNodeNames) {
  if (!desiredNodeNames || !desiredNodeNames.length) {
    return true;
  }

  if (typeof(desiredNodeNames) === "string") {
    return nodeName === desiredNodeNames;
  } else {
    return lang.array(desiredNodeNames).contains(nodeName);
  }
}

function _isElement(node) {
  return node.nodeType === Node.ELEMENT_NODE;
}

function _hasClassName(element, className, classRegExp) {
  var classNames = (element.className || "").match(classRegExp) || [];
  if (!className) {
    return !!classNames.length;
  }
  return classNames[classNames.length - 1] === className;
}

function _while(node, options, callback) {
  var levels = options.levels || 50
  var until;
  if (options.until && options.until.contains(node)) {
    until = options.until;
  } else if (node && node.ownerDocument) {
    until = node.ownerDocument.body;
  }
  var results;
  while (levels-- && node && until && node !== until) {
    results = callback(node)
    if (results !== undefined) {
      return results;
    }
    node = node.parentNode;
  }
{'fraction_non_alphanumeric': 0.13788939869596717, 'fraction_numerical': 0.004346776141028737, 'mean_word_length': 3.936829558998808, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2125333', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1261, 'n_words': 349}