Most of the code stolen from MWMathInspector and genericised. Also fix issues with fragment not wrapping the new node correctly. Bug: 58286 Bug: 58045 Change-Id: I708c4cb012becf8c493d5b65a051c0b13f7a11ac