How do I get html attribute order to be consistent when testing in Javascript
I have a component that creates a set of text like this in the innerHTML:
fourty two<br><br><input value="Select" type="button">
upon setting the innerHTML, the browser开发者_C百科 will sometimes parse this text, producing:
fourty two<br><br><input type="button" value="Select">
This behavior seems browser dependent, because I can get my tests to run in FFx, and then they will fail in safari, because of the order of the attributes.
Is there a way I can parse the HTML into some DOM-like form, and then print it out before the compare so that I can expect a consistent ordering of html attributes?
The attributes in HTML elements are unordered, that is: the order is irrelevant. If your tests assume a specific order then they're doing it wrong really.
I had to write my own canonicaization funciton for HTML for just this reason. See http://code.google.com/p/google-code-prettify/source/browse/trunk/src/prettify.js#556
精彩评论