Skip to content

Outputting an object directly on a tag is unexpectedly double escaped in v6 #1117

Description

@DMA57361

Came accross an inconsistency in how objects are output (esacaped?) after upgrading from 5.2.2 to 6.0.10.

For some unnecessary context: we use the draper gem to provide view decorators for objects and one of our apps uses these to have time objects output by default with a formatting with happens to include a non-breaking space, and after upgrading to v6 some - but not all! - were being double escaped.

I can reproduce the issue with the following setup added to a Rails 6.1 application. Given this class:

class Order
  def to_s
    "Order 1234 2022-11-29".html_safe
  end
end

If this HAML in placed into a view:

%div
  %p= Order.new
  %p
    = Order.new
  %p= Order.new.to_s
  %p
    = Order.new.to_s

The rendered HTML is:

<div>
<p>Order 1234&amp;nbsp;2022-11-29</p>
<p>
Order 1234&nbsp;2022-11-29
</p>
<p>Order 1234&nbsp;2022-11-29</p>
<p>
Order 1234&nbsp;2022-11-29
</p>
</div>

For clarity this is how it appears for the user:

Order 1234&nbsp;2022-11-29
Order 1234 2022-11-29
Order 1234 2022-11-29
Order 1234 2022-11-29

It seems that outputting an object such that to_s is being called implicitly is not considering the haml_safe value of the string and is instead re-escaping it..? But it seems to only do this when output inline with a tag? Moving the same code to a nested location seems to render as expected, as does explicitly calling to_s in either position.

In version 5.2.2 all four timestamps are rendered correctly, with the ampersand not being incorrectly double escaped.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions