# frozen_string_literal: true
require "nokogiri"
require_relative "../../test_helper"
require_relative "../../snapshot_utils"
require_relative "../../../lib/herb/engine"
require_relative "../../../lib/herb/engine/slots/visitor"
require_relative "../../../lib/herb/engine/slots/dynamics_compiler"
module Engine
module Slots
class MarkersTest < Minitest::Spec
include SnapshotUtils
def options
{ visitors: [Herb::Engine::Slots::Visitor.new], filename: "app/views/test.html.erb" }
end
def parse_slotted(template, locals)
Nokogiri::HTML5.fragment(evaluate_herb_source(Herb::Engine.new(template, **options).src, locals))
end
def item_marker_parents(document)
parents = []
document.traverse do |node|
parents << [node.text, node.parent.name] if node.comment? && node.text.include?("herb-item")
end
parents
end
test "anchors a child slot to its element when the slot is the whole content" do
assert_evaluated_snapshot(%(
<%= @name %>
), { "@name" => "Marco" }, options)
end
test "keeps paired comments when a child slot shares its element with text" do
assert_evaluated_snapshot("
Hi <%= @name %>!
", { "@name" => "Marco" }, options)
end
test "keeps paired comments when an element holds several slots" do
assert_evaluated_snapshot("
<%= @a %><%= @b %>
", { "@a" => "x", "@b" => "y" }, options)
end
test "keeps paired comments for a slot that can render nothing" do
assert_evaluated_snapshot("
<% if @admin %>x<% end %>
", { "@admin" => false }, options)
end
test "distinguishes an element's attribute slot from its content slot" do
assert_evaluated_snapshot(
%(
<%= @name %>
),
{ "@c" => "item", "@name" => "Marco" },
options
)
end
test "names the type on a marker for everything that is not a child" do
assert_evaluated_snapshot("
<% if @admin %>x<% end %>
", { "@admin" => true }, options)
end
test "names the type on an element anchor" do
assert_evaluated_snapshot(%(
>x
), { "@attrs" => "id=\"1\"" }, options)
end
test "delimits a child slot with paired comments" do
assert_evaluated_snapshot("
<%= @name %>
", { "@name" => "Marco" }, options)
end
test "gives sibling interpolations independent slots" do
assert_evaluated_snapshot(
"
Hi <%= @name %>, you have <%= @count %>
",
{ "@name" => "Marco", "@count" => 3 },
options
)
end
test "leaves an empty addressable position when a conditional is false" do
assert_evaluated_snapshot(
"
<% if @admin %>secret<% end %>
",
{ "@admin" => false },
options
)
end
test "delimits slots nested inside another slot" do
assert_evaluated_snapshot(
"
<% if @admin %><%= @secret %><% end %>
",
{ "@admin" => true, "@secret" => "s" },
options
)
end
test "a nested slot only exists once its parent branch renders" do
assert_evaluated_snapshot(
"
<% if @admin %><%= @secret %><% end %>
",
{ "@admin" => false, "@secret" => "s" },
options
)
end
test "anchors attribute slots on the element instead of inside the tag" do
assert_evaluated_snapshot(%(), { "@klass" => "card" }, options)
end
test "does not inject markers into raw text elements" do
assert_evaluated_snapshot(%(), { "@a" => "1" }, options)
end
test "wraps the document in a region marker carrying file and version" do
assert_evaluated_snapshot("
<%= @name %>
", { "@name" => "x" }, options)
end
test "compiles slot markers into the generated source" do
assert_compiled_snapshot("
<%= @name %>
", options)
end
test "compiles a conditional into the generated source" do
assert_compiled_snapshot("
<% if @admin %><%= @secret %><% end %>
", options)
end
test "compiles a collection into the generated source" do
assert_compiled_snapshot("
<% @items.each do |item| %>
<%= item %>
<% end %>
", options)
end
test "compiles attribute slots into the generated source" do
assert_compiled_snapshot(%(), options)
end
test "treats an if/elsif/else chain as one conditional slot" do
assert_evaluated_snapshot(
"
<% if @a %>A<% elsif @b %><%= @n %><% else %>C<% end %>
",
{ "@a" => false, "@b" => true, "@n" => "N" },
options
)
end
test "delimits a slot inside an unless/else" do
assert_evaluated_snapshot(
"
<% unless @a %><%= @n %><% else %>E<% end %>
",
{ "@a" => false, "@n" => "N" },
options
)
end
test "delimits a slot inside a when branch" do
assert_evaluated_snapshot(
"
<% case @x %><% when 1 %><%= @a %><% end %>
",
{ "@x" => 1, "@a" => "A" },
options
)
end
test "delimits a slot inside a case else branch" do
assert_evaluated_snapshot(
"
<% case @x %><% when 9 %>N<% else %><%= @a %><% end %>
",
{ "@x" => 1, "@a" => "A" },
options
)
end
test "gives a conditional nested inside an else its own slot" do
assert_evaluated_snapshot(
"
<% if @a %>A<% else %><% if @b %><%= @n %><% end %><% end %>
",
{ "@a" => false, "@b" => true, "@n" => "N" },
options
)
end
test "gives a conditional nested inside an elsif its own slot" do
assert_evaluated_snapshot(
"
<% if @a %>A<% elsif @b %><% if @c %><%= @n %><% end %><% end %>
",
{ "@a" => false, "@b" => true, "@c" => true, "@n" => "N" },
options
)
end
test "delimits a collection nested inside an else" do
assert_evaluated_snapshot(
"
<% if @a %>A<% else %><% @l.each do |i| %><%= i %><% end %><% end %>
",
{ "@a" => false, "@l" => [1] },
options
)
end
test "delimits nested collections" do
assert_evaluated_snapshot(
"<% @items.each do |item| %><% item.each do |cell| %><%= cell %><% end %><% end %>",
{ "@items" => [[1, 2]] },
options
)
end
test "delimits a collection body with several slots" do
assert_evaluated_snapshot(
"
<% @users.each do |u| %>
<%= u %> (<%= u %>)
<% end %>
",
{ "@users" => ["a", "b"] },
options
)
end
test "anchors several attribute slots on the same element" do
assert_evaluated_snapshot(
%(),
{ "@c" => "C", "@i" => "I" },
options
)
end
test "anchors an interpolated attribute value" do
assert_evaluated_snapshot(%(), { "@c" => "C" }, options)
end
test "delimits raw output" do
assert_evaluated_snapshot("
<%== @h %>
", { "@h" => "x" }, options)
end
test "does not assign a slot to an ERB comment" do
assert_evaluated_snapshot("
<%# skip %><%= @a %>
", { "@a" => "A" }, options)
end
test "preserves surrounding whitespace" do
assert_evaluated_snapshot("
\n
<%= @t %>
\n
", { "@t" => "T" }, options)
end
test "delimits a slot with no surrounding element" do
assert_evaluated_snapshot("<%= @a %>", { "@a" => "A" }, options)
end
test "emits only a region marker when there are no slots" do
assert_evaluated_snapshot("
static
", {}, options)
end
test "delimits a yield" do
assert_compiled_snapshot("<%= yield %>", options)
end
test "keeps head content free of injected elements" do
assert_evaluated_snapshot(
%(<%= @t %>),
{ "@t" => "T" },
options
)
end
test "keeps SVG subtrees free of injected elements" do
assert_evaluated_snapshot(
%(),
{ "@r" => "4" },
options
)
end
test "keeps adjacent siblings adjacent for CSS combinators" do
assert_evaluated_snapshot(
%(
<%= @x %>
y
),
{ "@x" => "" },
options
)
end
test "delimits a dynamic HTML comment from the outside" do
assert_evaluated_snapshot("", { "@c" => "X" }, options)
end
test "delimits a conditional inside an HTML comment" do
assert_evaluated_snapshot("", { "@a" => true }, options)
end
test "does not delimit a static HTML comment" do
assert_evaluated_snapshot("
<%= @a %>
", { "@a" => "A" }, options)
end
test "never emits a wrapper element" do
[
["
<%= @x %>
", { "@x" => "a" }],
["
<% if @x %>y<% end %>
", { "@x" => true }],
["
<% @x.each do |i| %>
i
<% end %>
", { "@x" => ["a"] }]
].each do |template, locals|
engine = Herb::Engine.new(template, **options)
output = evaluate_herb_source(engine.src, locals)
assert_snapshot_matches(output, template)
end
end
test "keeps markers out of a tag for ERB in element position" do
assert_evaluated_snapshot(%(
>x
), { "@a" => %(id="y") }, options)
end
test "keeps markers out of RCDATA content" do
assert_evaluated_snapshot("", { "@a" => "A" }, options)
end
test "delimits a for loop" do
assert_evaluated_snapshot(
"<% for x in @l %><%= x %><% end %>",
{ "@l" => [1] },
options
)
end
test "delimits each item of a keyed collection" do
assert_evaluated_snapshot(
%(<% @users.each do |user| %>
<%= user[:name] %>
<% end %>),
{ "@users" => [{ id: 1, name: "Marco" }, { id: 2, name: "Alice" }] },
options
)
end
test "delimits items of an unkeyed collection by position" do
assert_evaluated_snapshot(
"<% @users.each do |user| %>
<%= user[:name] %>
<% end %>",
{ "@users" => [{ id: 1, name: "Marco" }, { id: 2, name: "Joe" }] },
options
)
end
test "delimits items of a body with several roots via the herb:key directive" do
assert_evaluated_snapshot(
%(<% @users.each do |user| %><%# herb:key user[:id] %>
<%= user[:name] %>
<%= user[:email] %>
<% end %>),
{ "@users" => [{ id: 1, name: "Marco", email: "marco@example.com" }] },
options
)
end
test "delimits items of a body with no element at all" do
assert_evaluated_snapshot(
%(<% @users.each do |user| %><%# herb:key user[:id] %><%= user[:name] %><% end %>),
{ "@users" => [{ id: 1, name: "Marco" }, { id: 2, name: "Alice" }] },
options
)
end
test "names the branch of an if/elsif/else that rendered" do
assert_evaluated_snapshot(
"<% if @a %>A<% elsif @b %>B<% else %>C<% end %>",
{ "@a" => false, "@b" => true },
options
)
end
test "names the else branch that rendered" do
assert_evaluated_snapshot(
"<% if @a %>A<% elsif @b %>B<% else %>C<% end %>",
{ "@a" => false, "@b" => false },
options
)
end
test "names the case branch that rendered" do
assert_evaluated_snapshot(
"<% case @x %><% when 1 %>ONE<% else %>OTHER<% end %>",
{ "@x" => 9 },
options
)
end
test "names no branch when a conditional renders nothing" do
assert_evaluated_snapshot("<% if @a %>A<% end %>", { "@a" => false }, options)
end
test "keeps nested collection items distinguishable" do
assert_evaluated_snapshot(
%(<% @items.each do |item| %>
<% item[:cells].each do |cell| %>
<%= cell %>
<% end %>
<% end %>),
{ "@items" => [{ id: 1, cells: [11, 12] }] },
options
)
end
test "renders the same content as an unslotted template" do
template = %(
<%= @title %>
<% if @admin %>x<% end %>
)
locals = { "@title" => "T", "@admin" => true }
slotted = evaluate_herb_source(Herb::Engine.new(template, **options).src, locals)
plain = evaluate_herb_source(Herb::Engine.new(template).src, locals)
unmarked = slotted
.gsub(%r{}, "")
.gsub(/ data-herb-(child|slot)="[^"]*"/, "")
assert_equal plain, unmarked
end
test "a table collection has its item markers split across table and tbody" do
document = parse_slotted(
%(
<% @items.each do |item| %>
x
<% end %>
),
{ "@items" => [1] }
)
assert_equal [["herb-item:0:1", "table"], ["/herb-item:0", "tbody"]], item_marker_parents(document)
end
test "an explicit tbody keeps an item's markers under one parent" do
document = parse_slotted(
%(
<% @items.each do |item| %>
x
<% end %>
),
{ "@items" => [1] }
)
assert_equal [["herb-item:0:1", "tbody"], ["/herb-item:0", "tbody"]], item_marker_parents(document)
end
def rendered(template, locals)
evaluate_herb_source(Herb::Engine.new(template, **options).src, locals)
end
test "renders the same markers whichever branch of a same-shaped conditional runs" do
template = "<% if @c %>
<%= @today %>
<% else %>
<%= @tomorrow %>
<% end %>"
locals = { "@today" => "Mon", "@tomorrow" => "Tue" }
taken = rendered(template, locals.merge("@c" => true))
untaken = rendered(template, locals.merge("@c" => false))
assert_equal taken.sub("Mon", "?"), untaken.sub("Tue", "?")
assert_snapshot_matches(taken, "markers-same-branch")
end
test "collapses a same-shaped conditional to one slot" do
assert_evaluated_snapshot(
"<% if @c %>
<%= @today %>
<% else %>
<%= @tomorrow %>
<% end %>",
{ "@c" => false, "@today" => "Mon", "@tomorrow" => "Tue" },
options
)
end
test "keeps the conditional when the branches do not lay out the same" do
assert_evaluated_snapshot(
"<% if @c %>
<%= @today %>
<% else %>
<%= @tomorrow %>
<% end %>",
{ "@c" => false, "@today" => "Mon", "@tomorrow" => "Tue" },
options
)
end
class CapturingView
def initialize
@output_buffer = +""
end
def grab
outer = @output_buffer
@output_buffer = +""
yield
captured = @output_buffer
@output_buffer = outer
captured
end
end
test "puts no marker around a block whose value is not output" do
template = %(<% link = ->(label) do %><% grab do %><%= label %><% end %><% end %>
<% end %>)
key = %(a&b"d")
[false, true].each do |escape|
markup = evaluate_herb_source(Herb::Engine.new(template, **options, escape: escape).src, { "@items" => [key] })
assert_snapshot_matches(markup, "markers-item-#{escape}")
end
end
test "leaves a block's value as the block's own last expression" do
template = "<% data = fetch do %><% { count: 7 } %><% end %>
<%= data[:count] %>
"
source = Herb::Engine.new(template, **options).src
refute_match(/herb-slot:\d+:block/, source)
refute_match(%r{/herb-slot:\d+-->'.freeze;\s*end}, source)
end
test "a visitor that rewrites the template never sees a marker" do
seen = []
rewriter = Class.new(Herb::Visitor) do
define_method(:seen) { seen }
def self.rewrites_erb_source? = true
def visit_html_comment_node(node)
seen << node.to_s
super
end
end.new
source = Herb::Engine.new(
"
<%= @name %>
",
visitors: [Herb::Engine::Slots::Visitor.new, rewriter],
filename: "app/views/test.html.erb"
).src
assert_empty rewriter.seen
assert_snapshot_matches(evaluate_herb_source(source, { "@name" => "Marco" }), "markers-eval-slot")
end
test "recognizes every marker it writes as a comment" do
markers = Herb::Engine::Slots::Markers.new
comments = [
markers.slot_open(0, :child),
markers.slot_open(0, :attribute),
markers.slot_close(0),
markers.branch(0, 1),
markers.seeds_open_prefix,
markers.item_open_prefix(0),
markers.item_close(0),
markers.region_open_prefix("app/views/test.html.erb", "0417e5d5"),
markers.region_close("app/views/test.html.erb")
]
recognized = comments.select { |text| Herb::Engine::Slots::Markers.marker?(text) }
assert_equal comments, recognized
end
test "recognizes nothing else" do
markers = Herb::Engine::Slots::Markers.new
others = [
"",
"",
"",
markers.statics_open("app/views/test.html.erb", "0417e5d5"),
markers.manifests_open
]
recognized = others.select { |text| Herb::Engine::Slots::Markers.marker?(text) }
assert_equal [], recognized
end
test "delimits a standalone keyed element with its key in the slot marker" do
assert_evaluated_snapshot(
%(<%# herb:slots %>\n
<%= @name %>
),
{ "@id" => 7, "@name" => "Marco" },
options
)
end
test "anchors a slot on an element opened across a conditional" do
template = %(<% if @c %>
<% else %>
<% end %><%= @value %>
)
assert_evaluated_snapshot(template, { "@c" => true, "@value" => "v" }, options)
assert_evaluated_snapshot(template, { "@c" => false, "@value" => "v" }, options)
end
test "restarts the position of a nested unkeyed collection for every outer item" do
assert_evaluated_snapshot(
"<% @rows.each do |row| %>
<% row.each do |cell| %>
<%= cell %>
<% end %>
<% end %>",
{ "@rows" => [["a"], ["b", "c"]] },
options
)
end
test "closes the item of an iteration that a next skips" do
assert_evaluated_snapshot(
"<% @users.each do |user| %><%# herb:key user[:id] %><% next if user[:id] == 1 %>
<%= user[:name] %>
<% end %>",
{ "@users" => [{ id: 1, name: "Marco" }, { id: 2, name: "Joe" }] },
options
)
end
test "closes the item of an iteration that a break leaves" do
assert_evaluated_snapshot(
"<% @users.each do |user| %><% break if user[:id] == 2 %>
<%= user[:name] %>
<% end %>",
{ "@users" => [{ id: 1, name: "Marco" }, { id: 2, name: "Joe" }] },
options
)
end
end
end
end