From 1455b2c5160fd1de07c952bd9f4133a82e41cc3e Mon Sep 17 00:00:00 2001 From: Will Cosgrove Date: Fri, 8 Dec 2023 16:34:23 -0600 Subject: [PATCH] Use `yield_content` in `whitespace` This will allow the block form of `whitespace` to work the same way you would work with an element method. Your block can return a string, or any object that can be formatted by `format_object` if it contains no other element methods. ```rb span { "9" } whitespace { "out of" } span { "10" } ``` --- lib/phlex/sgml.rb | 4 ++-- test/phlex/view/whitespace.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index f800fad8..66bcdb09 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -141,13 +141,13 @@ def plain(content) # Output a whitespace character. This is useful for getting inline elements to wrap. If you pass a block, a whitespace will be output before and after yielding the block. # @return [nil] # @yield If a block is given, it yields the block with no arguments. - def whitespace + def whitespace(&block) target = @_context.target target << " " if block_given? - yield + yield_content(&block) target << " " end diff --git a/test/phlex/view/whitespace.rb b/test/phlex/view/whitespace.rb index a6cfe525..c5e0f865 100644 --- a/test/phlex/view/whitespace.rb +++ b/test/phlex/view/whitespace.rb @@ -30,4 +30,18 @@ def template expect(output).to be == " Home " end end + + with "whitespace around a string" do + view do + def template + span { "9" } + whitespace { "out of" } + span { "10" } + end + end + + it "produces the correct output" do + expect(output).to be == "9 out of 10" + end + end end