weavejester / hiccup

Fast library for rendering HTML in Clojure
http://weavejester.github.io/hiccup
Eclipse Public License 1.0
2.68k stars 174 forks source link

attributes in a seq doesn't get expanded in the element but converted to string as body #78

Closed nahuel closed 11 years ago

nahuel commented 11 years ago

This works ok:

(html [:p '([:pre])])    ;=> "<p><pre></pre></p>"

But using a seq with element attributes doesn't expand in the element, it's converted to a plain string:

(html [:p '({:bgcolor "red"})])  ;=> "<p>{:bgcolor \"red\"}</p>"

I think is not an homogeneous behaviour. It's intentional or this is a bug?

weavejester commented 11 years ago

Seqs are only allowed in the contents of element vectors. It doesn't make syntactic sense to encase attributes in a seq.

So it's intentional :)

nahuel commented 11 years ago

I think I distilled too much the example, losing my intention :), I was trying to use the seq expanding facility to return attributes + element content from a function, like:

 (defn myf [] (seq '({:bgcolor "red"} "p content")))
 (html [:p (myf)])
 ;=> I expected  "<p bgcolor=red>p content</p>" but got "<p>{:bgcolor \"red\"}p content</p>"

Obviously I can fix this by passing the tag name to myf and making myf return all the element vector, but would be nice if the seq expansion worked in the same way it do with elements. So consider this as a suggestion, not as a bug report :)

weavejester commented 11 years ago

This seems an fairly rare use-case, and it feels semantically wrong to group the attributes and content together in a seq. It will also impact performance, as it prevents some compiler optimisations.

For those reasons I think I'll keep the current behaviour the way it is. I'm afraid you'll need to pass the tag name to your myf function.

nahuel commented 11 years ago

I understand, thanks for the reply!