   new ScopedCss(prefix, null, node).process()
 
 addLink = (prefix, node)->
-  addSheet(prefix, node)
+  addSheet(prefix, node, node.ownerDocument)
   finalize()
 
 removeLink = (node)->

  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')")
prefixRule = (prefix, rule)->
  return unless rule
  if rule.selectorText

  rules = getRules(style.sheet)
  removeStyleSheet(style)
  rules
prefixRules = (prefix, rules)->
  r = _.map rules, (rule)-> prefixRule(prefix, rule);
  r.join(' ');

appendText = (entry, text)->
  rules = textToRules(resolveUrls(text, entry.node.href, entry.doc))
  entry.style = prefixRules(entry.prefix, rules) if rules
  true

appendStyle = (entry)->
  if !!entry.node.href
    if entry.node.__importParsed
      # Polyfills go through here.
      # Skip links since we'll have them as Styles from the polyfill
      appendText(entry, entry.node.__resource)
    else
      # Chrome goes here
      # Loading External stylesheets (out of current domain)
      # Only Chrome will use this, since we have tweaked the Polyfill to avoid 2 requests.
      removeStyleSheet(entry.node)
      fetch(entry.node.href).then appendText.bind(this, entry)
  else
    resolveUrlsInStyle(entry.node)
    # Disable again to prevent infinite loops
    entry.node.disabled=true
    rules = getRules(entry.node.sheet)
    entry.style = prefixRules(entry.prefix, rules) if rules

addSheet = (prefix, node, doc)->
  entry = findNode(prefix, node)
  unless entry
