mirror of
https://github.com/penpot/penpot.git
synced 2026-04-08 04:12:40 +02:00
🐛 Fix null element crash in get-attrs-from-styles
Guard against null element in get-attrs-from-styles so that when the DOM node is not available the function returns the provided defaults instead of throwing 'Cannot read properties of null (reading style)'. Signed-off-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
@@ -38,21 +38,23 @@
|
||||
|
||||
(defn get-attrs-from-styles
|
||||
[element attrs defaults]
|
||||
(let [attrs (or attrs [])
|
||||
value-empty? (fn [v]
|
||||
(or (nil? v)
|
||||
(and (string? v) (empty? v))
|
||||
(and (coll? v) (empty? v))))]
|
||||
(reduce (fn [acc key]
|
||||
(let [style (.-style element)
|
||||
value (if (contains? styles/mapping key)
|
||||
(let [style-name (styles/get-style-name-as-css-variable key)
|
||||
[_ style-decode] (get styles/mapping key)]
|
||||
(style-decode (.getPropertyValue style style-name)))
|
||||
(let [style-name (styles/get-style-name key)]
|
||||
(styles/normalize-attr-value key (.getPropertyValue style style-name))))]
|
||||
(assoc acc key (if (value-empty? value) (get defaults key) value))))
|
||||
{} attrs)))
|
||||
(if (nil? element)
|
||||
(or defaults {})
|
||||
(let [attrs (or attrs [])
|
||||
value-empty? (fn [v]
|
||||
(or (nil? v)
|
||||
(and (string? v) (empty? v))
|
||||
(and (coll? v) (empty? v))))]
|
||||
(reduce (fn [acc key]
|
||||
(let [style (.-style element)
|
||||
value (if (contains? styles/mapping key)
|
||||
(let [style-name (styles/get-style-name-as-css-variable key)
|
||||
[_ style-decode] (get styles/mapping key)]
|
||||
(style-decode (.getPropertyValue style style-name)))
|
||||
(let [style-name (styles/get-style-name key)]
|
||||
(styles/normalize-attr-value key (.getPropertyValue style style-name))))]
|
||||
(assoc acc key (if (value-empty? value) (get defaults key) value))))
|
||||
{} attrs))))
|
||||
|
||||
(defn get-text-span-styles
|
||||
[element]
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
[frontend-tests.util-object-test]
|
||||
[frontend-tests.util-range-tree-test]
|
||||
[frontend-tests.util-simple-math-test]
|
||||
[frontend-tests.util-text-from-dom-test]
|
||||
[frontend-tests.worker-snap-test]))
|
||||
|
||||
(enable-console-print!)
|
||||
@@ -52,5 +53,6 @@
|
||||
'frontend-tests.util-object-test
|
||||
'frontend-tests.util-range-tree-test
|
||||
'frontend-tests.util-simple-math-test
|
||||
'frontend-tests.util-text-from-dom-test
|
||||
'frontend-tests.tokens.workspace-tokens-remap-test
|
||||
'frontend-tests.worker-snap-test))
|
||||
|
||||
21
frontend/test/frontend_tests/util_text_from_dom_test.cljs
Normal file
21
frontend/test/frontend_tests/util_text_from_dom_test.cljs
Normal file
@@ -0,0 +1,21 @@
|
||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS INC
|
||||
|
||||
(ns frontend-tests.util-text-from-dom-test
|
||||
(:require
|
||||
[app.common.types.text :as txt]
|
||||
[app.util.text.content.from-dom :as fd]
|
||||
[cljs.test :as t]))
|
||||
|
||||
(t/deftest get-attrs-from-styles-with-null-element
|
||||
(t/testing "returns defaults when element is nil"
|
||||
(let [defaults txt/default-root-attrs
|
||||
result (fd/get-attrs-from-styles nil txt/root-attrs defaults)]
|
||||
(t/is (= defaults result))))
|
||||
|
||||
(t/testing "returns empty map when element and defaults are both nil"
|
||||
(let [result (fd/get-attrs-from-styles nil [] nil)]
|
||||
(t/is (= {} result)))))
|
||||
Reference in New Issue
Block a user