Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Differences from Hiccup
functions, and `runtime.cljs` contains functions that are also available at runtime. The contents
of `runtime.cljs` are also used at compile-time, so the goal is to keep it portable between
ClojureScript and Clojure.
* HTML is escaped by default, use `:dangerously-set-inner-HTML` to explicitly disable escaping.
* Unit tests are run in a PhantomJS browser using [lein-cljsbuild](https://github.com/emezeske/lein-cljsbuild/) and Closure's testing libs.
* Not everything has been ported yet. See ToDo.

Expand All @@ -35,9 +36,11 @@ Install

Add the following dependency to your `project.clj` file:

```clojure
[hiccups "0.3.0"]
```
[![Clojars Project](https://img.shields.io/clojars/v/macchiato/hiccups.svg)](https://clojars.org/macchiato/hiccups)

alternatively via NPM

[![NPM](https://nodei.co/npm/hiccups.png?mini=true)](https://www.npmjs.com/package/hiccups)

Usage
-----
Expand All @@ -47,7 +50,7 @@ Require both the core macros and the runtime functions in your namespace declara
```clojure
(ns myns
(:require-macros [hiccups.core :as hiccups :refer [html]])
(:require [hiccups.runtime :as hiccupsrt]))
(:require [hiccups.runtime :as hiccups]))

(hiccups/defhtml my-template []
[:div
Expand Down Expand Up @@ -98,9 +101,17 @@ convenient:
[:li x])])
"<ul><li>1</li><li>2</li><li>3</li></ul>"
```

Note that while lists are considered to be seqs in Clojure(Script), vectors and sets are not. As a consequence, Hiccups will bail out if a vector is passed in without a tag: `[[:div] [:div]]`.

Disabling HTML escaping is accomplished by using React inspired `:dangerously-set-inner-HTML` attribute:

```clojure
[:div
{:dangerously-set-inner-HTML
{:__html "<p>safe html</p>"}}]
```
=======

See the [Hiccup wiki](https://github.com/weavejester/hiccup/wiki) for more information.

ToDo
Expand Down
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "hiccups",
"version": "0.4.3",
"description": "A library for rendering HTML in ClojureScript ",
"author": "Dmitri Sotnokov",
"homepage": "https://github.com/macchiato-framework/hiccups",
"license": "EPL-1.0",
"directories": {
"lib": "src"
},
"files": [
"src/*"
],
"keywords": [
"cljs",
"cljc",
"html",
"templating"
]
}
78 changes: 31 additions & 47 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,54 +1,38 @@
(defproject hiccups "0.3.1"
:description "A ClojureScript port of Hiccup - a fast library for rendering HTML in ClojureScript"
:url "http://example.com/FIXME"
(defproject macchiato/hiccups "0.4.2"
:description "a library for rendering HTML in ClojureScript using Hiccup syntax"
:url "https://github.com/macchiato-framework/hiccups"
:license {:name "Eclipse Public License - v 1.0"
:url "http://www.eclipse.org/legal/epl-v10.html"
:distribution :repo}
:url "http://www.eclipse.org/legal/epl-v10.html"}

;; Required by cljsbuild plugin
:min-lein-version "2.2.0"
:clojurescript? true
:source-paths ["src"]

;; We need to add src/cljs too, because cljsbuild does not add its
;; source-paths to the project source-paths
:source-paths ["src/clj" "src/cljs"]
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/clojurescript "1.10.597"]]

;; The libs which the project depends on.
;; Here we use the latest stable clj and cljs releases
:dependencies [[org.clojure/clojure "1.5.1"]
[org.clojure/clojurescript "0.0-2069"]]
:plugins [[lein-doo "0.1.7"]
[lein-cljsbuild "1.1.4"]]

;; The plugins which the project depends on. Here we're using the
;; experimental 1.0.0-alpha2 release. Change it to the official
;; 1.0.0 as soon as it will be available
:plugins [[lein-cljsbuild "1.0.0"]]
:profiles {:test
{:cljsbuild
{:builds
{:test
{:source-paths ["src" "test/cljs"]
:compiler {:main hiccups.runner
:output-to "target/test/core.js"
:target :nodejs
:optimizations :none
:source-map true
:pretty-print true}}}}
:doo {:build "test"}}}

;; Hooks the cljsbuild subtasks to the lein tasks: lein clean, lein
;; compile, lein test and lein jar
:hooks [leiningen.cljsbuild]

;; Lein-cljsbuild plugin configuration. Here we define one build
;; only: the one used for packaging any cljs sources in the jar
;; generated by lein jar command
:cljsbuild
{:builds {;; This build is only used for including any cljs source
;; in the packaged jar when you issue lein jar command and
;; any other command that depends on it
:useless
{;; The dir pathnames where cljs sources live. Do not
;; include here any cljs source which is only used during
;; development or testing
:source-paths ["src/cljs"]
;; The :jar true option is not needed to include the CLJS
;; sources in the packaged jar. This is because we added
;; the CLJS source codebase to the Leiningen
;; :source-paths
;:jar true
;; Compilation Options
:compiler
{;; The JS file pathname emitted by the compiler
:output-to "dev-resources/public/js/useless.js"
;; Compiler optimizations option set to :none to speed
;; up this useless compilation
:optimizations :none
;; No need to prettify the emitted js file
:pretty-print false}}}})
:aliases
{"test"
["do"
["clean"]
["with-profile" "test" "doo" "node" "once"]]
"test-watch"
["do"
["clean"]
["with-profile" "test" "doo" "node"]]})
3 changes: 1 addition & 2 deletions src/clj/hiccups/core.clj → src/hiccups/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"Library for rendering a tree of vectors into a string of HTML.
Pre-compiles where possible for performance.
Core macros and their (Clojure) helper functions."
(:require [hiccups.runtime :as rt])
(:import [clojure.lang IPersistentVector ISeq]))
(:require [hiccups.runtime :as rt]))

(def doctype
{:html4
Expand Down
File renamed without changes.
14 changes: 10 additions & 4 deletions src/cljs/hiccups/runtime.cljs → src/hiccups/runtime.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
(true? value)
(if (xml-mode?)
(xml-attribute name name)
(str " " (as-str name)))
(str " " (escape-html name)))
(not value)
""
:else
(xml-attribute name (if (map? value) (render-attr-map value) value) false)))
(xml-attribute name (if (map? value) (render-attr-map value) (escape-html value)) false)))

(defn render-attr-map [attrs]
(apply str
Expand All @@ -79,10 +79,16 @@
"Render a tag vector as a HTML element."
[element]
(let [[tag attrs content] (normalize-element element)]
(if (or content (container-tags tag))
(cond
(:dangerouslySetInnerHTML attrs)
(-> attrs :dangerouslySetInnerHTML :__html)
(:dangerously-set-inner-HTML attrs)
(-> attrs :dangerously-set-inner-HTML :__html)
(or content (container-tags tag))
(str "<" tag (render-attr-map attrs) ">"
(render-html content)
"</" tag ">")
:else
(str "<" tag (render-attr-map attrs) (end-tag)))))

(defn render-html
Expand All @@ -91,4 +97,4 @@
(cond
(vector? x) (render-element x)
(seq? x) (apply str (map render-html x))
:else (as-str x)))
:else (escape-html x)))
Loading