R
@tsaukpaetra It looks like any space at beginning/end of the selection cause the issue, and does indeed move the cursor back by the amount of spaces. It might be that we would get fun results if using various whitespace characters (tabs, other unicode stuff), and maybe mix it with right-to-left marks and what-not.
In any case, I would also guess the same thing as you, it moves the cursor backs by the trimmed-at-start-and-end length. Dunno why though, since the text itself is not modified... Oh, actually, that's not true: those spaces that cause the issue? They are not kept inside the <attr> tag and are instead put before/after it (and preserving the correct number of spaces). So I guess that the code strips them from the string, but uses the length of the original string to move the cursor.