Author: jezdez
Date: 2011-12-27 14:49:24 -0800 (Tue, 27 Dec 2011)
New Revision: 17282

Added:
   
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/
   
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.eot
   
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.svg
   
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fragments.css
Modified:
   django/trunk/django/contrib/staticfiles/storage.py
   django/trunk/tests/regressiontests/staticfiles_tests/storage.py
   django/trunk/tests/regressiontests/staticfiles_tests/tests.py
Log:
Fixed #17455 -- Extended `CachedStaticFilesStorage` slightly to handle some 
URLs better that are used to add support for webfonts to IE 6-8. Also ignore 
`data:` URLs and fragment-only URLs (e.g. `#default#VML`).

Modified: django/trunk/django/contrib/staticfiles/storage.py
===================================================================
--- django/trunk/django/contrib/staticfiles/storage.py  2011-12-27 15:35:41 UTC 
(rev 17281)
+++ django/trunk/django/contrib/staticfiles/storage.py  2011-12-27 22:49:24 UTC 
(rev 17282)
@@ -12,7 +12,7 @@
 from django.core.exceptions import ImproperlyConfigured
 from django.core.files.base import ContentFile
 from django.core.files.storage import FileSystemStorage, get_storage_class
-from django.utils.encoding import force_unicode
+from django.utils.encoding import force_unicode, smart_str
 from django.utils.functional import LazyObject
 from django.utils.importlib import import_module
 from django.utils.datastructures import SortedDict
@@ -84,9 +84,14 @@
         for chunk in content.chunks():
             md5.update(chunk)
         md5sum = md5.hexdigest()[:12]
-        hashed_name = os.path.join(path, u"%s.%s%s" % (root, md5sum, ext))
+        hashed_name = os.path.join(path, u"%s.%s%s" %
+                                   (root, md5sum, ext))
         unparsed_name = list(parsed_name)
         unparsed_name[2] = hashed_name
+        # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
+        # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
+        if '?#' in name and not unparsed_name[3]:
+            unparsed_name[2] += '?'
         return urlunsplit(unparsed_name)
 
     def cache_key(self, name):
@@ -103,7 +108,8 @@
             hashed_name = self.cache.get(cache_key)
             if hashed_name is None:
                 hashed_name = self.hashed_name(name).replace('\\', '/')
-                # set the cache if there was a miss (e.g. if cache server goes 
down)
+                # set the cache if there was a miss
+                # (e.g. if cache server goes down)
                 self.cache.set(cache_key, hashed_name)
         return unquote(super(CachedFilesMixin, self).url(hashed_name))
 
@@ -119,7 +125,7 @@
             """
             matched, url = matchobj.groups()
             # Completely ignore http(s) prefixed URLs
-            if url.startswith(('http', 'https')):
+            if url.startswith(('#', 'http', 'https', 'data:')):
                 return matched
             name_parts = name.split(os.sep)
             # Using posix normpath here to remove duplicates
@@ -182,7 +188,8 @@
                 if self.exists(hashed_name):
                     self.delete(hashed_name)
 
-                saved_name = self._save(hashed_name, ContentFile(content))
+                content_file = ContentFile(smart_str(content))
+                saved_name = self._save(hashed_name, content_file)
                 hashed_name = force_unicode(saved_name.replace('\\', '/'))
                 processed_files.append(hashed_name)
 

Added: 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.eot
===================================================================
--- 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.eot
                            (rev 0)
+++ 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.eot
    2011-12-27 22:49:24 UTC (rev 17282)
@@ -0,0 +1 @@
+not really a EOT ;)
\ No newline at end of file

Added: 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.svg
===================================================================
--- 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.svg
                            (rev 0)
+++ 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fonts/font.svg
    2011-12-27 22:49:24 UTC (rev 17282)
@@ -0,0 +1 @@
+not really a SVG ;)
\ No newline at end of file

Added: 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fragments.css
===================================================================
--- 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fragments.css
                             (rev 0)
+++ 
django/trunk/tests/regressiontests/staticfiles_tests/project/documents/cached/css/fragments.css
     2011-12-27 22:49:24 UTC (rev 17282)
@@ -0,0 +1,8 @@
+@font-face {
+    src: url('fonts/font.eot?#iefix') format('embedded-opentype'),
+         url('fonts/font.svg#webfontIyfZbseF') format('svg');
+         
url('data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA');
+}
+div {
+    behavior: url("#default#VML");
+}

Modified: django/trunk/tests/regressiontests/staticfiles_tests/storage.py
===================================================================
--- django/trunk/tests/regressiontests/staticfiles_tests/storage.py     
2011-12-27 15:35:41 UTC (rev 17281)
+++ django/trunk/tests/regressiontests/staticfiles_tests/storage.py     
2011-12-27 22:49:24 UTC (rev 17282)
@@ -4,7 +4,7 @@
 class DummyStorage(storage.Storage):
     """
     A storage class that does implement modified_time() but raises
-    NotImplementedError when calling 
+    NotImplementedError when calling
     """
     def _save(self, name, content):
         return 'dummy'

Modified: django/trunk/tests/regressiontests/staticfiles_tests/tests.py
===================================================================
--- django/trunk/tests/regressiontests/staticfiles_tests/tests.py       
2011-12-27 15:35:41 UTC (rev 17281)
+++ django/trunk/tests/regressiontests/staticfiles_tests/tests.py       
2011-12-27 22:49:24 UTC (rev 17282)
@@ -331,6 +331,16 @@
             self.assertNotIn("cached/other.css", content)
             self.assertIn("/static/cached/other.d41d8cd98f00.css", content)
 
+    def test_path_with_querystring_and_fragment(self):
+        relpath = self.cached_file_path("cached/css/fragments.css")
+        self.assertEqual(relpath, "cached/css/fragments.75433540b096.css")
+        with storage.staticfiles_storage.open(relpath) as relfile:
+            content = relfile.read()
+            
self.assertIn('/static/cached/css/fonts/font.a4b0478549d0.eot?#iefix', content)
+            
self.assertIn('/static/cached/css/fonts/font.b8d603e42714.svg#webfontIyfZbseF', 
content)
+            
self.assertIn('data:font/woff;charset=utf-8;base64,d09GRgABAAAAADJoAA0AAAAAR2QAAQAAAAAAAAAAAAA',
 content)
+            self.assertIn('#default#VML', content)
+
     def test_template_tag_absolute(self):
         relpath = self.cached_file_path("cached/absolute.css")
         self.assertEqual(relpath, "cached/absolute.cc80cb5e2eb1.css")

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to