8

I am using tidy to clean up and format the output of HTML generated by the twig template engine.

I am using the following configuration for tidy:

$config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0);

Everything works nice and well, except when we get to textareas.

Here's the uncleaned fragment:

<textarea id="words"
         rows="10"       cols="50"                  >sdfds</textarea>

While the formatting is very messy, the correct value is outputted in the text area: 'sdfds' without any whitespace before or after.

This is the cleaned format after using tidy:

                <textarea id="words" name="words" rows="10" cols="50" title="prompt">
sdfds
</textarea>

As can be seen, the markup is much neater now, but tidy has introduced a linebreak after 'sdfds', which means that the cursor is now pointing at the line after 'sdfds' when viewed in the browser.

This is rather annoying, and I am not sure how to go about dealing with this. I would still want to have the textarea tag cleaned up, but I would prefer it to be formatted like so:

<textarea id="words" name="words" rows="10" cols="50" title="prompt">sdfds</textarea>

Has anyone dealt with this issue before? If so, How can I get tidy to not introduce those whitespaces for the textarea tag?

F21
  • 32,163
  • 26
  • 99
  • 170

4 Answers4

0

If sdfds is outputed with php you will need to add another config option.

 $config = array('indent' => TRUE, 'output-html' => TRUE, 'wrap' => 0, 'wrap-php' => 0);

Wrap only worries about html and any php statements are treated as a new line. For more information on config options you can visit: http://tidy.sourceforge.net/docs/quickref.html

James Williams
  • 4,221
  • 1
  • 19
  • 35
  • Unfortunately sdfds is not generated by php. The HTML is generated as a string by twig. – F21 Oct 04 '11 at 05:00
  • Twig adds php code to echo data from a file or a database. Add the wrap-php, if that does not work replace 0 with false. – James Williams Oct 04 '11 at 05:16
  • I have tried using 0 and False for the `wrap-php` parameter, but I am still getting the same result. It is possible that its due to the text area being pulled from another twig template file using `block()`. – F21 Oct 04 '11 at 07:40
0

When you use indent => true you will get messed up texareas with tidy. It gets to tidy because even the textareas value is indented. If you want your textareas to appear correct you could just set indent => false. This will tidy up your HTML, but will also leave your textarea with the same value after applying tidy. I have seen there are some patches which solves the problem, but then you should compile tidy yourself. You could also do this with PHP but then you are tidying up tidy.

dennis
  • 620
  • 8
  • 21
  • I am still seeing the same problem with `indent => false`. I have also tried setting `output-html` to `true` or `false`, but I am still seeing the same problem. – F21 Oct 05 '11 at 23:25
  • 1
    @phpdev This is true for html output, where tidy will add a newline before the exact value of the textarea. This can be solved by using output-xml => true instead of output-html => true. – dennis Oct 06 '11 at 09:11
  • Thanks dennis. That seems to have solved the problem, but it then introduced another one! Due to output-xml adding a closing tag, `` is now ``, this now fails to validate as HTML 4.01 Strict: `using XHTML-style self-closing tags (such as ) in HTML 4.01 or earlier. To fix, remove the extra slash ('/') character. For more information about the reasons for this, see Empty elements in SGML, HTML, XML, and XHTML.` – F21 Oct 06 '11 at 11:35
0

Tidy can be rally a "messy" sometimes and needs further refinement with rexep, here is a workaround that will put the cursor on the same line in the final output, so the usability doesn't suffer. Just run your tidied html trough:

$subject = preg_replace('%(\r\n|\n)(?=</textarea>)%sim', '', $subject);
mangefort
  • 353
  • 1
  • 2
  • 9
-1

Just use trim function for text area. And you will get what you are looking for

user1635914
  • 125
  • 10