Skip to content

Commit

Permalink
Merge pull request #2562 from BetterThanTomorrow/2561-html2hiccup-sty…
Browse files Browse the repository at this point in the history
…le-ws

2561 html2hiccup style ws
  • Loading branch information
PEZ authored Jun 3, 2024
2 parents a769e64 + 0b01389 commit f0e06f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes to Calva.

## [Unreleased]

- Fix: [HTML->Hiccup fails on style attribute strings where the entries do not have whitespace](https://github.com/BetterThanTomorrow/calva/issues/2561)

## [2.0.453] - 2024-06-03

- Bump deps.clj to v1.11.3.1463
Expand Down
4 changes: 2 additions & 2 deletions src/cljs-lib/src/calva/html2hiccup.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

(defn- mapify-style [style-str]
(try
(into {} (for [[_ k v] (re-seq #"(\S+):\s+([^;]+);?" style-str)]
(into {} (for [[_ k v] (re-seq #"(\S+?)\s*:\s*([^;]+);?" style-str)]
[(-> k string/lower-case keyword) (normalize-css-value v)]))
(catch :default e
(js/console.warn "Failed to mapify style: '" style-str "'." (.-message e))
Expand Down Expand Up @@ -76,7 +76,7 @@
(string/split class #"\s+"))
[kw-classes remaining-classes] (if-not (:add-classes-to-tag-keyword? options)
[() classes]
(bisect-all-by valid-as-hiccup-kw? classes))
(bisect-all-by valid-as-hiccup-kw? classes))
tag-w-id+classes (build-tag-with-classes tag+id kw-classes)
remaining-attrs (cond-> normalized-attrs
:always (dissoc :class)
Expand Down
17 changes: 14 additions & 3 deletions src/cljs-lib/test/calva/html2hiccup_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
(sut/html->hiccup "<foo class='clz1 clz[2]'></foo>"))))
(testing "When only invalid kw-classes they all remain in class attr"
(is (= [[:foo {:class ["a/b" "clz[2]"]}]]
(sut/html->hiccup "<foo class='a/b clz[2]'></foo>"))))
(sut/html->hiccup "<foo class='a/b clz[2]'></foo>"))))
(testing "Attributes other than `class` and `id` get tucked in 'props' position"
(is (= [[:foo#foo-id.clz1.clz2 {:bar "2"} "baz"]]
(sut/html->hiccup "<foo id='foo-id' class='clz1 clz2' bar=2>baz</foo>"))))
Expand Down Expand Up @@ -78,7 +78,7 @@
(is (= [[:foo {:disabled "disabled"}]]
(sut/html->hiccup "<foo disabled='disabled'></foo>")))))

(deftest html->hiccup-wkebab-attrs?
(deftest html->hiccup-kebab-attrs?
(testing "camelCase attributes are kebab-cased with :kebab-attrs? enabled"
(is (= [[:foo {:on-change "bar" :max-height "10px"}]]
(sut/html->hiccup "<foo onChange='bar' maxHeight='10px'></foo>" {:kebab-attrs? true}))))
Expand All @@ -95,6 +95,17 @@
(is (= [[:foo {:onchange "bar"}]]
(sut/html->hiccup "<foo ONCHANGE='bar'></foo>" {:kebab-attrs? true})))))

(deftest html->hiccup-style
(testing "style attributes do not need whitespace between entries with mapify-style? disabled"
(is (= [[:foo {:style "color:blue"}]]
(sut/html->hiccup "<foo style='color:blue'></foo>" {:mapify-style? false}))))
(testing "style attributes do not need whitespace between entries with mapify-style? enabled"
(is (= [[:foo {:style {:color :blue}}]]
(sut/html->hiccup "<foo style='color:blue'></foo>" {:mapify-style? true})))
(is (= [[:foo {:style {:color :blue
:stroke :red}}]]
(sut/html->hiccup "<foo style='color:blue;stroke:red;'></foo>" {:mapify-style? true})))))

(deftest html->hiccup-w-mapify-style?
(testing "style attribute is mapified with :mapify-style? enabled"
(is (= [[:foo {:style {:color :blue}}]]
Expand All @@ -117,7 +128,7 @@
(testing "style attribute non-bare-word, non bare-numeric is stringified"
(is (= [[:foo {:style {:padding "var(--some-padding, 0 0)"}}]]
(sut/html->hiccup "<foo style='padding: var(--some-padding, 0 0);'></foo>" {:mapify-style? true})))))

(deftest html->hiccup-wo-add-classes-to-tag-keyword?
(testing "When the :add-classes-to-tag-keyword? option is false they all remain in class attr"
(is (= [[:foo {:class ["clz1" "clz2"]}]]
Expand Down

0 comments on commit f0e06f8

Please sign in to comment.