File tree 3 files changed +29
-5
lines changed
3 files changed +29
-5
lines changed Original file line number Diff line number Diff line change 372
372
:superseded-by " reagent.dom/render" }
373
373
[& _]
374
374
(throw (js/Error. " Reagent.core/render function was moved to reagent.dom namespace in Reagent v1.0." )))
375
+
376
+ (defn unsafe-html
377
+ " Create a tagged value for use with :dangerouslySetInnerHTML.
378
+ Reagent doesn't allow other values to be used with the property,
379
+ to ensure EDN and Transit data can't be used to accidentally
380
+ create arbitrary HTML."
381
+ [s]
382
+ (tmpl/UnsafeHTML. s))
Original file line number Diff line number Diff line change 9
9
[reagent.debug :refer-macros [dev? warn]]
10
10
[goog.object :as gobj]))
11
11
12
+ (deftype UnsafeHTML [__html])
13
+
12
14
; ; From Weavejester's Hiccup, via pump:
13
15
(def ^{:doc " Regular expression that parses a CSS-style id and class
14
16
from a tag name." }
118
120
(let [class (:class props)
119
121
props (-> props
120
122
(cond-> class (assoc :class (util/class-names class)))
121
- (set-id-class id-class))]
122
- (if (.-custom id-class)
123
- (convert-custom-prop-value props)
124
- (convert-prop-value props))))
123
+ (set-id-class id-class))
124
+ ^js js-props (if (.-custom id-class)
125
+ (convert-custom-prop-value props)
126
+ (convert-prop-value props))]
127
+ ; ; Ensure only tagged values are used for dangerouslySetInnerHTML
128
+ (when-let [d (and js-props (.-dangerouslySetInnerHTML js-props))]
129
+ (when-not (instance? UnsafeHTML d)
130
+ (js-delete js-props " dangerouslySetInnerHTML" )))
131
+ js-props))
125
132
126
133
; ;; Conversion from Hiccup forms
127
134
Original file line number Diff line number Diff line change 308
308
(as-string [:div.bar [:p " foo" ]])))
309
309
(is (= " <div class=\" bar\" ><p>foobar</p></div>"
310
310
(as-string [:div.bar {:dangerously-set-inner-HTML
311
- { :__html " <p>foobar</p>" } }]))))
311
+ ( r/unsafe-html " <p>foobar</p>" ) }]))))
312
312
313
313
(u/deftest ^:dom test-return-class
314
314
(let [ran (atom 0 )
1525
1525
16 ))))
1526
1526
[really-simple]]
1527
1527
u/fn-compiler)))))))
1528
+
1529
+ (u/deftest test-unsafe-html
1530
+ (testing " Regular value is ignored"
1531
+ (is (= " <div></div>"
1532
+ (as-string (r/as-element [:div {:dangerouslySetInnerHTML {:__html " <img/>" }}])))))
1533
+
1534
+ (testing " Tagged value is allowed"
1535
+ (is (= " <div><img/></div>"
1536
+ (as-string (r/as-element [:div {:dangerouslySetInnerHTML (r/unsafe-html " <img/>" )}]))))))
You can’t perform that action at this time.
0 commit comments