From 090b4d6647c08f9b2a962966c892b84ef05fdd41 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ionu=C8=9B=20G=2E=20Stan?= <ionut.g.stan@gmail.com>
Date: Wed, 2 Nov 2011 12:47:41 +0200
Subject: [PATCH] Fix same origin policy issue when adding @font-face rules

If the first stylesheet in the document is located on an external domain, then
trying to access the `cssRules` property of that `CSSStyleSheet` object will
result in a Security error being thrown in Firefox. In Safari, `cssRules` will
be null, which causes a null pointer exception in the `styleSheet.cssRules.length`
expression.
---
 src/fonts.js | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/fonts.js b/src/fonts.js
index b027b766a..f4f71c4f6 100644
--- a/src/fonts.js
+++ b/src/fonts.js
@@ -1787,12 +1787,11 @@ var Font = (function Font() {
       var url = ('url(data:' + this.mimetype + ';base64,' +
                  window.btoa(data) + ');');
       var rule = "@font-face { font-family:'" + fontName + "';src:" + url + '}';
-      var styleSheet = document.styleSheets[0];
-      if (!styleSheet) {
-        document.documentElement.firstChild.appendChild(
-          document.createElement('style'));
-        styleSheet = document.styleSheets[0];
-      }
+
+      document.documentElement.firstChild.appendChild(
+        document.createElement('style'));
+
+      var styleSheet = document.styleSheets[document.styleSheets.length - 1];
       styleSheet.insertRule(rule, styleSheet.cssRules.length);
 
       return rule;