threedaymonk / htmlbeautifier

A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

textarea needs to be treated as a preformatted element

trendster opened this issue · comments

I've noticed some odd behavior with <textarea>. Formatted indention is showing-up in field content. I believe it needs to be treated as a preformatted element.

Here is a test along with example fix based on the existing solution for <pre>.

diff --git a/lib/htmlbeautifier/html_parser.rb b/lib/htmlbeautifier/html_parser.rb
index 1d4285d..e18f843 100644
--- a/lib/htmlbeautifier/html_parser.rb
+++ b/lib/htmlbeautifier/html_parser.rb
@@ -31,6 +31,8 @@ module HtmlBeautifier
        :foreign_block],
       [%r{(<pre#{ELEMENT_CONTENT}>)(.*?)(</pre>)}omi,
        :preformatted_block],
+      [%r{(<textarea#{ELEMENT_CONTENT}>)(.*?)(</textarea>)}omi,
+       :preformatted_block],
       [%r{<#{HTML_VOID_ELEMENTS}(?: #{ELEMENT_CONTENT})?/?>}om,
        :standalone_element],
       [%r{</#{HTML_BLOCK_ELEMENTS}>}om,
diff --git a/spec/behavior_spec.rb b/spec/behavior_spec.rb
index 53c13cd..7609d6e 100644
--- a/spec/behavior_spec.rb
+++ b/spec/behavior_spec.rb
@@ -338,6 +338,21 @@ describe HtmlBeautifier do
     expect(described_class.beautify(source)).to eq(source)
   end

+  it "does not modify content of <textarea>" do
+    source = code <<-END
+      <div>
+        <textarea>   Preformatted   text
+
+                should  <em>not  be </em>
+                      modified,
+                ever!
+
+        </textarea>
+      </div>
+    END
+    expect(described_class.beautify(source)).to eq(source)
+  end
+

(I've fixed your issue's markup: if you don't put things in angle brackets inside backticks, it seems to confuse the parser.)

You're right. This behaviour is incorrect, and your solution looks right at first glance. As it's past 11pm here, I'll take a closer look in the next few days.

Applied and released in 1.1.1. Thanks!