Skip to content

Commit

Permalink
Allow elements to output safe return values (#773)
Browse files Browse the repository at this point in the history
### Before

```ruby
script { safe(%(console.log("Hi 👋"))) } # => <script></script>
```

### After
```ruby
script { safe(%(console.log("Hi 👋"))) } # => <script>console.log("Hi 👋")</script>
```
  • Loading branch information
willcosgrove authored Sep 6, 2024
1 parent c699783 commit 815c94e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/phlex/elements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def #{method_name}(**attributes)
buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
when Phlex::SGML::SafeObject
buffer << content.to_s
else
if (formatted_object = format_object(content))
buffer << Phlex::Escape.html_escape(formatted_object)
Expand All @@ -94,6 +96,8 @@ def #{method_name}(**attributes)
buffer << Phlex::Escape.html_escape(content.name)
when nil
nil
when Phlex::SGML::SafeObject
buffer << content.to_s
else
if (formatted_object = format_object(content))
buffer << Phlex::Escape.html_escape(formatted_object)
Expand Down
10 changes: 10 additions & 0 deletions quickdraw/sgml/safe_value.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,13 @@ def view_template
test "safe value" do
expect(Example.call) == %(<a onclick="window.history.back()" href="javascript:window.history.back()"></a>)
end

class ExampleScript < Phlex::HTML
def view_template
script { safe(%(console.log("Hello World");)) }
end
end

test "element content blocks that return safe values" do
expect(ExampleScript.call) == %(<script>console.log("Hello World");</script>)
end

0 comments on commit 815c94e

Please sign in to comment.