You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

Why PHP Tidy didn’t work for me

I have to fit most of my applications within the confines of an LMS that requires everything it gets from my application to be valid XHTML. This causes an issue when displaying code users input or (in this case) 3rd party libraries are whole hog html5.

I don’t want to say you should close your tags, even in html, but you should.

So I’m using wysihtml5 for my wysiwyg needs. I wanted something smaller and cleaner than TinyMCE. What I got was something smaller. The issue created by wysihtml5 was they were doing img tags with document.createElement(‘IMG’) — which by default creates an unclosed void element. Now if you append that void element to an xhtml doctype, the browser will automatically fix it, but if you’re taking the toString of it and throwing it directly into a database, it will stay

So I decided to try and filter the entire app before it got sent to my LMS with PHP Tidy. tidy_repair_string will take a string, some config options and “fix” most validation errors.

echo tidy_repair_string($content,
array(
'output-xhtml'=>true,
'doctype'=>'omit',
'show-body-only'=>true
));

This worked almost perfectly. The only issue was wysihtml5 uses textareas. And the imgs go into the textareas. And that’s a violation of xhtml. So what does Tidy do with that violation? It just strips out the img (it also strips out brs). And there is no config option to stop that from happening. “Don’t correct this validation violation.”

So what am I stuck with?

preg_replace("/(]*)>/", "$1/>", $content);

Lame.

http://stackoverflow.com/questions/10028681/how-can-i-close-an-element-created-by-document-createelement
http://stackoverflow.com/questions/7539551/php-dom-method-createelement-need-to-be-self-closing-tag
http://stackoverflow.com/questions/7003042/javascript-createelement-no-end-tag

http://www.php.net/manual/en/tidy.repairstring.php
http://tidy.sourceforge.net/docs/quickref.html

http://stackoverflow.com/questions/15365484/prevent-php-tidy-from-converting-style-tag-data-to-cdata

Leave a comment