<commit_before>import { createContext } from 'react';

class NodeError extends Error {
  constructor(node, ...errorArgs) {
    super(...errorArgs);
    this.node = node;
  }
}

const iconStyleAttribute = 'data-es-icon-styles';
const iconsAsset = 'https://bdaim-webexcdn-p.azureedge.net/es-assets/icons.css';

const getExistingStyleTag = node =>
  node.querySelector(`[${iconStyleAttribute}]`) ||
  node.querySelector(`link[href="${iconsAsset}"]:not([rel="preload"])`);

const createStyleTag = () => {
  const styleTag = document.createElement('link');
  styleTag.setAttribute(iconStyleAttribute, '');
  styleTag.setAttribute('rel', 'stylesheet');
  styleTag.setAttribute('href', iconsAsset);
  return styleTag;
};

const addTag = (node, func) => {
  try {
    const foundTag = getExistingStyleTag(node);
    if (foundTag) return foundTag;

    const tag = createStyleTag();
    func(tag);
    return tag;
  } catch (err) {
    const nodeError = new NodeError(
      node,
      'Failed to load icon styles on node',
      { cause: err }
    );
    console.error(nodeError); // eslint-disable-line no-console
    return undefined;
  }
};

const defaultIconContext = {
  initializedNodes: []
};

const documentAppend = tag => document.head.append(tag);
const initializeBody = node => {
  if (getExistingStyleTag(document.head)) return node;

  return addTag(node, documentAppend);
};

const initializeNode = node => {
  // body must always be set up
  setup(document.body); // eslint-disable-line no-use-before-define
  addTag(node, tag => node.prepend(tag));
};

const setup = node => {
  if (!node) return;

  const { initializedNodes } = defaultIconContext;

  if (initializedNodes.some(initializedNode => initializedNode === node))
    return;

  const isBody = node === document.body;
<commit_msg>Fix: ensuring we return the node when adding to a Shadow Root<commit_after>import { createContext } from 'react';

class NodeError extends Error {
  constructor(node, ...errorArgs) {
    super(...errorArgs);
    this.node = node;
  }
}

const iconStyleAttribute = 'data-es-icon-styles';
const iconsAsset = 'https://bdaim-webexcdn-p.azureedge.net/es-assets/icons.css';

const getExistingStyleTag = node =>
  node.querySelector(`[${iconStyleAttribute}]`) ||
  node.querySelector(`link[href="${iconsAsset}"]:not([rel="preload"])`);

const createStyleTag = () => {
  const styleTag = document.createElement('link');
  styleTag.setAttribute(iconStyleAttribute, '');
  styleTag.setAttribute('rel', 'stylesheet');
  styleTag.setAttribute('href', iconsAsset);
  return styleTag;
};

const addTag = (node, func) => {
  try {
    const foundTag = getExistingStyleTag(node);
    if (foundTag) return foundTag;

    const tag = createStyleTag();
    func(tag);
    return tag;
  } catch (err) {
    const nodeError = new NodeError(
      node,
      'Failed to load icon styles on node',
      { cause: err }
    );
    console.error(nodeError); // eslint-disable-line no-console
    return undefined;
  }
};

const defaultIconContext = {
  initializedNodes: []
};

const documentAppend = tag => document.head.append(tag);
const initializeBody = node => {
  if (getExistingStyleTag(document.head)) return node;

  return addTag(node, documentAppend);
};

const initializeNode = node => {
  // body must always be set up
  setup(document.body); // eslint-disable-line no-use-before-define
  return addTag(node, tag => node.prepend(tag));
};

const setup = node => {
  if (!node) return;

  const { initializedNodes } = defaultIconContext;

  if (initializedNodes.some(initializedNode => initializedNode === node))
    return;

  const isBody = node === document.body;

{'fraction_non_alphanumeric': 0.14473324213406294, 'fraction_numerical': 0.0, 'mean_word_length': 4.015089163237311, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 2, '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': '2891454', 'n_tokens_mistral': 1116, 'n_tokens_neox': 1113, 'n_words': 262}