Permalink
Browse files

Ported element namespace.

  • Loading branch information...
1 parent 4ac6c3c commit 45ef6e6abadd4b39a1da6093f1e28971ae2afe39 @r0man committed Apr 7, 2012
View
@@ -21,7 +21,6 @@
:source-path "src/cljs"}
:test
{:compiler {:output-to "target/hiccup-test.js"
- :optimizations :whitespace
:pretty-print true}
:source-path "test/cljs"}}
:crossovers []
View
@@ -6,3 +6,14 @@
[base-url & body]
`(binding [hiccup.util/*base-url* ~base-url]
~@body))
+
+(defmacro defelem
+ "Defines a function that will return a element vector. If the first argument
+ passed to the resulting function is a map, it merges it with the attribute
+ map of the returned element value."
+ [name & fdecl]
+ (let [ns-name (-> &env :ns :name)
+ var (symbol (str ns-name "/" name))]
+ `(do (defn ~name ~@fdecl)
+ ;; (alter-meta! (var ~name) update-in [:arglists] #'update-arglists)
+ (set! ~var (hiccup.def/wrap-attrs ~var)))))
View
@@ -0,0 +1,16 @@
+(ns hiccup.def)
+
+(defn wrap-attrs
+ "Add an optional attribute argument to a function that returns a element vector."
+ [func]
+ (fn [& args]
+ (if (map? (first args))
+ (let [[tag & body] (apply func (rest args))]
+ (if (map? (first body))
+ (apply vector tag (merge (first body) (first args)) (rest body))
+ (apply vector tag (first args) body)))
+ (apply func args))))
+
+(defn update-arglists [arglists]
+ (for [args arglists]
+ (vec (cons 'attr-map? args))))
@@ -0,0 +1,36 @@
+(ns hiccup.element
+ (:use [hiccup.util :only (to-uri)])
+ (:use-macros [hiccup.macro :only (defelem)]))
+
+(defn javascript-tag
+ "Wrap the supplied javascript up in script tags and a CDATA section."
+ [script]
+ [:script {:type "text/javascript"}
+ (str "//<![CDATA[\n" script "\n//]]>")])
+
+(defelem link-to
+ "Wraps some content in a HTML hyperlink with the supplied URL."
+ [url & content]
+ [:a {:href (to-uri url)} content])
+
+(defelem mail-to
+ "Wraps some content in a HTML hyperlink with the supplied e-mail
+ address. If no content provided use the e-mail address as content."
+ [e-mail & [content]]
+ [:a {:href (str "mailto:" e-mail)}
+ (or content e-mail)])
+
+(defelem unordered-list
+ "Wrap a collection in an unordered list."
+ [coll]
+ [:ul (for [x coll] [:li x])])
+
+(defelem ordered-list
+ "Wrap a collection in an ordered list."
+ [coll]
+ [:ol (for [x coll] [:li x])])
+
+(defelem image
+ "Create an image element."
+ ([src] [:img {:src (to-uri src)}])
+ ([src alt] [:img {:src (to-uri src), :alt alt}]))
@@ -1,9 +1,13 @@
(ns hiccup.util
- ;; "Utility functions for Hiccup."
(:require [clojure.string :as str]
[goog.string :as string]
[goog.Uri :as Uri]))
+(extend-type goog.Uri
+ IEquiv
+ (-equiv [uri other]
+ (-equiv (str uri) (str other))))
+
(def ^:dynamic *base-url* nil)
(defprotocol ToString
View
@@ -1,39 +1,47 @@
if (phantom.args.length != 1) {
- console.log('Expected a target URL parameter.');
- phantom.exit(1);
+ console.log('Expected a target URL parameter.');
+ phantom.exit(1);
}
var page = require('webpage').create();
var url = phantom.args[0];
page.onConsoleMessage = function (message) {
- console.log("Test console: " + message);
+ console.log("Test console: " + message);
};
+page.onError = function (msg, trace) {
+ console.log(msg);
+ trace.forEach(function(item) {
+ console.log(' ', item.file, ':', item.line);
+ })
+}
+
console.log("Loading URL: " + url);
page.open(url, function (status) {
- if (status != "success") {
- console.log('Failed to open ' + url);
- phantom.exit(1);
- }
+ if (status != "success") {
+ console.log('Failed to open ' + url);
+ phantom.exit(1);
+ }
+
+ console.log("Running test.");
- console.log("Running test.");
+ var result = page.evaluate(function() {
+ return hiccup.test.run();
+ });
- var result = page.evaluate(function() {
- return hiccup.test.run();
- });
+ // NOTE: PhantomJS 1.4.0 has a bug that prevents the exit codes
+ // below from being returned properly. :(
+ //
+ // http://code.google.com/p/phantomjs/issues/detail?id=294
- // NOTE: PhantomJS 1.4.0 has a bug that prevents the exit codes
- // below from being returned properly. :(
- //
- // http://code.google.com/p/phantomjs/issues/detail?id=294
+ if (result != 0) {
+ console.log("*** Test failed! ***");
+ phantom.exit(1);
+ }
- if (result != 0) {
- console.log("*** Test failed! ***");
- phantom.exit(1);
- }
+ console.log("Test succeeded.");
+ phantom.exit(0);
- console.log("Test succeeded.");
- phantom.exit(0);
});
@@ -33,4 +33,3 @@
[:ol (list [:li "foo"]
[:li "bar"]
[:li "baz"])])))
-
@@ -1,7 +1,9 @@
(ns hiccup.test
(:require [hiccup.test.core :as core]
+ [hiccup.test.element :as element]
[hiccup.test.util :as util]))
(defn run []
(core/test)
- (util/test))
+ (element/test)
+ (util/test))
@@ -0,0 +1,41 @@
+(ns hiccup.test.element
+ (:require [goog.Uri :as Uri])
+ (:use [hiccup.element :only (javascript-tag link-to mail-to unordered-list ordered-list)]))
+
+(defn javascript-tag-test []
+ (assert (= (javascript-tag "alert('hello');")
+ [:script {:type "text/javascript"}
+ "//<![CDATA[\nalert('hello');\n//]]>"])))
+
+(defn link-to-test []
+ (assert (= (link-to "/")
+ [:a {:href (goog.Uri. "/")} nil]))
+ (assert (= (link-to "/" "foo")
+ [:a {:href (goog.Uri. "/")} (list "foo")]))
+ (assert (= (link-to "/" "foo" "bar")
+ [:a {:href (goog.Uri. "/")} (list "foo" "bar")])))
+
+(defn mail-to-test []
+ (assert (= (mail-to "foo@example.com")
+ [:a {:href "mailto:foo@example.com"} "foo@example.com"]))
+ (assert (= (mail-to "foo@example.com" "foo")
+ [:a {:href "mailto:foo@example.com"} "foo"])))
+
+(defn unordered-list-test []
+ (assert (= (unordered-list ["foo" "bar" "baz"])
+ [:ul (list [:li "foo"]
+ [:li "bar"]
+ [:li "baz"])])))
+
+(defn ordered-list-test []
+ (assert (= (ordered-list ["foo" "bar" "baz"])
+ [:ol (list [:li "foo"]
+ [:li "bar"]
+ [:li "baz"])])))
+
+(defn test []
+ (javascript-tag-test)
+ (link-to-test)
+ (mail-to-test)
+ (unordered-list-test)
+ (ordered-list-test))

0 comments on commit 45ef6e6

Please sign in to comment.