Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow setting arbitrary properties on the html5 root element #71

Merged
merged 1 commit into from

3 participants

@algernon

Allow setting arbitrary properties on the html5 root element

Update: Reworked the patch so that it doesn't use :root-options anymore, but dissocs :xml? instead.
Update#2: Another rework, uses defelem for xhtml-tags

@algernon

This is the fix for #70

@weavejester
Owner

I'd prefer the attributes be merged together. i.e. instead of:

(html5 {:lang "en" :root-options {:prefix "blah"}} ...)

I'd prefer:

(html5 {:lang "en" :prefix "blah"} ...)
@algernon

I thought about that too, but dismissed it due to :xml? - but I suppose I could just dissoc that. I'll update the patch shortly.

(The reason I don't want to special case :prefix, is because in xml mode, one would need "xml:og" "blah" instead of :prefix "og: blah")

@weavejester
Owner

Since attributes can't include "?" it should be safe just to dissoc :xml?.

This also fits in a little better with :lang, which is essentially an attribute with some special additions for XML.

@weavejester
Owner

Instead of having a new xhtml-tag-with-options function, we could change xhtml-tag to use defelem so that it has an optional map of attributes. This would be more in line with other functions in Hiccup.

@algernon

I didn't want to change xhtml-tag because it's not a private function, and perhaps someone somewhere relies on it not having options. I don't mind updating the patch to transform xhtml-tag instead, though.

@weavejester
Owner

That's a good policy to have in general, but defelem adds an optional attribute map, by checking to see if the first argument to the function is a map. Because xhtml-tag takes a string (lang) as it's first argument, this shouldn't impact any existing use of the function.

@algernon algernon Allow setting arbitrary properties on the html5 root element
With this change, the options arguemnt of the html5 macro can contain
any option, not just :lang, and they all will be used to form the
properties of the <html> element (except for the :xml? option).

This is accomplished by using defelem for xhtml-tag, and later in the
html5 macro, passing options (sans :xml?) to it as-is.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
0d5eae2
@weavejester weavejester merged commit 9c3628a into weavejester:master

1 check passed

Details default The Travis build passed
@semperos

I'm a little confused. Using hiccup 1.0.2, should this:

(html5 {:foo "bar"} [:body])

...produce this?

<!DOCTYPE html>\n<html><body></body></html>

I was expecting to see:

<!DOCTYPE html>\n<html foo="bar"><body></body></html>

...but I get what you see in the first HTML snippet above. Am I using this function incorrectly?

@algernon

The feature did not make it into 1.0.2, but was merged shortly after that version was tagged and released, as far as I remember. You are using the functionality correctly, but 1.0.2 does not support it, sadly.

@semperos

Understood, thanks for the response. Didn't think to check where it fell relative to the last release, since it's been a few months.

For anyone stumbling on this thread needing to accomplish this now, here's a simple workaround:

(html
  (:html5 doctype)
  [:html {:foo "bar"}
    [:head ,,,]
    [:body ,,,]])
@weavejester
Owner

1.0.3 should have been released a while ago, but it looks like it slipped off my todo list. I've now release 1.0.3 that should work as expected.

@semperos

Great, thank you @weavejester , it works as expected in 1.0.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 4, 2012
  1. @algernon

    Allow setting arbitrary properties on the html5 root element

    algernon authored
    With this change, the options arguemnt of the html5 macro can contain
    any option, not just :lang, and they all will be used to form the
    properties of the <html> element (except for the :xml? option).
    
    This is accomplished by using defelem for xhtml-tag, and later in the
    html5 macro, passing options (sans :xml?) to it as-is.
    
    Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 7 deletions.
  1. +7 −6 src/hiccup/page.clj
  2. +24 −1 test/hiccup/test/page.clj
View
13 src/hiccup/page.clj
@@ -1,7 +1,8 @@
(ns hiccup.page
"Functions for setting up HTML pages."
(:use hiccup.core
- hiccup.util))
+ hiccup.util
+ hiccup.def))
(def doctype
{:html4
@@ -16,7 +17,7 @@
:html5
"<!DOCTYPE html>\n"})
-(defn xhtml-tag
+(defelem xhtml-tag
"Create an XHTML element for the specified language."
[lang & contents]
[:html {:xmlns "http://www.w3.org/1999/xhtml"
@@ -58,15 +59,15 @@
(if-not (map? options)
`(html5 {} ~options ~@contents)
(if (options :xml?)
- `(let [options# ~options]
+ `(let [options# (dissoc ~options :xml?)]
(html {:mode :xml}
(xml-declaration (options# :encoding "UTF-8"))
(doctype :html5)
- (xhtml-tag (options# :lang) ~@contents)))
- `(let [options# ~options]
+ (xhtml-tag options# (options# :lang) ~@contents)))
+ `(let [options# (dissoc ~options :xml?)]
(html {:mode :html}
(doctype :html5)
- [:html {:lang (options# :lang)} ~@contents])))))
+ [:html options# ~@contents])))))
(defn include-js
"Include a list of external javascript files."
View
25 test/hiccup/test/page.clj
@@ -34,7 +34,18 @@
(is (= (html5 [:body [:p "Hello" [:br] "World"]])
"<!DOCTYPE html>\n<html><body><p>Hello<br>World</p></body></html>"))
(is (= (html5 {:lang "en"} [:body "Hello World"])
- "<!DOCTYPE html>\n<html lang=\"en\"><body>Hello World</body></html>")))
+ "<!DOCTYPE html>\n<html lang=\"en\"><body>Hello World</body></html>"))
+ (is (= (html5 {:prefix "og: http://ogp.me/ns#"}
+ [:body "Hello World"])
+ (str "<!DOCTYPE html>\n"
+ "<html prefix=\"og: http://ogp.me/ns#\">"
+ "<body>Hello World</body></html>")))
+ (is (= (html5 {:prefix "og: http://ogp.me/ns#"
+ :lang "en"}
+ [:body "Hello World"])
+ (str "<!DOCTYPE html>\n"
+ "<html lang=\"en\" prefix=\"og: http://ogp.me/ns#\">"
+ "<body>Hello World</body></html>"))))
(testing "XML mode"
(is (= (html5 {:xml? true} [:body [:p "Hello" [:br] "World"]])
(str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@@ -44,6 +55,18 @@
(str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE html>\n"
"<html lang=\"en\" xml:lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<body>Hello World</body></html>")))
+ (is (= (html5 {:xml? true,
+ "xml:og" "http://ogp.me/ns#"} [:body "Hello World"])
+ (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE html>\n"
+ "<html xml:og=\"http://ogp.me/ns#\" xmlns=\"http://www.w3.org/1999/xhtml\">"
+ "<body>Hello World</body></html>")))
+ (is (= (html5 {:xml? true, :lang "en"
+ "xml:og" "http://ogp.me/ns#"} [:body "Hello World"])
+ (str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<!DOCTYPE html>\n"
+ "<html lang=\"en\" xml:lang=\"en\" xml:og=\"http://ogp.me/ns#\" xmlns=\"http://www.w3.org/1999/xhtml\">"
"<body>Hello World</body></html>")))))
(deftest include-js-test
Something went wrong with that request. Please try again.