<commit_before>import dom from "../dom";
import { Constants } from "../constants";
import { formatInline } from "./formatInline";

var undef,
    NODE_NAME = "A";

function _removeFormat(composer, anchors) {
  var length  = anchors.length,
      i       = 0,
      anchor,
      codeElement,
      textContent;
  for (; i<length; i++) {
    anchor      = anchors[i];
    codeElement = composer.parentElement(anchor, { nodeName: "code" });
    textContent = dom.getTextContent(anchor);

    // if <a> contains url-like text content, rename it to <code> to prevent re-autolinking
    // else replace <a> with its childNodes
    if (textContent.match(Constants.URL_REG_EXP) && !codeElement) {
      // <code> element is used to prevent later auto-linking of the content
      codeElement = dom.renameElement(anchor, "code");
    } else {
      dom.replaceWithChildNodes(anchor);
    }
  }
}

function _format(composer, attributes) {
  var doc             = composer.doc,
      tempClass       = "_wysihtml5-temp-" + (+new Date()),
      tempClassRegExp = /non-matching-class/g,
      i               = 0,
      length,
      anchors,
      anchor,
      hasElementChild,
      isEmpty,
      elementToSetCaretAfter,
      textContent,
      whiteSpace,
      j;

  formatInline.exec(composer, undef, NODE_NAME, tempClass, tempClassRegExp);
  anchors = doc.querySelectorAll(NODE_NAME + "." + tempClass);
  length  = anchors.length;
  for (; i<length; i++) {
    anchor = anchors[i];
    anchor.removeAttribute("class");
    for (j in attributes) {
      anchor.setAttribute(j, attributes[j]);
    }
  }

  elementToSetCaretAfter = anchor;
  if (length === 1) {
    textContent = dom.getTextContent(anchor);
    hasElementChild = !!anchor.querySelector("*");
    isEmpty = textContent === "" || textContent === Constants.INVISIBLE_SPACE;
    if (!hasElementChild && isEmpty) {
      dom.setTextContent(anchor, attributes.text || anchor.href);
      whiteSpace = doc.createTextNode(" ");
      composer.selection.setAfter(anchor);
      dom.insert(whiteSpace).after(anchor);
      elementToSetCaretAfter = whiteSpace;
    }
  }
  composer.selection.setAfter(elementToSetCaretAfter);
  return anchors;
}

var createLink = {
  /**<commit_msg>Always insert ws after generating link<commit_after>import dom from "../dom";
import { Constants } from "../constants";
import { formatInline } from "./formatInline";

var undef,
    NODE_NAME = "A";

function _removeFormat(composer, anchors) {
  var length  = anchors.length,
      i       = 0,
      anchor,
      codeElement,
      textContent;
  for (; i<length; i++) {
    anchor      = anchors[i];
    codeElement = composer.parentElement(anchor, { nodeName: "code" });
    textContent = dom.getTextContent(anchor);

    // if <a> contains url-like text content, rename it to <code> to prevent re-autolinking
    // else replace <a> with its childNodes
    if (textContent.match(Constants.URL_REG_EXP) && !codeElement) {
      // <code> element is used to prevent later auto-linking of the content
      codeElement = dom.renameElement(anchor, "code");
    } else {
      dom.replaceWithChildNodes(anchor);
    }
  }
}

function _format(composer, attributes) {
  var doc             = composer.doc,
      tempClass       = "_wysihtml5-temp-" + (+new Date()),
      tempClassRegExp = /non-matching-class/g,
      i               = 0,
      length,
      anchors,
      anchor,
      hasElementChild,
      isEmpty,
      elementToSetCaretAfter,
      textContent,
      whiteSpace,
      j;

  formatInline.exec(composer, undef, NODE_NAME, tempClass, tempClassRegExp);
  anchors = doc.querySelectorAll(NODE_NAME + "." + tempClass);
  length  = anchors.length;
  for (; i<length; i++) {
    anchor = anchors[i];
    anchor.removeAttribute("class");
    for (j in attributes) {
      anchor.setAttribute(j, attributes[j]);
    }
  }

  elementToSetCaretAfter = anchor;
  if (length === 1) {
    textContent = dom.getTextContent(anchor);
    hasElementChild = !!anchor.querySelector("*");
    isEmpty = textContent === "" || textContent === Constants.INVISIBLE_SPACE;
    if (!hasElementChild && isEmpty) {
      dom.setTextContent(anchor, attributes.text || anchor.href);
    }

    whiteSpace = doc.createTextNode(" ");
    composer.selection.setAfter(anchor);
    dom.insert(whiteSpace).after(anchor);
    elementToSetCaretAfter = whiteSpace;
  }
  composer.selection.setAfter(elementToSetCaretAfter);
  return anchors;
}

var createLink = {
  /**
{'fraction_non_alphanumeric': 0.12795363352652697, 'fraction_numerical': 0.0017833259028087382, 'mean_word_length': 2.9394205443371377, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '913908', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1357, 'n_words': 307}