Skip to content

Commit 9d5cf82

Browse files
committed
Add serialization example
1 parent bfcc8d3 commit 9d5cf82

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

_posts/2020-02-03-the-background-webcompat-debacle.markdown

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ Serialization is the process of converting from a data structure (e.g. Rust stru
100100

101101
Whenever you ask the browser for state on styles, such as through the [HTMLElement.style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) or [Window.getComputedStyle](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) APIs, the value it returns to you is the result of the serialization of its data structures to a string.
102102

103+
It's important to note that how you input styles for an element may not be how they are later serialized back to you. In fact, this point is the crux of the issue we're exploring — the serialization of the `background` shorthand property differed from author's expectations based on previous browser behavior and the spec.
104+
105+
For example, given this specified style:
106+
107+
{% highlight css %}
108+
#target {
109+
background: url("https://bit.ly/") 1px 2px / 3px 4px
110+
space round local padding-box content-box,
111+
rgb(5, 6, 7) url("https://bit.ly/") 1px 2px / 3px 4px
112+
space round local padding-box content-box;
113+
}
114+
{% endhighlight%}
115+
116+
You might get back this (or something completely different, depending on your browser) post-serialization:
117+
118+
{% highlight javascript %}
119+
const target = document.getElementById('target')
120+
console.log(target.style['background'])
121+
122+
// Logs the following.
123+
// Note the difference in order vs. what was specified above.
124+
125+
// url("https://bit.ly/") space round local
126+
// 1px 2px / 3px 4px padding-box content-box,
127+
// rgb(5, 6, 7) url("https://bit.ly/") space round local
128+
// 1px 2px / 3px 4px padding-box content-box
129+
{% endhighlight %}
130+
131+
The order in which properties are serialized could be very important. For example, perhaps you are writing a JavaScript framework that parses the serialized styles of HTML elements in order to compute some new ones. Different serializations in different browsers makes this much, much more difficult to do.
132+
103133
### So why was this change made?


104134

105135
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):
@@ -125,7 +155,12 @@ Feel free to read the full conversation yourself — for the sake of brevity, he
125155
</li>
126156
<li>
127157
<span class="non-bold">
128-
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=743392">Authors complained</a> about this change in serialization, as tutorials had codified the CSS 2.1 serialization order.
158+
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=743392">Authors complained</a> about this change in serialization, as tutorials had codified the CSS 2.1 serialization order (<code>&lt;background-color&gt;</code> first).
159+
</span>
160+
</li>
161+
<li>
162+
<span class="non-bold">
163+
It is discovered Chrome's <code>background</code> shorthand serialization is "100% broken". While not made as explicit in the conversation, WebKit (Safari's browser engine) and Gecko (Firefox's browser engine) likely had, and may still have problems relative to the spec — more on that later.
129164
</span>
130165
</li>
131166
</ol>

0 commit comments

Comments
 (0)