From 4ec5f31b4c9c92987cbb0d2b33e1f1bbde42a980 Mon Sep 17 00:00:00 2001 From: "Felipe S. S. Schneider" Date: Wed, 26 May 2021 17:34:18 -0300 Subject: [PATCH 1/2] Use an extensible rich rendering system --- src/Hyperscript.jl | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Hyperscript.jl b/src/Hyperscript.jl index 598ee9c..2b6418d 100644 --- a/src/Hyperscript.jl +++ b/src/Hyperscript.jl @@ -237,9 +237,20 @@ renderdomchild(io, rctx::RenderContext, ctx::HTMLSVG, node::AbstractNode{HTMLSVG renderdomchild(io, rctx::RenderContext, ctx, x::Nothing) = nothing # Render and escape other HTMLSVG children, including CSS nodes, in the parent context. -# If a child is `showable` with text/html, render with that using `repr`. -renderdomchild(io, rctx::RenderContext, ctx, x) = - showable(MIME("text/html"), x) ? show(io, MIME("text/html"), x) : printescaped(io, x, escapechild(ctx)) +# If a child is `showable` with a supported MIME type, render with that using `showrich`. +function renderdomchild(io, rctx::RenderContext, ctx, x) + for mime in allmimes + if showable(mime, x) + return showrich(io, mime, x) + end + end + printescaped(io, x, escapechild(ctx)) +end + +# Supported MIME types in descending order of importance. +const allmimes = [MIME"text/html"()] + +showrich(io::IO, mime::MIME"text/html", x) = show(io, mime, x) # All camelCase attribute names from HTML 4, HTML 5, SVG 1.1, SVG Tiny 1.2, and SVG 2 const HTML_SVG_CAMELS = Dict(lowercase(x) => x for x in [ From 1fe2301ed526aaa65d5c88d1163be8f25e936b3d Mon Sep 17 00:00:00 2001 From: "Felipe S. S. Schneider" Date: Wed, 26 May 2021 18:24:19 -0300 Subject: [PATCH 2/2] Use base64 for images if possible --- Project.toml | 1 + src/Hyperscript.jl | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 3bfda6c..5c1526c 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ author = ["Yuri Vishnevsky "] version = "0.0.4" [deps] +Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] diff --git a/src/Hyperscript.jl b/src/Hyperscript.jl index 2b6418d..a583935 100644 --- a/src/Hyperscript.jl +++ b/src/Hyperscript.jl @@ -55,6 +55,8 @@ module Hyperscript +using Base64: stringmime + export @tags, @tags_noescape, m, css, Style, styles, render, Pretty, savehtml, savesvg # Units @@ -241,16 +243,24 @@ renderdomchild(io, rctx::RenderContext, ctx, x::Nothing) = nothing function renderdomchild(io, rctx::RenderContext, ctx, x) for mime in allmimes if showable(mime, x) - return showrich(io, mime, x) + showrich(io, mime, x) + return end end printescaped(io, x, escapechild(ctx)) end # Supported MIME types in descending order of importance. -const allmimes = [MIME"text/html"()] +const base64imagemimes = [MIME"image/png"(), MIME"image/jpg"(), MIME"image/jpeg"(), MIME"image/bmp"(), MIME"image/gif"()] +const imagemimes = [MIME"image/svg+xml"(), base64imagemimes...] +const allmimes = [MIME"text/html"(), imagemimes...] showrich(io::IO, mime::MIME"text/html", x) = show(io, mime, x) +for mime in base64imagemimes + @eval showrich(io::IO, mime::$(typeof(mime)), x) = + show(io, MIME("text/html"), m("img", src=datauri(mime, x))) +end +datauri(mime, x) = "data:$(mime);base64,$(stringmime(mime, x))" # All camelCase attribute names from HTML 4, HTML 5, SVG 1.1, SVG Tiny 1.2, and SVG 2 const HTML_SVG_CAMELS = Dict(lowercase(x) => x for x in [