strip whitespace function preserves cursor position