Re: UA's implementation of ::selection

On May 14, 2010, at 9:36 PM, Boris Zbarsky wrote:

>> Based on my tests, I would say Safari has the following behavior
>> when it comes to ::selection :
>> 
>>> The ::selection doesn't propagate at all in the DOM Tree.
> 
> You mean that ::selection is only applied to the immediate child textnodes of the element selected by the rest of the selector it's attached to (as in Gecko)?
> 

Right.  It all depends on who is responsible for painting the selection.  In WebKit, individual elements paint their own selections, but who fills the gaps can be somewhat complicated (for typical gap filling its the nearest enclosing block formatting context, but not always).

Text paints its own selection using the style of the enclosing element, so e.g., for

<div>Hello</div>

The selection for "Hello" is determined by looking at the ::selection pseudo element for the div.  Honored properties for text selection painting include background-color, color, fill and stroke, and text-shadow.

For replaced elements, WebKit paints a wash over the element using background-color.

WebKit's selection attempts to be non-ragged on both the left and right sides.  This means gaps are filled not only to the ends of lines, but also into the horizontal margins.  Vertical margins are also filled.  This selection is also a wash over the content.

Because selection backgrounds paint behind text but above replaced elements (and above the backgrounds and/or possible overlapping content in the gap filled areas), we need the selection background color to be partially transparent.  Our heuristic is to try to compute a new background-color with alpha such that when blended against white it ends up looking the same as the original specified color.  If the selection already has alpha in it, however, we honor it as is.

The chosen colors for selection in the absence of any specified pseudo element come from the platform.  We have both active and inactive foreground and background colors (so 4 total possible different colors).  If the pseudo element specifies only a background color and not a foreground color, we'll use the platform foreground color (and vice versa).  Any specified selection colors in the pseudo element will be used in both the active and inactive states.

dave
(hyatt@apple.com)

Received on Saturday, 15 May 2010 04:05:21 UTC