Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ml-in-barcelona/server-reason-react
Browse files Browse the repository at this point in the history
* 'main' of github.com:ml-in-barcelona/server-reason-react:
  Remove data-reactroot attr from ReactDom render_element (#129)
  • Loading branch information
davesnx committed Apr 11, 2024
2 parents bc2d6d1 + 7bf4fc9 commit 84d3b3e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
11 changes: 2 additions & 9 deletions packages/reactDom/src/ReactDOM.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ let attributes_to_string attrs =
| [] -> ""
| rest -> " " ^ (rest |> String.concat " " |> String.trim)

let react_root_attr_name = "data-reactroot"
let data_react_root_attr = Printf.sprintf " %s=\"\"" react_root_attr_name

type mode = String | Markup

let render_to_string ~mode element =
Expand All @@ -74,9 +71,6 @@ let render_to_string ~mode element =
<!-- --> between text nodes *)
let previous_was_text_node = ref false in
let rec render_element element =
let root_attribute =
match is_root.contents with true -> data_react_root_attr | false -> ""
in
match element with
| Empty -> ""
| Provider children -> render_element children
Expand All @@ -88,11 +82,10 @@ let render_to_string ~mode element =
| Lower_case_element { tag; attributes; _ }
when Html.is_self_closing_tag tag ->
is_root.contents <- false;
Printf.sprintf "<%s%s%s />" tag root_attribute
(attributes_to_string attributes)
Printf.sprintf "<%s%s />" tag (attributes_to_string attributes)
| Lower_case_element { tag; attributes; children } ->
is_root.contents <- false;
Printf.sprintf "<%s%s%s>%s</%s>" tag root_attribute
Printf.sprintf "<%s%s>%s</%s>" tag
(attributes_to_string attributes)
(children |> List.map render_element |> String.concat "")
tag
Expand Down
12 changes: 4 additions & 8 deletions packages/reactDom/test/test_renderToString.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@ let assert_string left right =

let react_root_one_element () =
let div = React.createElement "div" [] [] in
assert_string (ReactDOM.renderToString div) "<div data-reactroot=\"\"></div>"
assert_string (ReactDOM.renderToString div) "<div></div>"

let react_root_two_elements () =
let div = React.createElement "div" [] [ React.createElement "span" [] [] ] in
assert_string
(ReactDOM.renderToString div)
"<div data-reactroot=\"\"><span></span></div>"
assert_string (ReactDOM.renderToString div) "<div><span></span></div>"

let text_single_node () =
let div =
React.createElement "div" []
[ React.createElement "span" [] [ React.string "Hello" ] ]
in
assert_string
(ReactDOM.renderToString div)
"<div data-reactroot=\"\"><span>Hello</span></div>"
assert_string (ReactDOM.renderToString div) "<div><span>Hello</span></div>"

let consecutives_text_nodes () =
let div =
Expand All @@ -30,7 +26,7 @@ let consecutives_text_nodes () =
in
assert_string
(ReactDOM.renderToString div)
"<div data-reactroot=\"\"><span>Hello<!-- -->Hello</span></div>"
"<div><span>Hello<!-- -->Hello</span></div>"

let case title fn = Alcotest_lwt.test_case_sync title `Quick fn

Expand Down

0 comments on commit 84d3b3e

Please sign in to comment.