Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: void foreign elements get self closing tags #243

Merged
merged 1 commit into from
Oct 20, 2024
Merged
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
116 changes: 21 additions & 95 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,103 +20,29 @@ html = ~w[
]a

svg = ~w[
circle
ellipse
line
path
polygon
polyline
rect
stop
use
a
altGlyph
altGlyphDef
altGlyphItem
animate
animateColor
animateMotion
animateTransform
animation
audio
canvas
clipPath
cursor
defs
desc
discard
feBlend
feColorMatrix
feComponentTransfer
feComposite
feConvolveMatrix
feDiffuseLighting
feDisplacementMap
feDistantLight
feDropShadow
feFlood
feFuncA
feFuncB
feFuncG
feFuncR
feGaussianBlur
feImage
feMerge
feMergeNode
feMorphology
feOffset
fePointLight
feSpecularLighting
feSpotLight
feTile
feTurbulence
filter
font
foreignObject
g
glyph
glyphRef
handler
hatch
hatchpath
hkern
iframe
image
linearGradient
listener
marker
mask
mesh
meshgradient
meshpatch
meshrow
metadata
mpath
pattern
prefetch
radialGradient
script
set
solidColor
solidcolor
style
svg
switch
symbol
tbreak
text
textArea
textPath
title
tref
tspan
unknown
video
view
vkern
circle ellipse line path polygon polyline rect stop use a
altGlyph altGlyphDef altGlyphItem animate animateColor animateMotion
animateTransform animation audio canvas clipPath cursor defs desc
discard feBlend feColorMatrix feComponentTransfer feComposite
feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight
feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur
feImage feMerge feMergeNode feMorphology feOffset fePointLight
feSpecularLighting feSpotLight feTile feTurbulence filter font
foreignObject g glyph glyphRef handler hatch hatchpath hkern iframe
image linearGradient listener marker mask mesh meshgradient meshpatch
meshrow metadata mpath pattern prefetch radialGradient script set
solidColor solidcolor style svg switch symbol tbreak text textArea
textPath title tref tspan unknown video view vkern
]a

locals_without_parens = Enum.map(temple ++ html ++ svg, &{&1, :*})
mathml = ~w[
math mi mn mo ms mspace mtext
merror mfrac mpadded mphantom mroot mrow msqrt mstyle
mmultiscripts mover msub msubsup msup munder munderover
mtable mtd mtr annotation semantics mprescripts
]a

locals_without_parens = Enum.map(temple ++ html ++ svg ++ mathml, &{&1, :*})

[
import_deps: [:typed_struct],
Expand Down
3 changes: 3 additions & 0 deletions lib/temple/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ defmodule Temple.Parser do
def void_elements,
do: @void_elements ++ Keyword.values(@void_svg_lookup) ++ Keyword.values(@void_mathml_lookup)

def foreign_void_elements,
do: Keyword.values(@void_svg_lookup) ++ Keyword.values(@void_mathml_lookup)

def void_elements_aliases,
do: @void_elements_aliases ++ @void_svg_aliases ++ @void_mathml_aliases

Expand Down
9 changes: 8 additions & 1 deletion lib/temple/renderer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ defmodule Temple.Renderer do
end
end

state.engine.handle_text(buffer, [], ">\n")
ending =
if ast.name in Temple.Parser.foreign_void_elements() do
"/>"
else
">"
end

state.engine.handle_text(buffer, [], "#{ending}\n")
end

def render(buffer, state, %AnonymousFunctions{} = ast) do
Expand Down
20 changes: 20 additions & 0 deletions test/temple/renderer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ defmodule Temple.RendererTest do
assert_html expected, result
end

test "foreign void elements get self closing tags" do
result =
Renderer.compile do
input type: "text"
mprescripts foo: "bar"
rect width: "256"
circle cx: "128"
end

# html
expected = """
<input type="text">
<mprescripts foo="bar"/>
<rect width="256"/>
<circle cx="128"/>
"""

assert_html expected, result
end

test "a match does not emit" do
result =
Renderer.compile do
Expand Down
Loading