<commit_before>_              = require '../../utils/lodash'
styleSandbox   = require './sandbox'

observerOptions = 
  subtree          : true
  childList        : true
  characterData    : false
  attributes       : false
  attributeOldValue: false

isStyleTag      = (node)-> node.nodeType == 1 && node.nodeName == 'STYLE'
isLinkSheet     = (node)-> node.nodeType == 1 && node.nodeName=='LINK' && node.getAttribute('rel')=='stylesheet'

hasLinkSheet    = (node)-> node.nodeType == 1 && !!node.getElementsByTagName('link[rel=stylesheet]').length
hasStyleTag     = (node)-> node.nodeType == 1 && !!node.getElementsByTagName('style').length

isOrHasStyleTag = (node)-> isStyleTag(node)  || hasStyleTag(node)
isOrHasLinkSheet= (node)-> isLinkSheet(node) || hasLinkSheet(node)

getStyles         = (node)->
  return [] if node.nodeType != 1
  return [node] if isStyleTag(node)
  return node.querySelectorAll('style')

getLinks = (node)->
  return [] if node.nodeType != 1
  return [node] if isLinkSheet(node)
  return node.querySelectorAll('link[rel=stylesheet]')

hasStyleMutations = (mutations) ->
  _.some mutations, (m)->
    return true if isStyleTag(m.target) or isLinkSheet(m.target)
    return _.some m.addedNodes,   isOrHasStyleTag
    return _.some m.removedNodes, isOrHasStyleTag
    return _.some m.addedNodes,   isOrHasLinkSheet
    return _.some m.removedNodes, isOrHasLinkSheet
    false

processDocumentMutations = (prefix, doc, mutations)->
  styleSandbox.processDocument(prefix, doc) if hasStyleMutations(mutations)

# Collect all style mutations in the mutation batch, and add / remove them
processNodeMutations = (prefix, mutations)->
  _.map mutations, (mutation)->
    return styleSandbox.addStyle(prefix, mutation.target) if isStyleTag(mutation.target)
    # Can't be a style and also have addedNodes...
    _.map mutation.addedNodes, (node)->
      styleSandbox.addStyle(prefix, style) for style in getStyles(node)

class StyleObserver
  constructor : (prefix)->
    @_observers = []
    @prefix = prefix

  observe : (target)->
    @observeDocument(target) if target.nodeType==9 # document
    @observeNode(target)     if target.nodeType==3 # node

  process : (target)->
    @processDocument(target) if target.nodeType==9 # document
    @processNode(target)     if target.nodeType==3 # node
      

  processNode : (node)->
    _.map node.querySelectorAll('style'), styleSandbox.processStyle.bind(@, @prefix)
    
  observeNode: (node)->
    return unless node

    # Do it once at the beginning to ensure we catch what's already in there.
    @processNode(node)

    # Observe for future changes
    observer = new MutationObserver processNodeMutations.bind(@, @prefix)
    observer.observe(node, observerOptions)
    @_observers.push({root:node, observer: observer})
    observer

  processDocument : (doc)->
    styleSandbox.processDocument @prefix, doc

  observeDocument: (doc)->
    return unless doc

    # Only useful for Native HTML imports. Polyfills put link tags in the main DOM
    # _.map doc.querySelectorAll('link[rel=stylesheet]'), styleSandbox.processLink.bind(@, @prefix)

    # Do it once at the beginning to ensure we catch what's already in there.
    @processDocument(doc)
<commit_msg>Fix nodeType detection<commit_after>_              = require '../../utils/lodash'
styleSandbox   = require './sandbox'

observerOptions = 
  subtree          : true
  childList        : true
  characterData    : false
  attributes       : false
  attributeOldValue: false

isStyleTag      = (node)-> node.nodeType == 1 && node.nodeName == 'STYLE'
isLinkSheet     = (node)-> node.nodeType == 1 && node.nodeName=='LINK' && node.getAttribute('rel')=='stylesheet'

hasLinkSheet    = (node)-> node.nodeType == 1 && !!node.getElementsByTagName('link[rel=stylesheet]').length
hasStyleTag     = (node)-> node.nodeType == 1 && !!node.getElementsByTagName('style').length

isOrHasStyleTag = (node)-> isStyleTag(node)  || hasStyleTag(node)
isOrHasLinkSheet= (node)-> isLinkSheet(node) || hasLinkSheet(node)

getStyles         = (node)->
  return [] if node.nodeType != 1
  return [node] if isStyleTag(node)
  return node.querySelectorAll('style')

getLinks = (node)->
  return [] if node.nodeType != 1
  return [node] if isLinkSheet(node)
  return node.querySelectorAll('link[rel=stylesheet]')

hasStyleMutations = (mutations) ->
  _.some mutations, (m)->
    return true if isStyleTag(m.target) or isLinkSheet(m.target)
    return _.some m.addedNodes,   isOrHasStyleTag
    return _.some m.removedNodes, isOrHasStyleTag
    return _.some m.addedNodes,   isOrHasLinkSheet
    return _.some m.removedNodes, isOrHasLinkSheet
    false

processDocumentMutations = (prefix, doc, mutations)->
  if hasStyleMutations(mutations)
    styleSandbox.processDocument(prefix, doc)

# Collect all style mutations in the mutation batch, and add / remove them
processNodeMutations = (prefix, mutations)->
  _.map mutations, (mutation)->
    # Style mutations don't interest us for now. we just skip them
    # return styleSandbox.addStyle(prefix, mutation.target) if isStyleTag(mutation.target)

    # Can't be a style and also have addedNodes...
    _.map mutation.addedNodes, (node)->
      styleSandbox.addStyle(prefix, style) for style in getStyles(node)

class StyleObserver
  constructor : (prefix)->
    @_observers = []
    @prefix = prefix

  observe : (target)->
    @observeDocument(target) if target.nodeType==9 # document
    @observeNode(target)     if target.nodeType==1 # node

  process : (target)->
    @processDocument(target) if target.nodeType==9 # document
    @processNode(target)     if target.nodeType==1 # node
      

  processNode : (node)->
    _.map node.querySelectorAll('style'), styleSandbox.addStyle.bind(@, @prefix)
    
  observeNode: (node)->
    return unless node

    # Do it once at the beginning to ensure we catch what's already in there.
    @processNode(node)

    # Observe for future changes
    observer = new MutationObserver processNodeMutations.bind(@, @prefix)
    observer.observe(node, observerOptions)
    @_observers.push({root:node, observer: observer})
    observer

  processDocument : (doc)->
    styleSandbox.processDocument @prefix, doc

  observeDocument: (doc)->
    return unless doc

    # Only useful for Native HTML imports. Polyfills put link tags in the main DOM
    # _.map doc.querySelectorAll('link[rel=stylesheet]'), styleSandbox.processLink.bind(@, @prefix)

    # Do it once at the beginning to ensure we catch what's already in there.
    @processDocument(doc)

{'fraction_non_alphanumeric': 0.11951704111263946, 'fraction_numerical': 0.0030567018187375823, 'mean_word_length': 3.7420289855072464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '3496315', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1928, 'n_words': 529}