Skip to content

Commit

Permalink
Merge pull request innoq#158 from mvitz/157-fix-hashtag-with-caret
Browse files Browse the repository at this point in the history
Fix innoq#157: caret in hashtag causes 500 error
  • Loading branch information
aheusingfeld committed Dec 29, 2014
2 parents 3ede96a + 09accfe commit 2129e7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
12 changes: 7 additions & 5 deletions src/statuses/views/common.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns statuses.views.common
(:require [hiccup.util :refer [escape-html]]))
(:require [hiccup.core :refer [html]]
[hiccup.element :refer [link-to mail-to]]
[hiccup.util :refer [escape-html url]]))

(defn icon [icon-name]
[:span {:class (str "fa fa-" icon-name)}])
Expand All @@ -10,10 +12,10 @@
(def email #"([a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)")

(defn linkify [text]
(let [handle (fn [[_ m]] (str "@<a href='/statuses/updates?author=" m "'>" m "</a>"))
hashtag (fn [[_ m]] (str "#<a href='/statuses/updates?query=%23" m "'>" m "</a>"))
anchor (fn [[m _]] (str "<a href='" m "'>" m "</a>"))
mailto (fn [[m _]] (str "<a href='mailto:" m "'>" m "</a>"))]
(letfn [(handle [[_ m]] (html "@" (link-to (url "/statuses/updates" {:author m}) m)))
(hashtag [[_ m]] (html "#" (link-to (url "/statuses/updates" {:query (str "#" m)}) m)))
(anchor [[m _]] (html (link-to m m)))
(mailto [[m _]] (html (mail-to m)))]
(-> text
escape-html
(clojure.string/replace #"(?:^|(?<=\s))@(\w+)" handle)
Expand Down
35 changes: 19 additions & 16 deletions test/linkification.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,63 @@
(deftest linkify-uris
(is (=
(linkify "lorem http://example.org ipsum")
"lorem <a href='http://example.org'>http://example.org</a> ipsum"))
"lorem <a href=\"http://example.org\">http://example.org</a> ipsum"))
(is (=
(linkify "http://example.org lipsum")
"<a href='http://example.org'>http://example.org</a> lipsum"))
"<a href=\"http://example.org\">http://example.org</a> lipsum"))
(is (=
(linkify "lipsum http://example.org")
"lipsum <a href='http://example.org'>http://example.org</a>")))
"lipsum <a href=\"http://example.org\">http://example.org</a>")))

(deftest linkify-hashtags
(is (=
(linkify "lorem #hashtag ipsum")
"lorem #<a href='/statuses/updates?query=%23hashtag'>hashtag</a> ipsum"))
"lorem #<a href=\"/statuses/updates?query=%23hashtag\">hashtag</a> ipsum"))
(is (=
(linkify "#hashtag lipsum")
"#<a href='/statuses/updates?query=%23hashtag'>hashtag</a> lipsum"))
"#<a href=\"/statuses/updates?query=%23hashtag\">hashtag</a> lipsum"))
(is (=
(linkify "lipsum #hashtag")
"lipsum #<a href='/statuses/updates?query=%23hashtag'>hashtag</a>")))
"lipsum #<a href=\"/statuses/updates?query=%23hashtag\">hashtag</a>"))
(is (=
(linkify "#WTF^2")
"#<a href=\"/statuses/updates?query=%23WTF%5E2\">WTF^2</a>")))

(deftest linkify-uris-with-fragment-identifier
(is (=
(linkify "lorem http://example.org#anchor ipsum")
"lorem <a href='http://example.org#anchor'>http://example.org#anchor</a> ipsum")))
"lorem <a href=\"http://example.org#anchor\">http://example.org#anchor</a> ipsum")))
(is (=
(linkify "#hashtag lipsum http://example.org#anchor-name")
"#<a href='/statuses/updates?query=%23hashtag'>hashtag</a> lipsum <a href='http://example.org#anchor-name'>http://example.org#anchor-name</a>"))
"#<a href=\"/statuses/updates?query=%23hashtag\">hashtag</a> lipsum <a href=\"http://example.org#anchor-name\">http://example.org#anchor-name</a>"))
(is (=
(linkify "lipsum http://example.org#anchor-name #hashtag")
"lipsum <a href='http://example.org#anchor-name'>http://example.org#anchor-name</a> #<a href='/statuses/updates?query=%23hashtag'>hashtag</a>"))
"lipsum <a href=\"http://example.org#anchor-name\">http://example.org#anchor-name</a> #<a href=\"/statuses/updates?query=%23hashtag\">hashtag</a>"))

(deftest linkify-email-addresses
(is (=
(linkify "hello [email protected] how are you")
"hello <a href='mailto:[email protected]'>[email protected]</a> how are you")))
"hello <a href=\"mailto:[email protected]\">[email protected]</a> how are you")))

(deftest linkify-mentions
(is (=
(linkify "@foo")
"@<a href='/statuses/updates?author=foo'>foo</a>"))
"@<a href=\"/statuses/updates?author=foo\">foo</a>"))
(is (=
(linkify "@foo how are you")
"@<a href='/statuses/updates?author=foo'>foo</a> how are you"))
"@<a href=\"/statuses/updates?author=foo\">foo</a> how are you"))
(is (=
(linkify "how are you @foo")
"how are you @<a href='/statuses/updates?author=foo'>foo</a>"))
"how are you @<a href=\"/statuses/updates?author=foo\">foo</a>"))
(is (=
(linkify "nice to see @foo again")
"nice to see @<a href='/statuses/updates?author=foo'>foo</a> again"))
"nice to see @<a href=\"/statuses/updates?author=foo\">foo</a> again"))
(is (=
(linkify "@?")
"@?"))
(is (=
(linkify "@foo: test")
"@<a href='/statuses/updates?author=foo'>foo</a>: test"))
"@<a href=\"/statuses/updates?author=foo\">foo</a>: test"))
(is (=
(linkify "@foo.bar test")
"@<a href='/statuses/updates?author=foo'>foo</a>.bar test")))
"@<a href=\"/statuses/updates?author=foo\">foo</a>.bar test")))
2 changes: 1 addition & 1 deletion test/statuses/views/test/atom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

(is (= (get (entry (feed-with-entry {:text "@bar: http://www.test.de"})) 3)
[:content {:type "html"}
"@&lt;a href=&apos;/statuses/updates?author=bar&apos;&gt;bar&lt;/a&gt;: &lt;a href=&apos;http://www.test.de&apos;&gt;http://www.test.de&lt;/a&gt;"])
"@&lt;a href=&quot;/statuses/updates?author=bar&quot;&gt;bar&lt;/a&gt;: &lt;a href=&quot;http://www.test.de&quot;&gt;http://www.test.de&lt;/a&gt;"])
"content is linkified and escaped")

(is (= (get (entry (feed-with-entry {:id 1337})) 4)
Expand Down

0 comments on commit 2129e7e

Please sign in to comment.