when cursor position or selection is out of content bounds, fix it to nearest valid position
See Implementation
when cursor position or selection is out of content bounds, fix it to nearest valid position