   uri.pop()
   uri.join('/')+'/'
 
+
+textToRules = (text)->
+  style = createStyleSheet(text, true)
+  rules = getRules(style.sheet)
+  removeStyleSheet(style)
+  rules
+rulesToText = (prefix, rules)->
+  r = _.map rules, (rule)-> prefixRule(prefix, rule);
+  r.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
+    uri = getUriFromHref(href)
+    if entry.node.__resource
+      # Polyfills go through here.
+      rules = textToRules(replaceLinkTagUrls(uri,entry.node.__resource))
+      appendRules(entry, rules) if rules
+    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(href).then (text)->
+        rules = textToRules(replaceLinkTagUrls(uri, text))
+        appendRules(entry, rules) if rules
+        true
 addSheet = (prefix, node, doc)->
   entry = findNode(prefix, node)
   unless entry

superagent          = require 'superagent'

Styles     = []


createStyleSheet = (content="", disabled=false)->
  style = document.createElement('style')

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')")
