Skip to content

Commit ae67594

Browse files
authored
Merge pull request #14 from twilco/background-serialization-webcompat-post
Rough draft of background shorthand webcompat post
2 parents cdf77d4 + a227b38 commit ae67594

File tree

7 files changed

+166
-0
lines changed

7 files changed

+166
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
---
2+
layout: post
3+
title: "The `background` debacle — a case study on web compatibility"
4+
date: 2020-02-03 12:42:53
5+
categories: posts
6+
description: A case study on web compatibility featuring the serialization of the `background` CSS shorthand property. Learn about the CSS standards process and web platform tests (WPTs) along the way.
7+
---
8+
9+
{: .no_toc}
10+
#### Table of contents
11+
1. TOC
12+
{:toc}
13+
14+
### Introduction
15+
16+
When browsing the [CSS Backgrounds 3 spec](https://www.w3.org/TR/2017/CR-css-backgrounds-3-20171017) a few days ago trying to determine the browser-default `background-color` of the viewport for use in [Kosmonaut](https://github.com/twilco/kosmonaut) (hint: it's system and browser dependent), I stumbled across this note in the changelog:
17+
18+
{:refdef: style="text-align: center;"}
19+
<a href="/assets/img/background_serialization/bg_serialization_change.png">![Image saying: (Header) Changes since the 9 September 2014 Candidate Recommendation (Bullet list item) Moved background-color (hyperlink) component of final-bg-layer (hyperlink) to the front for serialization because authors seem to expect this even though it doesn't make sense?](/assets/img/background_serialization/bg_serialization_change.png)</a>
20+
{:refdef}
21+
22+
Clicking into the `final-bg-layer` hyperlink, we discover this revision changed the serialization order of the `background` shorthand property:
23+
24+
{:refdef: style="text-align: center;"}
25+
<a href="/assets/img/background_serialization/bg_serialization_change.png">![Image showing the spec definition of the background shorthand property. Highlighted in red is the value for the property: bg-layer#, final-bg-layer. Further down in the image, the definition of final-bg-layer is also highlighted in red: background-color || bg-image || bg-position](/assets/img/background_serialization/background_shorthand_definition.png)</a>
26+
{:refdef}
27+
<div style="margin-top: -20px; margin-bottom: 10px; text-align: center; font-style: italic; font-size: .85rem">The <span style="font-style: normal"><code>#</code></span> next to <span style="font-style: italic;"><code>bg-layer</code></span> means "0 or more" non-final background layers</div>
28+
29+
This intrigued me. Why do authors expect the color to be first? Why does it make less sense for the color to be first? How was the decision made to make this change to the specification?
30+
31+
Read on for a glimpse into [how the CSS sausage is made](https://en.wiktionary.org/wiki/how_the_sausage_gets_made), from CSS specification to (often differing) browser implementation.
32+
33+
### First, a detour: how CSS comes to be
34+
35+
Having an understanding of how the CSS standards process works will help in understanding the `background` shorthand awkwardness, so let's go over it.
36+
37+
In summary: CSS syntax is standardized by the [CSSWG (CSS working group)](https://www.w3.org/Style/CSS/members), which is comprised of representatives from browser vendors, universities, various companies, and some independent experts. New CSS syntax passes through six stages:
38+
39+
<ol class="bold slightly-spaced-list">
40+
<li>
41+
Editor's draft (ED)
42+
<br />
43+
<span class="non-bold">
44+
New syntax goes from idea to "paper" in this phase, during which it is fleshed out internally by the CSSWG. You can find WIP editor drafts here: <a href="https://drafts.csswg.org/">https://drafts.csswg.org/</a>
45+
</span>
46+
</li>
47+
<li>
48+
Working draft (WD)
49+
<br />
50+
<span class="non-bold">
51+
If an ED is accepted internally by the CSSWG, it moves onto this phase for review by the community (technical organizations, W3C members, the public). Changes to the specification continue to be made during this phase. You can find the current working drafts (and more) here: <a href="https://www.w3.org/Style/CSS/current-work.en.html">https://www.w3.org/Style/CSS/current-work.en.html</a>
52+
</span>
53+
</li>
54+
<li>
55+
Last call working draft (LCWD)
56+
<br />
57+
<span class="non-bold">
58+
The LCWD provides a hard deadline for any final changes to a WD before it moves on to the CR phase.
59+
</span>
60+
</li>
61+
<li>
62+
Candidate recommendation (CR)
63+
<br />
64+
<span class="non-bold">
65+
Browsers typically implement the specification in this phase in order to gain <a href="https://www.w3.org/2018/Process-20180201/#implementation-experience">implementation experience</a>, thus determining the worthiness of this specification for the final phases. We see this phase mentioned in the above screenshot, meaning the change to <code>background</code> serialization we will dive into shortly was an revision to the original CR.
66+
</span>
67+
</li>
68+
<li>
69+
Proposed recommendation (PR)
70+
<br />
71+
<span class="non-bold">
72+
The <a href="https://www.w3.org/2005/10/Process-20051014/organization#AC">W3C Advisory Committee</a> decides if the specification should move to the final phase.
73+
</span>
74+
</li>
75+
<li>
76+
Recommendation (REC)
77+
<br />
78+
<span class="non-bold">
79+
The specification is considered complete and ready to implement. Only small maintenance work happens at this phase. In reality, most browsers implement specifications at the CR phase, so specifications that make here are often "dead", laden with errors discovered in implementation that are too difficult to fix in these later phases.
80+
</span>
81+
</li>
82+
</ol>
83+
84+
The above is largely a summary of [this article](https://css-tricks.com/css-standards-process/), so head there if you're looking for more detail.
85+
86+
### Back to business
87+
88+
Let's get back to our original goal: digging into [this revision](https://www.w3.org/TR/2017/CR-css-backgrounds-3-20171017/#changes-2014-09) in the CSS Backgrounds 3 specification:
89+
90+
> Moved <‘background-color’> component of <final-bg-layer> to the front for serialization because some authors seem to expect this even though it makes less sense?
91+
92+
In the context of CSS specifications, "authors" are the authors of webpages — web developers. You may also see the terminology "implementor" — these are the people implementing the specification, so browser (also known as _user agent_) developers.
93+
94+
Serialization is the process of converting an author-given value into something with semantic value to a user agent[^1]. That's why the order is important — if browsers expect the first value in a list of values to be a <code>background-color</code> but instead get something else, such as a <code>bg-image</code>, the value for the property won't get serialized as the author might expect (if at all).
95+
96+
### Why was this change made?


97+
98+
Development of these specifications [happens on Github](https://github.com/w3c/csswg-drafts), so let's look at [the commit that introduced this revision](https://github.com/w3c/csswg-drafts/commit/02fe11230e02279b495e4c5931be6ed5bab61c5c):
99+
100+
{:refdef: style="text-align: center;"}
101+
<a href="/assets/img/background_serialization/background_serialization_commit.png">![Image of commit on GitHub making the revision to the background serialization spec. Commit message is: Move bg-color to beginning of final layer per (hyperlink to meeting notes) even though it is silly (as leaverou mentions in the minutes)](/assets/img/background_serialization/background_serialization_commit.png)</a>
102+
{:refdef}
103+
104+
Clicking on the [pictured hyperlink](https://lists.w3.org/Archives/Public/www-style/2015Jan/0406.html) takes us to the meeting notes — search for "background serialization" to find the bit we're interested in.
105+
106+
Feel free to read the full conversation yourself — for the sake of brevity, here is a summary of points:
107+
108+
<ol class="bold slightly-spaced-list">
109+
<li>
110+
<span class="non-bold">
111+
The <a href="https://www.w3.org/TR/CSS21/colors.html#background-properties">CSS 2.1 background spec</a> placed the <code>background-color</code> first in the value list.
112+
</span>
113+
</li>
114+
<li>
115+
<span class="non-bold">
116+
For CSS3, the <code>background-color</code> was moved to the end of the final background layer because it is painted underneath all the other components of this layer (e.g. the background image and all its modifiers).
117+
</span>
118+
</li>
119+
<li>
120+
<span class="non-bold">
121+
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=743392">Authors complained</a> about this change in serialization, as tutorials had codified CSS 2.1 serialization order.
122+
</span>
123+
</li>
124+
</ol>
125+
126+
### But wait, there's more!
127+
128+
This 2015 spec revision was not the end of the confusion surrounding `background` serialization — the topic [came back up again](https://github.com/w3c/csswg-drafts/issues/418) in 2016:
129+
130+
{:refdef: style="text-align: center;"}
131+
<a href="/assets/img/background_serialization/cssom_define_ser.png">![GitHub issue with the title: cssom define serialization for background shorthand](/assets/img/background_serialization/cssom_define_ser.png)</a>
132+
{:refdef}
133+
134+
{:refdef: style="text-align: center;"}
135+
<a href="/assets/img/background_serialization/serialization_questions.png">![Part of the text of the GitHub issue described above. Text is: Firefox seems to only serialize when all set longhands are of the same length(and when it does, it serializes to all default values). Chrome always serializes, and just prints out whatever was set. Paragraph break. This should probably be specced. Important questions to answer. Bullet list item: Should we truncate everything to the length of the background-image longhand? Bullet list item: For underspecified longhands, should we cycle through them to make them the same length as background-image? Bullet list item: Should we be explicit (like Firefox) or implicit (like Chrome)? Bullet list item: Is serialization (of a shorthand) allowed to fail if parsing hasn't?](/assets/img/background_serialization/serialization_questions.png)</a>
136+
{:refdef}
137+
138+
And roughly two years after this, [it was noted](https://github.com/w3c/csswg-drafts/issues/418#issuecomment-380951618) that this inconsistency across browsers still existed.
139+
140+
### Web platform tests: the ultimate equalizer
141+
142+
There is a largely happy ending to a story, and it comes thanks to something called a _web platform test_ (WPT). Quoting the [WPT project readme](https://github.com/web-platform-tests/wpt#the-web-platform-tests-project):
143+
144+
> The web-platform-tests project is a cross-browser test suite for the Web-platform stack. Writing tests in a way that allows them to be run in all browsers gives browser projects confidence that they are shipping software that is compatible with other implementations, and that later implementations will be compatible with their implementations.
145+
146+
Most browsers automatically sync these tests to their own repositories, and use them to help prevent any set of changes from accidentally regressing web-compatibility.
147+
148+
As you might now be guessing, [a WPT was created](https://github.com/web-platform-tests/wpt/pull/10462/files) to account for all the various ways the `background` shorthand property may be ordered in the wild. With this test in place, browsers can more easily align on the acceptance of various different `background` serializations, or at the very least make a lack of compatibility obvious in test failure.
149+
150+
### Summary
151+
152+
In summary, web compatibility is hard. Thank your local browser developer when you next get a chance :)
153+
154+
If you have any questions, comments, or corrections, feel free to [open up an issue](https://github.com/twilco/twilco.github.io/issues) or leave a comment below via [utterances](https://github.com/utterance/utterances).
155+
156+
<br />
157+
158+
---
159+
160+
<br />
161+
162+
[^1]: Reading various other specifications, serialization also seems to refer to the opposite process of converting from a semantic component, such as a <code>color</code> or <code>shape</code>, to a string. See <a href="https://drafts.csswg.org/cssom/#serializing-css-values">here</a> for a comprehensive list of CSS "serializations" and <a href="https://drafts.csswg.org/cssom/#serializing-css-values-examples">here</a> for some specific serialization examples. Perhaps my understanding of serialization is flawed.

_sass/_base.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ ol.bold > li > span.non-bold {
4343
font-weight: 300;
4444
}
4545

46+
.slightly-spaced-list li {
47+
margin-bottom: 10px;
48+
}
49+
4650
/**
4751
* Begin factory-default Jekyll stuff
4852
*/
115 KB
Loading
214 KB
Loading
131 KB
Loading
59.3 KB
Loading
126 KB
Loading

0 commit comments

Comments
 (0)