Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Preserve URL appendages in CSS files

They're needed for weird bugs with @font-face in certain browsers.
  • Loading branch information...
commit fb4e76c711b9e5590903d6ae3437b165036b1bdb 1 parent 7ad6422
@magnars authored
Showing with 16 additions and 9 deletions.
  1. +7 −6 src/optimus/assets/load_css.clj
  2. +9 −3 test/optimus/assets_test.clj
View
13 src/optimus/assets/load_css.clj
@@ -10,13 +10,10 @@
pathetic/render-path
pathetic/ensure-trailing-separator))
-(defn- remove-url-appendages [s]
- (first (str/split s #"[\?#]")))
-
-(defn- combine-paths [container-url relative-url]
+(defn- to-absolute-url [container-url relative-url]
(-> container-url
(just-the-path)
- (pathetic/resolve (remove-url-appendages relative-url))
+ (pathetic/resolve relative-url)
(pathetic/normalize)))
(def css-url-re #"(?:url\(['\"]?([^\)]+?)['\"]?\)|@import ['\"](.+?)['\"])")
@@ -35,17 +32,21 @@
(if (or (data-url? url)
(external-url? url))
match ;; leave alone
- (str/replace match url (combine-paths original-path url)))))
+ (str/replace match url (to-absolute-url original-path url)))))
(defn- make-css-urls-absolute [file]
(->> (partial match-url-to-absolute (original-path file))
(str/replace (:contents file) css-url-re)
(assoc-in file [:contents])))
+(defn- remove-url-appendages [s]
+ (first (str/split s #"[\?#]")))
+
(defn- paths-in-css [file]
(->> file :contents
(re-seq css-url-re)
(map url-match)
+ (map remove-url-appendages)
(remove data-url?)
(remove external-url?)))
View
12 test/optimus/assets_test.clj
@@ -145,13 +145,19 @@
(fact
"URLs can have querys and refs, but file paths can't. To find the
- files so we can serve them, these appendages have to be sliced off."
+ files so we can serve them, these appendages have to be sliced off.
+
+ Because they're needed for weird bugs with @font-face in certain
+ browsers tho, we need to preserve them in the actual URL."
(with-files [["/query.css" "#id { background: url(\"/bg.png?query\"); }"]
["/ref.css" "#id { background: url(/bg.png#ref); }"]
["/bg.png" "binary"]]
- (-> (load-assets public-dir ["/query.css"]) first :contents) => "#id { background: url(\"/bg.png\"); }"
- (-> (load-assets public-dir ["/ref.css"]) first :contents) => "#id { background: url(/bg.png); }"))
+ (->> (load-assets public-dir ["/query.css" "/ref.css"])
+ (map (juxt :contents :references))
+ (set)) => #{["#id { background: url(\"/bg.png?query\"); }" #{"/bg.png"}]
+ ["#id { background: url(/bg.png#ref); }" #{"/bg.png"}]
+ [nil nil]}))
(with-files [["/code.js" "1 + 2"]
["/more.js" "3 + 5"]]
Please sign in to comment.
Something went wrong with that request. Please try again.