I discovered this as I was writing a paranoid HTML cleanup library and wanted to prevent the attack where a user sticks a text-direction-change character into the page and reverses the whole thing. As we've all just witnessed, that can't happen in a conforming browser.
But when viewing the page as a text stream, yup, it reverses and then never really unsticks. Everything's working as designed!
(Maybe my library should still restore the page flow after all... I never thought of how it could mess up view source. As attacks go, it's weak sauce... but like I said, it's meant to be really, really paranoid.)
Normally blacklisting is bad, but we're only targeting the few text-direction-changing characters that exist.
There are other such libraries for other languages, poke around. See for instance http://htmlpurifier.org/ .
edit: I get the same in Opera 11.50, Firefox 5.0 and some Chrome version.
Is Chrome and Safari broken or being responsible? (Are there settings to change the behaviors in any of these browsers...?)
So say your logical text is this:
ltr LTR.
where the capital letters are RTL chars (whether because they're actually RTL or because of an RLO in the char stream). That is, the above represents the reading order. Visually this would look like this: ltr RTL.
assuming that this paragraph directionality is left-to-right; a reader reading this text would read the letters in the order 'l', 't', 'r', ' ', n 'L', 'T', 'R' (which if you note matches the logical order, hence the name).Now say you mouse down between the 't' the 'r' and drag right until your mouse is between the 'R' and the 'T'. Those are your selection endpoints. But the selection happens on the _logical_ text, so what's selected is the 'r', the space, the 'L', and the 'T' (and even in that order). You can see that in the example above; if you mouse down between the two 'o' chars in "elgooG" and drag right to between the two 'o' chars in "Google", then you get exactly this sort of behavior.
Hi RLO elgooG
^ select ^
I expected (everything between the logical end points selected): Hi Google
^^^^^^^ (selected)
In other words, I expected text selection to obey RLO character as well.If that's not enough pain, read http://unicode.org/reports/tr9/ (the unicode BiDi algorithm)
If you're still liking it, I know a few people who'd love to hire you :)
Moving your mouse happens over the physical text and sets the selection endpoints. Then everything that's logically (as opposed to physically) between those endpoints is highlighted. Your "I expected" diagram is showing the text physically between the endpoints, not the text logically between them...