Php tidy and text areas
Asked Answered
M

4

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?

Manysided answered 4/10, 2011 at 4:44 Comment(0)
S
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

Streamy answered 4/10, 2011 at 4:51 Comment(3)
Unfortunately sdfds is not generated by php. The HTML is generated as a string by twig.Manysided
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.Streamy
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().Manysided
W
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.

Wormeaten answered 5/10, 2011 at 16:9 Comment(3)
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.Manysided
@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.Wormeaten
Thanks dennis. That seems to have solved the problem, but it then introduced another one! Due to output-xml adding a closing tag, <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> is now <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>, this now fails to validate as HTML 4.01 Strict: using XHTML-style self-closing tags (such as <meta ... />) 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.Manysided
P
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);
Prase answered 13/12, 2011 at 10:19 Comment(0)
C
-1

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

Cryptozoic answered 6/11, 2012 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.