Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses CSSStyleDeclarations to get styles instead of parsing style string. #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Uses CSSStyleDeclarations to get styles instead of parsing style string.
  • Loading branch information
akilism committed Jul 16, 2015
commit 5149503b96d5839bcdf9f142563760d6446b11b8
32 changes: 15 additions & 17 deletions src/cljs/domina.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,27 @@
content)

;; We don't use the existing style/parseStyleAttributes because it camelcases everything.
;; This uses the same technique, however.
(defn parse-style-attributes
"Parses a CSS style string and returns the properties as a map."
"Returns a map of a single css name/value pair."
[style]
(reduce (fn [acc pair]
(let [[k v] (. pair split #"\s*:\s*")]
(if (and k v)
(assoc acc (keyword (. k (toLowerCase))) v)
acc)))
{}
(. style split #"\s*;\s*")))
(let [length (.-length style)]
(apply merge (map
(fn [i]
(let [name (.item style i)
value (.getPropertyValue style name)
priority (.getPropertyPriority style name)]
(if (= "" priority) {(keyword name) value} {(keyword name) (str value " " priority)})))
(range length)))))


(defn styles
"Returns a map of the CSS styles/values. Assumes content will be a single node. Style names are returned as keywords."
[content]
(let [style (attr content "style")]
(cond (string? style)
(parse-style-attributes style)
(nil? style)
{}
(. style -cssText)
(parse-style-attributes (. style -cssText))
:else {})))
(let [style (.-style (single-node content))
length (.-length style)]
(cond
(pos? length) (parse-style-attributes style)
:else {})))

(defn attrs
"Returns a map of the HTML attributes/values. Assumes content will be a single node. Attribute names are returned as keywords."
Expand Down
8 changes: 8 additions & 0 deletions test/cljs/domina/test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@
(assert (= {}
(styles (xpath "//div"))))))

(add-test "can get CSS styles with ':' in property from a single node."
#(do (reset)
(append! (xpath "//body") "<div>1</div>")
(set-style! (xpath "//div") "background-image" "url(http://www.some-site.com:8088/image.jpg)")
(set-style! (xpath "//div") "background-color" "black")
(assert (= {:background-image "url(http://www.some-site.com:8088/image.jpg)" :background-color "black"}
(styles (xpath "//div"))))))

(add-test "can get multiple HTML attributes from a single node."
#(do (reset)
(append! (xpath "//body") "<div>1</div>")
Expand Down