Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ author = ["Yuri Vishnevsky <yurivish@gmail.com>"]
version = "0.0.4"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
Expand Down
27 changes: 24 additions & 3 deletions src/Hyperscript.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@

module Hyperscript

using Base64: stringmime

export @tags, @tags_noescape, m, css, Style, styles, render, Pretty, savehtml, savesvg

# Units
Expand Down Expand Up @@ -237,9 +239,28 @@ 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)
showrich(io, mime, x)
return
end
end
printescaped(io, x, escapechild(ctx))
end

# Supported MIME types in descending order of importance.
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 [
Expand Down