<commit_before>const DECL_REGEX = /.([^:{]+)(:[^{]+)?{([^:]+):([^}]+)}/g;

const StyletronCore = require('styletron-core');

/**
 * StyletronClient
 * @extends StyletronCore
 * @packagename styletron-client
 * @example
 * const styleElement = document.querySelector('style');
 * const styletron = new StyletronClient(styleElement);
 */
class StyletronClient extends StyletronCore {
  /**
   * Create a new StyletronClient instance
   * @param {NodeList|HTMLCollection|HTMLStyleElement[]} serverStyles - List of server style elements
   */
  constructor(serverStyles, opts) {
    super(opts);
    if (!serverStyles) {
      throw Error('No stylesheet');
    }
    this.uniqueCount = 0;
    this.mediaSheets = {};
    for (let i = 0; i < serverStyles.length; i++) {
      const element = serverStyles[i];
      if (element.media) {
        this.mediaSheets[element.media] = element;
      } else {
        this.mainSheet = element;
      }
      this.hydrateCacheFromCssString(element.textContent, element.media);
    }
  }

  /*
   * Hydrate the cache from a css string and media string
   * @param {string} css   - The stylesheet css content
   * @param {string} media - The stylesheet media string
   */
  hydrateCacheFromCssString(css, media) {
    let decl;
    // {
    //  1: className,
    //  2: pseudo,
    //  3: prop,
    //  4: val
    // }
    while (decl = DECL_REGEX.exec(css)) {
      this.uniqueCount++;
      StyletronCore.assignDecl(this.cache, {
        pseudo: decl[2],
        prop: decl[3],
        val: decl[4],
        media
      }, decl[1]);
    }
  }

  /**
   * Inject declaration into the stylesheet and return the unique class name
   * @return {string}      class name
   * @example
   * // <style id="styletron">.c0{color:red}</style>
   * const styletron = new StyletronClient(document.getElementsByClassName('_styletron_hydrate_'));
   * styletron.injectDeclaration({prop: 'color', val: 'blue'});
   * // → 'c1'
   * styletron.injectDeclaration({prop: 'color', val: 'red', media: '(min-width: 800px)'});
   * // → 'c2'
   * styletron.injectDeclaration({prop: 'color', val: 'red'});
   * // → 'c0'
   */
  injectDeclaration(decl) {
    const oldCount = this.uniqueCount;
    const className = super.injectDeclaration(decl);
    if (oldCount !== this.uniqueCount) {
      const rule = declarationToRule(className, decl);
      let sheet;
      if (decl.media) {
        if (!this.mediaSheets[decl.media]) {
          const mediaSheet = document.createElement('style');
          this.mediaSheets[decl.media] = mediaSheet;
          this.mainSheet.parentNode.appendChild(mediaSheet);
        }
        sheet = this.mediaSheets[decl.media].sheet;
      } else {
        sheet = this.mainSheet.sheet;
      }
      sheet.insertRule(rule, sheet.rules.length);<commit_msg>Reference sheet.cssRules instead of sheet.rules<commit_after>const DECL_REGEX = /.([^:{]+)(:[^{]+)?{([^:]+):([^}]+)}/g;

const StyletronCore = require('styletron-core');

/**
 * StyletronClient
 * @extends StyletronCore
 * @packagename styletron-client
 * @example
 * const styleElement = document.querySelector('style');
 * const styletron = new StyletronClient(styleElement);
 */
class StyletronClient extends StyletronCore {
  /**
   * Create a new StyletronClient instance
   * @param {NodeList|HTMLCollection|HTMLStyleElement[]} serverStyles - List of server style elements
   */
  constructor(serverStyles, opts) {
    super(opts);
    if (!serverStyles) {
      throw Error('No stylesheet');
    }
    this.uniqueCount = 0;
    this.mediaSheets = {};
    for (let i = 0; i < serverStyles.length; i++) {
      const element = serverStyles[i];
      if (element.media) {
        this.mediaSheets[element.media] = element;
      } else {
        this.mainSheet = element;
      }
      this.hydrateCacheFromCssString(element.textContent, element.media);
    }
  }

  /*
   * Hydrate the cache from a css string and media string
   * @param {string} css   - The stylesheet css content
   * @param {string} media - The stylesheet media string
   */
  hydrateCacheFromCssString(css, media) {
    let decl;
    // {
    //  1: className,
    //  2: pseudo,
    //  3: prop,
    //  4: val
    // }
    while (decl = DECL_REGEX.exec(css)) {
      this.uniqueCount++;
      StyletronCore.assignDecl(this.cache, {
        pseudo: decl[2],
        prop: decl[3],
        val: decl[4],
        media
      }, decl[1]);
    }
  }

  /**
   * Inject declaration into the stylesheet and return the unique class name
   * @return {string}      class name
   * @example
   * // <style id="styletron">.c0{color:red}</style>
   * const styletron = new StyletronClient(document.getElementsByClassName('_styletron_hydrate_'));
   * styletron.injectDeclaration({prop: 'color', val: 'blue'});
   * // → 'c1'
   * styletron.injectDeclaration({prop: 'color', val: 'red', media: '(min-width: 800px)'});
   * // → 'c2'
   * styletron.injectDeclaration({prop: 'color', val: 'red'});
   * // → 'c0'
   */
  injectDeclaration(decl) {
    const oldCount = this.uniqueCount;
    const className = super.injectDeclaration(decl);
    if (oldCount !== this.uniqueCount) {
      const rule = declarationToRule(className, decl);
      let sheet;
      if (decl.media) {
        if (!this.mediaSheets[decl.media]) {
          const mediaSheet = document.createElement('style');
          this.mediaSheets[decl.media] = mediaSheet;
          this.mainSheet.parentNode.appendChild(mediaSheet);
        }
        sheet = this.mediaSheets[decl.media].sheet;
      } else {
        sheet = this.mainSheet.sheet;
      }
      sheet.insertRule(rule, sheet.cssRules.length);
{'fraction_non_alphanumeric': 0.14729370008873113, 'fraction_numerical': 0.006033717834960071, 'mean_word_length': 2.836623553437713, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '7052674', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1794, 'n_words': 385}