Skip to content

Commit

Permalink
fix: void foreign elements get self closing tags (#243)
Browse files Browse the repository at this point in the history
Closes #242

fix: add mathml tags to formatter
  • Loading branch information
mhanberg authored Oct 20, 2024
1 parent 67ee3fe commit 4e8de14
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 96 deletions.
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

0 comments on commit 4e8de14

Please sign in to comment.