 superagent          = require 'superagent'
 
 Styles     = []
-
 
 createStyleSheet = (content="", disabled=false)->
   style = document.createElement('style')

  uri.pop()
  uri.join('/')+'/'

appendStyle = (entry)->
  rules = getRules(entry.node.sheet)
  if rules
    appendRules(entry, rules) if rules
  else if !!entry.node.href
    # Loading External stylesheets (out of current domain)
    # Only Chrome will use this, since we have tweaked the Polyfill to avoid 2 requests.
    href = entry.node.href
    uri = getUriFromHref(entry.node.href)
    entry.node.parentNode.removeChild(entry.node)
    fetch(href).then (text)->
      style = createStyleSheet(replaceLinkTagUrls(uri, text), true)
      rules = getRules(style.sheet)
      appendRules(entry, rules)
      removeStyleSheet(style)
      true
addSheet = (prefix, node, doc)->
  entry = findNode(prefix, node)
  unless entry

getRules = (sheet)->
  return unless sheet?
  sheet.cssRules || sheet.rules

appendRules = (entry, rules)->
  entry.style = rulesToText(entry.prefix, rules)
  true

fetch = (href)->
  new Promise (resolve, reject)->
    return reject() unless !!href
    superagent.get(href).end (err, res)->
      resolve(res.text)

replaceLinkTagUrls = (uri, text)->
  re = new RegExp "url\\(['\"]?([^)]*?)['\"]?\\)", "g"
  text.replace(re, "url('#{uri}$1')")
