Skip to content
This repository has been archived by the owner on Jan 30, 2019. It is now read-only.

Commit

Permalink
support anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
cemerick committed Apr 24, 2012
1 parent e8ec5eb commit 4645e54
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/cemerick/url.clj
Expand Up @@ -48,7 +48,7 @@
(str username ":" password)))

(defrecord URL
[protocol username password host port path query]
[protocol username password host port path query anchor]
Object
(toString [this]
(let [creds (url-creds username password)]
Expand All @@ -60,7 +60,8 @@
path
(when query (str \? (if (string? query)
query
(map->query query))))))))
(map->query query))))
(when anchor (str \# anchor))))))

(defn- normalize-path
[path]
Expand Down Expand Up @@ -89,7 +90,8 @@
(.getHost url)
(.getPort url)
(normalize-path (.getPath url))
(query->map (.getQuery url))))))
(query->map (.getQuery url))
(.getRef url)))))
([base-url & path-segments]
(let [base-url (if (instance? URL base-url) base-url (url base-url))]
(assoc base-url :path (normalize-path (reduce pathetic/resolve
Expand Down
7 changes: 6 additions & 1 deletion test/cemerick/test_url.clj
Expand Up @@ -3,7 +3,7 @@
(:use cemerick.url
clojure.test))

(def url-str (comp str url))
(def url-str (comp str #'url))

(deftest test-map-to-query-str
(are [x y] (= x (#'cemerick.url/map->query y))
Expand Down Expand Up @@ -51,3 +51,8 @@
(is (= "http://a" (str (url "http://a/b/c" "/"))))
(is (= "http://a" (str (url "http://a/b/c" "../.."))))
(is (= "http://a/x" (str (url "http://a/b/c" "../.." "." "./x")))))

(deftest anchors
(is (= "http://a#x" (url-str "http://a#x")))
(is (= "http://a?b=c#x" (url-str "http://a?b=c#x")))
(is (= "http://a?b=c#x" (-> "http://a#x" url (assoc :query {:b "c"}) str))))

0 comments on commit 4645e54

Please sign in to comment.