The order isn’t random, it is just fine. But unlike HTML, in this system is matters where the linefeeds happen to fall.
All it took to fix that broken post was adding a line feed / CR after the closing tag.
I’m willing to keep manually editing posts when users fail to comply with the required structure, but it still seems strange that line feeds should matter to the parser…