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

JSONEncoder - render :tag_self_close html #398

Merged
merged 1 commit into from
Jan 26, 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
5 changes: 3 additions & 2 deletions lib/beacon/template/heex/json_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ defmodule Beacon.Template.HEEx.JSONEncoder do
end
end

defp transform_entry({:tag_self_close, tag, attrs}, _site, _assigns) do
defp transform_entry({:tag_self_close, tag, attrs} = node, site, assigns) do
%{
"tag" => tag,
"attrs" => transform_attrs(attrs, true),
"content" => []
"content" => [],
"rendered_html" => Beacon.Template.HEEx.render(site, HEExDecoder.decode(node), assigns)
}
end

Expand Down
41 changes: 36 additions & 5 deletions test/beacon/template/heex/json_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ defmodule Beacon.Template.HEEx.JSONEncoderTest do
)
end

test "eex expressions in attrs" do
assert_output(
~S|
<img
alt={@beacon_live_data.person.bio}
src={@beacon_live_data.person.picture}
class="w-full h-auto max-w-full"
/>
|,
[
%{
"attrs" => %{
"alt" => "{@beacon_live_data.person.bio}",
"class" => "w-full h-auto max-w-full",
"self_close" => true,
"src" => "{@beacon_live_data.person.picture}"
},
"content" => [],
"rendered_html" => "<img alt=\"person bio\" src=\"profile.jpg\" class=\"w-full h-auto max-w-full\">",
"tag" => "img"
}
],
%{beacon_live_data: %{person: %{bio: "person bio", picture: "profile.jpg"}}}
)
end

test "block expressions" do
assert_output(
~S"""
Expand Down Expand Up @@ -201,7 +227,12 @@ defmodule Beacon.Template.HEEx.JSONEncoderTest do
assert_output(
~S|<BeaconWeb.Components.image name="logo.jpg" width="200px" />|,
[
%{"attrs" => %{"name" => "logo.jpg", "self_close" => true, "width" => "200px"}, "content" => [], "tag" => "BeaconWeb.Components.image"}
%{
"attrs" => %{"name" => "logo.jpg", "self_close" => true, "width" => "200px"},
"content" => [],
"tag" => "BeaconWeb.Components.image",
"rendered_html" => "<img src=\"/beacon_assets/logo.jpg\" class=\"\" width=\"200px\">"
}
]
)

Expand All @@ -211,8 +242,8 @@ defmodule Beacon.Template.HEEx.JSONEncoderTest do
%{
"attrs" => %{"path" => "/contact", "replace" => "{true}"},
"content" => ["Book meeting"],
"rendered_html" => "<a href=\"#\" path=\"/contact\">Book meeting</a>",
"tag" => ".link"
"tag" => ".link",
"rendered_html" => "<a href=\"#\" path=\"/contact\">Book meeting</a>"
}
]
)
Expand All @@ -230,8 +261,8 @@ defmodule Beacon.Template.HEEx.JSONEncoderTest do
"attrs" => %{},
"content" => ["my_component(\"sample_component\", %{val: 1})"],
"metadata" => %{"opt" => ~c"="},
"rendered_html" => "<span id=\"my-component-1\">1</span>",
"tag" => "eex"
"tag" => "eex",
"rendered_html" => "<span id=\"my-component-1\">1</span>"
}
]
)
Expand Down
Loading