PHP DOMNode -> setAttribute() max-width confusion

While adding some custom code into Mahara, I came across what seems like a very odd PHP bug today, so odd that I put it down to user error, but just can’t seem to find what the user error is! Imagine the code below with some embed code from youtube or picasa as the value of $embedCode.
$embedCode = "<embed .................></embed>";
$dom = new DOMDocument();
$dom->loadHTML($
embedCode);
$allElements = $dom->getElementsByTagName('*');
$firstElement = $allElements->item(2);
$
firstElement->setAttribute("style", "max-width:100%");
echo $dom->saveHTML();

The line that plays up is the one including setAttribute(“style”, “max-width:100%”).

It works on pretty much all other possible css values, but doesn’t seem to like max-width nor min-width. I can set the style to hold color or background values, or text-align, but not max-width! What gives? Why does PHP seem to have CSS max-width?

EDIT: I have since uncovered the cause of this. My text string was being passed through a Mahara function clean_html() which strips out various things, including max-width style definitions. The solution is to clean_html() before I apply my max-width and hey presto, everything works as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *