{"id":342,"date":"2013-10-15T11:20:38","date_gmt":"2013-10-15T09:20:38","guid":{"rendered":"http:\/\/blogs.igalia.com\/jfernandez\/?p=342"},"modified":"2013-10-15T11:20:38","modified_gmt":"2013-10-15T09:20:38","slug":"css-regions-and-selection","status":"publish","type":"post","link":"https:\/\/blogs.igalia.com\/jfernandez\/2013\/10\/15\/css-regions-and-selection\/","title":{"rendered":"CSS Regions and Selection"},"content":{"rendered":"<p>Back in early June, <a href=\"http:\/\/blogs.adobe.com\/webplatform\/2013\/06\/19\/together-for-a-better-web-adobe-and-igalia\/\" target=\"_blank\">Adobe<\/a> and <a href=\"http:\/\/www.igalia.com\/nc\/igalia-247\/news\/item\/igalia-partners-with-adobe-to-move-the-web-forward\/}\" target=\"_blank\">Igalia<\/a> announced a collaboration to work on the <em>CSS Regions<\/em> and <em>CSS Shapes<\/em> W3C standards. Our first challenge has been to improve the <em>Selection<\/em> use cases when using complex layout models, like<em> CSS Regions.<\/em><\/p>\n<p>The<a href=\"http:\/\/dev.w3.org\/csswg\/css-regions\/\" target=\"_blank\"><em> CSS Regions<\/em><\/a> model allows content to flow across multiple areas called <em>Regions<\/em>. This new approach offers web content designers a way to build richer and more complex layouts, mapping content with specific visual areas of a document. Defining different<em> Flow Threads<\/em> with multiple <em>Regions,<\/em> associating them to specific content, and applying different styles\u00a0<em><\/em>to a set of <em>Regions<\/em> is very powerful in terms of design and user experience. If you are interested, <a href=\"http:\/\/html.adobe.com\/webplatform\/layout\/regions\/\" target=\"_blank\">here<\/a> you can find some examples of what is possible with <em>CSS Regions<\/em>.<\/p>\n<p>But having this flexibility in web design requires overcoming quite a few technical challenges. The current implementation of <em>CSS Regions<\/em> in <em>WebKit<\/em> changes the way the <em>Render Tree<\/em> is created from the DOM Tree. This poses the challenge of making selection work with regions since selection is <strong>DOM based<\/strong>. Given its importance and frequent use, improving the interaction of selection and <em>CSS Regions<\/em> has been the main goal of our collaboration.<\/p>\n<p>The <a href=\"https:\/\/dvcs.w3.org\/hg\/editing\/raw-file\/tip\/editing.html\" target=\"_blank\">W3C selection specification<\/a>, which has not been updated since the last year, does not address the complexities introduced by new layout modules, like <em>CSS Regions, <a href=\"http:\/\/www.w3.org\/TR\/css3-flexbox\/\">CSS FlexBox <\/a><\/em>and<a href=\"http:\/\/www.w3.org\/TR\/css3-grid-layout\/\"><em> CSS Grid Layout<\/em><\/a> module . We found out very quickly that selection had many issues, with respect to both visual appearance and selected content. We have created a <a href=\"http:\/\/igalia.github.io\/css-regions-selection\/\" target=\"_blank\">tests suite<\/a> to evaluate the different use cases of selection with <em>CSS Regions<\/em>.<\/p>\n<table width=\"605\">\n<tbody>\n<tr>\n<td style=\"text-align: left\">\n<p><figure id=\"attachment_356\" aria-describedby=\"caption-attachment-356\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-356 \" alt=\"test2\" src=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test2-300x248.png\" width=\"300\" height=\"248\" srcset=\"https:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test2-300x248.png 300w, https:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test2.png 483w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-356\" class=\"wp-caption-text\">Selected content does not match the highlighted area.<\/figcaption><\/figure><\/td>\n<td>\n<p><figure id=\"attachment_352\" aria-describedby=\"caption-attachment-352\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-352\" alt=\"test1\" src=\"http:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test1-300x295.png\" width=\"300\" height=\"295\" srcset=\"https:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test1-300x295.png 300w, https:\/\/blogs.igalia.com\/jfernandez\/files\/2013\/09\/test1.png 508w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-352\" class=\"wp-caption-text\">Selection direction issues<\/figcaption><\/figure><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s start with a very simple description of the concept of <strong>Selection Direction<\/strong>, which consist of the following points:<\/p>\n<ul>\n<li>The <em>WebCore::VisibleSelection<\/em> class has two attributes called <strong>base<\/strong> and <strong>extent<\/strong> declared as <em>dom::Position<\/em> instances<\/li>\n<li>Such positions refer respectively to the anchor and focus nodes in the <em>DOM Tree.<\/em><\/li>\n<li>Additionally, <em>WebCore::VisibleSelection<\/em> has two <em>dom:Position<\/em> attributes, <strong>start<\/strong> and <strong>end<\/strong>, which are used later during the rendering phase<\/li>\n<\/ul>\n<p>Once the <em>base<\/em> and <em>extent<\/em> fields are set when instantiating a new <em>VisibleSelection<\/em> class, some adjustments and checks are performed to validate the selection. One of those checks is whether <em>base<\/em> position is <strong>before<\/strong> <em>extent<\/em> in the <em>DOM Tree<\/em>. Based on the result of this check, the <em>start<\/em> and <em>end<\/em> attributes will be set to either <em>base<\/em> or <em>extent<\/em> respectively.<\/p>\n<p>In the first test, the selected content includes the entire region block, even when it was not selected by the user. The cause, as we will see in later, derives from the position of the <em>source<\/em> block in the DOM Tree, which in this case is defined between the two regular blocks.<\/p>\n<p>The second example shows some selection direction related issues; in this case, what the user selected is precisely the content between the two highlighted areas. The problem here is that the <strong>base<\/strong> node of the original selection is below the <strong>extent<\/strong> node in the DOM Tree, so they are swapped in the selection logic. In addition, the <em>CSS Regions<\/em> implementation builds the <em>Render Tree<\/em> in a way that the <em>source<\/em> content defined by a <em>RenderNamedFlowThread<\/em> block is positioned below where it was defined in the DOM Tree. The consequence of this is that the <strong>start<\/strong> node is below the <strong>end<\/strong> node, so the highlighted area starts in the region block and continues from the root element (usually the body) until reaching the <em>end<\/em> node.<\/p>\n<p>Our first approach was trying to provide a better user experience during selection with <em>CSS Regions<\/em>. We thought that adding <strong><em>multi-range <\/em><\/strong>capabilities to the <em>DOM Selection API<\/em> was the best way to go and we provided a <a href=\"https:\/\/bugs.webkit.org\/show_bug.cgi?id=106817\" target=\"_blank\">patch<\/a>. However, this approach was<strong> rejected<\/strong> by some Apple reviewers because <em>multi-range<\/em> selection introduces many problems, such as those detailed in the selection specification.<\/p>\n<p>We have opened the debate again on the mailing list, though, because we think there might be some advantages to this approach, even without modifying the selection<em> API.<\/em> For instance, being able to handle independent <em>Ranges<\/em> and compose the expected selection will provide the flexibility needed to implement complex use cases. But, after some discussion with some of the <em>Adobe Web Platform<\/em> contributors, we have decided to focus on improving the selection following the current specification. While we feel this approach may lead to a non-optimal user experience for certain use cases, we expect implementing it will help us discover the problems inherent in the current selection specification. We have also been discussing these issues with some reviewers from Apple, <em>Ryosuke Niwa<\/em> and <em>David Hyatt<\/em>, and looking for alternatives to the <em>multi-range<\/em> approach.<\/p>\n<p>We have posted additional patches, one to <a href=\"https:\/\/bugs.webkit.org\/show_bug.cgi?id=119622\" target=\"_blank\">improve the selection behavior<\/a> and another to <a href=\"https:\/\/bugs.webkit.org\/show_bug.cgi?id=119137\" target=\"_blank\">revert the current limitation<\/a> of selections related to including content from different Flow Threads. We think that this approach provides better integration of <em>CSS Regions<\/em> with <em>HTML<\/em> documents. Plus, it will allow us to properly evaluate the performance issues of selection with <em>CSS Region<\/em>s.<\/p>\n<p>The other challenges we faced during this collaboration include:<\/p>\n<ul>\n<li>changing how the selection rendering traverses the <em>Render Tree<\/em> in order to deal with the special <em>RenderFlowThread<\/em> blocks<\/li>\n<li>adjusting the block gaps filling algorithm<\/li>\n<li>clearing the selection<\/li>\n<li>selection direction issues derived from the <em>Render Tree<\/em> and <em>DOM Tree<\/em> divergence<\/li>\n<\/ul>\n<p>We detected a number of other issues,\u00a0 such as how <em>LayoutPoints<\/em> are positioned in the<em> DOM Tree<\/em> when pointing to <em>Region <\/em>blocks, leading to an incorrect Selection <em>extent<\/em> node.\u00a0 We are confident that we will ultimately have a fully-compliant selection specification implementation for<em> CSS Regions<\/em>, but the improvements using this approach are limited. Even after solving all the issues we have detected so far, selection might still seem weird and buggy from an end user perspective. Thus we think that the final solution, one which will provide the user with a more consistent experience, will be to complement the selection specification to consider not only the <em>DOM Tree<\/em>, but also how the <em>Render Tree<\/em> is built by the <em>Layout Model<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in early June, Adobe and Igalia announced a collaboration to work on the CSS Regions and CSS Shapes W3C standards. Our first challenge has been to improve the Selection use cases when using complex layout models, like CSS Regions. The CSS Regions model allows content to flow across multiple areas called Regions. This new &hellip; <a href=\"https:\/\/blogs.igalia.com\/jfernandez\/2013\/10\/15\/css-regions-and-selection\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">CSS Regions and Selection<\/span><\/a><\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,11,12],"tags":[],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-css-regions","category-planet","category-webkit"],"_links":{"self":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":43,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/posts\/342\/revisions\/394"}],"wp:attachment":[{"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.igalia.com\/jfernandez\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}