Broken off from bug 14868.
Created attachment 16642 [details] Patch
Created attachment 16643 [details] Patch2 Added some layout tests to the patch.
Comment on attachment 16643 [details] Patch2 r=me Don't forget to add a ChangeLog. One other comment: you use i++ as the increment in some for loops, I would mildly suggest changing it to ++i. Preincrement vs. postincrement doesn't matter for integers, but it is more efficient for iterators and it is nicer to consistently use that style instead of only when needed.
Added ChangeLog. Changed some i++ to ++i. Committed revision 26682.