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

Fix textareas only after preserved timing #941

Merged
merged 1 commit into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/haml/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def rstrip!
# @since Haml 4.0.1
# @private
def fix_textareas!(input)
return input unless toplevel? && input.include?('<textarea'.freeze)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that toplevel? is considered to be added just for optimization, and it must be removed now to apply this in partial view (we didn't need to call in partial before because outside layout will render partial as ruby script and call fix_textareas! for it).

And this fix won't cause regression as this method is idempotent and safe to call multiple times.

return input unless input.include?('<textarea'.freeze)

pattern = /<(textarea)([^>]*)>(\n|&#x000A;)(.*?)<\/textarea>/im
input.gsub!(pattern) do |s|
Expand Down
6 changes: 3 additions & 3 deletions lib/haml/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ def build_script_formatter(text, opts)
text = "(#{text}).strip"
end
if opts[:preserve_tag]
text = "::Haml::Helpers.preserve(#{text})"
text = "_hamlout.fix_textareas!(::Haml::Helpers.preserve(#{text}))"
elsif opts[:preserve_script]
text = "::Haml::Helpers.find_and_preserve(#{text}, _hamlout.options[:preserve])"
text = "_hamlout.fix_textareas!(::Haml::Helpers.find_and_preserve(#{text}, _hamlout.options[:preserve]))"
end
"_hamlout.fix_textareas!(#{text});"
"#{text};"
end

def push_generated_script(text)
Expand Down
8 changes: 6 additions & 2 deletions lib/haml/helpers/action_view_mods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ def content_tag_with_haml(name, *args, &block)
preserve = haml_buffer.options.fetch(:preserve, %w[textarea pre code]).include?(name.to_s)

if block_given? && block_is_haml?(block) && preserve
return content_tag_without_haml(name, *args) {preserve(&block)}
return content_tag_without_haml(name, *args) do
haml_buffer.fix_textareas!(Haml::Helpers.preserve(&block)).html_safe
end
end

content = content_tag_without_haml(name, *args, &block)
content = Haml::Helpers.preserve(content) if preserve && content
if preserve && content
content = haml_buffer.fix_textareas!(Haml::Helpers.preserve(content)).html_safe
end
content
end

Expand Down