0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 23:18:48 -05:00

🐛 Fix rasterizer not loading embedded styles

This commit is contained in:
Aitor 2024-01-03 10:02:13 +01:00 committed by Andrey Antukh
parent 79c2a6c5d5
commit 5ad31a878b
2 changed files with 47 additions and 11 deletions

View file

@ -96,10 +96,16 @@
:method :get
:mode :cors
:omit-default-headers true})
(rx/map :body)
(rx/mapcat wapi/read-file-as-data-url)
(rx/tap (fn [data-uri]
(.set data-uri-cache uri (wapi/create-blob data-uri "text/plain")))))))
(rx/catch (fn [cause]
(log/error :hint "fetching data uri"
:cause cause)
(rx/of nil)))
(rx/mapcat (fn [response]
(if (nil? response)
(rx/of uri)
(->> (rx/of (:body response))
(rx/mapcat wapi/read-file-as-data-url)
(rx/tap (fn [data-uri] (.set data-uri-cache uri (wapi/create-blob data-uri "text/plain")))))))))))
(defn- svg-update-image!
"Updates an image in an SVG to a Data URI."
@ -128,27 +134,53 @@
(dom/append-child! style (dom/create-text svg styles))
(dom/append-child! doc style)))
(defn- svg-resolve-styles!
"Resolves all fonts in an SVG to Data URIs."
[svg styles]
(defn- svg-resolve-external-resources
"Resolves all external resources in an SVG to Data URIs."
[styles]
(->> (rx/from (re-seq #"url\((https?://[^)]+)\)" styles))
(rx/map second)
(rx/mapcat (fn [url]
(->> (fetch-as-data-uri url)
(rx/map (fn [uri] [url uri])))))
(->> (fetch-as-data-uri url)
(rx/map (fn [uri] [url uri])))))
(rx/reduce (fn [styles [url uri]]
(str/replace styles url uri))
styles)
styles)))
(defn- svg-resolve-styles!
"Resolves all fonts in an SVG to Data URIs."
[svg styles]
(->> (svg-resolve-external-resources styles)
(rx/tap (partial svg-add-style! svg))
(rx/ignore)))
(defn- svg-resolve-inline-styles!
"Resolves all inline styles in an SVG to Data URIs."
[svg]
(->> (rx/from (dom/query-all svg "[style]"))
(rx/mapcat (fn [node]
(let [styles (dom/get-attribute node "style")]
(->> (svg-resolve-external-resources styles)
(rx/tap (fn [styles] (dom/set-attribute! node "style" styles)))))))
(rx/ignore)))
(defn- svg-resolve-style-elements!
"Resolves all style elements in an SVG to Data URIs."
[svg]
(->> (rx/from (dom/query-all svg "style"))
(rx/mapcat (fn [node]
(let [styles (dom/get-text node)]
(->> (svg-resolve-external-resources styles)
(rx/tap (fn [styles] (dom/set-text! node styles)))))))
(rx/ignore)))
(defn- svg-resolve-all!
"Resolves all images and fonts in an SVG to Data URIs."
[svg styles]
(rx/concat
(svg-resolve-images! svg)
(svg-resolve-styles! svg styles)
(svg-resolve-inline-styles! svg)
(svg-resolve-style-elements! svg)
(rx/of svg)))
(defn- svg-parse

View file

@ -504,6 +504,10 @@
(.setAttribute node property value))
node)
(defn get-text [^js node]
(when (some? node)
(.-textContent node)))
(defn set-text! [^js node text]
(when (some? node)
(set! (.-textContent node) text))