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

Support html safety for the #render_in method #222

Merged
merged 4 commits into from
Oct 14, 2022

Conversation

bradgessler
Copy link
Contributor

Fixes issue where this code:

  def show
    render inline: (Views::Layouts::Article.new.render_in(view_context){ "<p>Hi</p>".html_safe }), content_type: "text/html"
  end

Would result in a rendition like this:

193955078-810f8309-c9e2-4261-a97d-683b28d2f580

The updated method checks if the string is html_safe before deciding to call raw or text

lib/phlex/renderable.rb Outdated Show resolved Hide resolved
Co-authored-by: Joel Drapper <joel@drapper.me>
@bradgessler
Copy link
Contributor Author

Just tried your proposed changes in a Rails project and it works.

The "dumbest" thing I can think of to make this work outside of ActiveSupport is a #raw_render_in method. If that's gross then maybe I'd propose an Phlex.raw method that wraps a string in a Phlex::RawString (I don't think I got that name right) class that could then be checked by Phlex.

Putting that all together, the call would look something like this outside of Rails:

Views::Layouts::Article.new.render_in { Phlex.raw("<p>Hi</p>") }

@joeldrapper
Copy link
Collaborator

I’d be happy to have some kind of ActiveSupport feature detection that includes a module to override this method. 🤔

@joeldrapper
Copy link
Collaborator

👋 I thought of a way to add feature detection and it seems to work.

@joeldrapper joeldrapper merged commit 7132a01 into phlex-ruby:main Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants