Skip to content

Commit

Permalink
implement an escape hatch for emitting attributes with non-standard n…
Browse files Browse the repository at this point in the history
…ames (ocsigen#295)
  • Loading branch information
cemerick committed Mar 16, 2022
1 parent 905fd4e commit 2e8b221
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

* Add support for `dialog` element and `onclose` attribute
(#301 by Julien Sagot)

* Add an escape hatch for emitting attributes with non-standard names
in jsx or ppx code (a leading `_` character on attribute name)
(#295 Chas @cemerick Emerick)
* Add support for `type` attribute on `<script>` elements
(#293 by Ulrik @ulrikstrid Strid and Chas @cemerick Emerick)

Expand Down
2 changes: 1 addition & 1 deletion jsx/dune
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
-open Ppxlib
-w "-9"
))
)
)
25 changes: 19 additions & 6 deletions syntax/attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,28 @@ let parse loc (language, element_name) attributes =
labeled, e::regular
in

(* Check if this is a "data-foo" or "aria-foo" attribute. Parse the
attribute value, and accumulate it in the list of attributes passed
(* Check if this is a "data-foo", "aria-foo", or unsafe attribute. Parse
the attribute value, and accumulate it in the list of attributes passed
in ~a. *)
match parse_prefixed "data-" local_name,
parse_prefixed "aria-" local_name
parse_prefixed "aria-" local_name,
parse_prefixed "_" local_name
with
| Some tag, _ -> parse_prefixed_attribute tag "a_user_data"
| _, Some tag -> parse_prefixed_attribute tag "a_aria"
| None, None ->
| Some tag, _, _ -> parse_prefixed_attribute tag "a_user_data"
| _, Some tag, _ -> parse_prefixed_attribute tag "a_aria"
| _, _, Some tag ->
let value =
match value with
| Val s -> s
| Antiquot _ ->
Common.error loc
"Internal error: unsafe string attribute %s has a non-string value"
tag in
let value = Common.string loc value in
let identifier = Common.make ~loc language "Unsafe.string_attrib" in
let tag = Common.string loc tag in
labeled, [%expr [%e identifier] [%e tag] [%e value]] :: regular
| None, None, None ->
let tyxml_name =
match Common.find test_renamed Reflected.renamed_attributes with
| Some (name, _, _) -> name
Expand Down
5 changes: 5 additions & 0 deletions test/test_jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ let attribs = (
// the Xml.W.nil is here to satisfy the internal structure of what the jsx ppx produces
[div(~a=[a_user_data("foo-bar", "baz")], Xml.W.nil ())],
),
( "arbitrary (unchecked) attributes via an escape hatch",
[<div _some_attr="value"/>],
// the Xml.W.nil is here to satisfy the internal structure of what the jsx ppx produces
[div(~a=[Unsafe.string_attrib("some-attr", "value")], Xml.W.nil ())],
),
]
),
);
Expand Down
6 changes: 6 additions & 0 deletions test/test_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ let attribs = "ppx attribs", HtmlTests.make Html.[
[[%html "<div data-foo='valfoo'></div>"]],
[div
~a:[a_user_data "foo" "valfoo"] []] ;

"arbitrary (unchecked) attributes via an escape hatch",
[[%html "<div _some-attr='value'></div>"]],
[div
~a:[Unsafe.string_attrib "some-attr" "value"] []]

]

let ns_nesting = "namespace nesting" , HtmlTests.make Html.[
Expand Down

0 comments on commit 2e8b221

Please sign in to comment.