Skip to content

Commit

Permalink
Render MathML and SVG as XML automatically
Browse files Browse the repository at this point in the history
Without having to specially print using the XML conversions. This is the
expected behaviour as browsers switch to 'foreign content mode' when
parsing inside `<math>` and `<svg>` tags.
  • Loading branch information
yawaramin committed Aug 26, 2024
1 parent 733adbb commit 4de8dc9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dream-html.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "v3.6.1"
version: "v3.6.2"
synopsis: "HTML generator eDSL for Dream"
description:
"Write HTML directly in your OCaml source files with editor support."
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(name dream-html)

(version v3.6.1)
(version v3.6.2)

(generate_opam_files true)

Expand Down
2 changes: 1 addition & 1 deletion pure-html.opam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
version: "v3.6.1"
version: "v3.6.2"
synopsis: "HTML generator eDSL"
description:
"Write HTML directly in your OCaml source files with editor support."
Expand Down
4 changes: 3 additions & 1 deletion pure-html/pure_html.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type 'a text_tag = attr list -> ('a, unit, string, node) format4 -> 'a

let write_attr ~xml p = function
| "", _ -> ()
| name, "" when not xml->
| name, "" when not xml ->
p "\n";
p name
| name, value ->
Expand All @@ -56,11 +56,13 @@ let rec write_tag ~xml p = function
List.iter (write_attr ~xml p) attrs;
p " />"
| Tag { name; attrs; children = None } ->
let xml = name = "math" || name = "svg" || xml in
p "<";
p name;
List.iter (write_attr ~xml p) attrs;
p (if xml then " />" else ">")
| Tag { name; attrs; children = Some children } ->
let xml = name = "math" || name = "svg" || xml in
if name = "html" then p "<!DOCTYPE html>\n";
p "<";
p name;
Expand Down
3 changes: 2 additions & 1 deletion test/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
data-hx-target="closest article > p">Test para 1.</p><p>Test para 2.</p><a
href="/a?b=cd:efg/hij">cd:efg/hij</a><a
href="/foo?a=1&amp;b=2%203&amp;c=4%3C5&amp;d=6%3E5">Test</a><a
href="/%F0%9F%98%89">wink</a></article><input
href="/%F0%9F%98%89">wink</a><math
style=""></math></article><input
type="text"
autocomplete="name"
onblur="if (1 > 0) alert(this.value)"><!-- oops --&gt;&lt;script&gt;alert('lol')&lt;/script&gt; --><dialog
Expand Down
6 changes: 4 additions & 2 deletions test/pure_html_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ let html_node =
p [] [txt "Test para 2."];
a [href "/a?b=cd:efg/hij"] [txt "cd:efg/hij"];
a [href "/foo?a=1&b=2 3&c=4<5&d=6>5"] [txt "Test"];
a [href "/😉"] [txt "wink"] ];
a [href "/😉"] [txt "wink"];
MathML.math [style_ ""] [] ];
input
[ type_ "text";
autocomplete `name;
Expand Down Expand Up @@ -75,7 +76,8 @@ let mathml_node =
let open HTML in
let open MathML in
p []
[ math [style_ ""]
[ math
[style_ ""]
[ mtable []
[ mtr []
[ mtd []
Expand Down

0 comments on commit 4de8dc9

Please sign in to comment.