<commit_before>(ns cljss.sheet
  (:require [goog.object :as gobj]
            [goog.dom :as dom]))

(def ^:private dev? ^boolean goog.DEBUG)
(def ^:private limit 65534)

(defn- make-style-tag []
  (let [tag (dom/createElement "style")
        head (aget (dom/getElementsByTagNameAndClass "head") 0)]
    (gobj/set tag "type" "text/css")
    (dom/appendChild tag (dom/createTextNode ""))
    (dom/appendChild head tag)
    tag))

(defn- find-sheet [tag]
  (if-let [sheet (gobj/get tag "sheet")]
    sheet
    ;; workaround for Firefox
    (let [sheets (gobj/get js/document "styleSheets")]
      (loop [idx 0
             sheet (aget sheets idx)]
        (if (= tag (gobj/get sheet "ownerNode"))
          sheet
          (recur (inc idx) (aget sheets (inc idx))))))))


(defprotocol ISheet
  (insert! [this css cls-name])
  (flush! [this])
  (filled? [this]))

(deftype Sheet [tag cls-names]
  ISheet
  (insert! [this rule cls-name]
    (when (filled? this)
      (throw (js/Error. (str "A stylesheet can only have " limit " rules"))))
    (when-not (@cls-names cls-name)
      (swap! cls-names conj cls-name)
      (let [sheet (find-sheet tag)
            rules-count (gobj/get (gobj/get sheet "cssRules") "length")]
        (if dev?
         (if (not= (.indexOf rule "@import") -1)
           (.insertBefore tag (dom/createTextNode rule) (gobj/get tag "firstChild"))
           (dom/appendChild tag (dom/createTextNode rule)))
         (try
           (if (not= (.indexOf rule "@import") -1)
             (.insertRule sheet rule 0)
             (.insertRule sheet rule rules-count))
           (catch :default e
             (when dev?
               (js/console.warn "Illegal CSS rule" rule))))))))
  (flush! [this]
    (-> tag
        .parentNode
        (.removeChild tag)))
  (filled? [this]
    (= (count @cls-names) limit)))

(defn create-sheet []
  (Sheet. (make-style-tag) (atom #{})))<commit_msg>bind tag sheet to Sheet instance instead of doing DOM lookup<commit_after>(ns cljss.sheet
  (:require [goog.object :as gobj]
            [goog.dom :as dom]))

(def ^:private dev? ^boolean goog.DEBUG)
(def ^:private limit 65534)

(defn- make-style-tag []
  (let [tag (dom/createElement "style")
        head (aget (dom/getElementsByTagNameAndClass "head") 0)]
    (gobj/set tag "type" "text/css")
    (dom/appendChild tag (dom/createTextNode ""))
    (dom/appendChild head tag)
    tag))


(defprotocol ISheet
  (insert! [this css cls-name])
  (flush! [this])
  (filled? [this]))

(deftype Sheet [tag sheet cls-names]
  ISheet
  (insert! [this rule cls-name]
    (when (filled? this)
      (throw (js/Error. (str "A stylesheet can only have " limit " rules"))))
    (when-not (@cls-names cls-name)
      (swap! cls-names conj cls-name)
      (let [rules-count (gobj/get (gobj/get sheet "cssRules") "length")]
        (if dev?
         (if (not= (.indexOf rule "@import") -1)
           (.insertBefore tag (dom/createTextNode rule) (gobj/get tag "firstChild"))
           (dom/appendChild tag (dom/createTextNode rule)))
         (try
           (if (not= (.indexOf rule "@import") -1)
             (.insertRule sheet rule 0)
             (.insertRule sheet rule rules-count))
           (catch :default e
             (when dev?
               (js/console.warn "Illegal CSS rule" rule))))))))
  (flush! [this]
    (-> tag
        .parentNode
        (.removeChild tag)))
  (filled? [this]
    (= (count @cls-names) limit)))

(defn create-sheet []
  (let [tag (make-style-tag)
        sheet (gobj/get tag "sheet")]
    (Sheet. tag sheet (atom #{}))))
{'fraction_non_alphanumeric': 0.15921237693389592, 'fraction_numerical': 0.005344585091420534, 'mean_word_length': 2.4424007744433687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5141839', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1171, 'n_words': 351}