I have used Visual Studio Online for a while for a project, and the way they apply rounded borders to selections in their online code viewer is very interesting:
I've tried inspecting the element and looking for some kind of custom CSS, but had no luck.
I have a feeling this requires some complex "hacks" to make it work, but it seems very interesting as I've never seen it done before.
How are they able to apply rounded borders to a selection?
Note: The normal selection is completely hidden WHILE selecting, and the rounded selection follows your cursor just like a regular selection. Not AFTER you have selected something.
Edit: I have created a fork of @Coma's answer that should work in Firefox and select while the mouse if moving using:
$(document).on('mousemove', function () {
(The borders in certain cases could still use work.)
I can assure you this has nothing to do with html, css border radius or highlighting. The proof?
(there are always possibilities. For example you could cover the selection using a white rectangle with a border-radius, but this appears to be highly inefficient and unlikely... So...)
Summary, they must be using the Canvas property and whole lot of codes to 'underlay' an interactive selection procedure. There are numerous different types of highlighting appearing in the editor, like 'same word highlighting', 'selected highlighting', 'out of focus highlighting', etc... For all these to happen efficiently, I can't find a better alternative than canvas.
Don't be mad at me for posting this. But I didn't want to see my 4 hours of research as a waste. At least I got an observation and that's that.
UPDATE :
Though I thought covering the selection using a white rectangle with a border-radius at the end, is a rather inefficient and unnecessary way. Microsoft doesn't think so.
They are using the curved edged rectangles to cover up the end of highlights to give that effect. They are using absolutely positioned, round-edged
<div>
s to give the effect of highlighting. And at the end of that<div>
, they overlay an image of a rounded rectangle.And kudos to them, they have done a great job with it.
Not perfect but it's working:
http://jsfiddle.net/coma/9p2CT/
Remove the real selection
Add some styles
Wrap every character in a node element
Get the selected nodes and highlight them, take care of their parents
Thanks to Tim Down for Rangy!
They are actually using round edged rectangles to cover the end of highlights in sentences which are smaller than the preceding or succeeding lines (just as I said in point 2). Check this out yourself:
<div>
contains all the "selection" highlights. They just put round edged, background-colored rectangles below the text using absolute, top and left!!!**<div>
holds similar background-colored<div>
s, only they are for highlighting focused word, similar words, etc...This is actually the content of the iframe. See the
#document
at the top?See the expanded view. The small space above having the code is actually the highlighted section.
This is not a very good idea for a simple website though. They really needed to parse and stuff with the words and letters, since it is supposed to be a high-end code editor, so cant blame them for spending a comparatively little time to 'round'ening the edges a little.
I fond also a solution which you might like. It basically works with spans around each word which you then can apply a border-radius. But I don't know how to archive the corner above - so it's only horizontal connected.
CSS'
::selection
only supports declaring color, background, cursor and outline (See W3C). So there's no possibility to defineborder-radius
for the selection with pure CSS.So I believe they did it like Niklas mentioned in comments:
I begun to try to create a solution myself, but I lost my motivation since it tooks too much time. Maybe someone could need my suggestions (I used jQuery):
For point 2:
For point 4 use replace()
For point 6:
Fiddle