-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser-whitespace-study.html
506 lines (444 loc) · 15.2 KB
/
browser-whitespace-study.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Study of how browsers collapse whitespace.</title>
<style>
body {
max-width: 35em;
}
code {
background-color: lightgray;
}
.example {
background-color: lightsteelblue;
margin: 10px 10px;
}
table {
border: 1px solid #000;
border-collapse: collapse;
}
table td {
text-align: center;
padding: 3px 6px;
}
.code-block {
text-align: left;
white-space: pre;
font-family: monospace;
}
.big-left {
font-size: 25px;
text-align: left;
}
.alternating {
display: inline-block;
}
.alternating b {
color: white;
}
.alternating b:nth-child(odd) {
background: blue;
}
.alternating b:nth-child(even) {
background: red;
}
.as-inline-block b {
display: inline-block;
}
.as-quote b::before {
content: open-quote;
}
.as-quote b::after {
content: close-quote;
}
.as-block b {
display: block;
}
q {
display: inline;
}
q::before {
content: open-quote;
}
q::after {
content: close-quote;
}
q.alt {
font-style: italic;
}
q.alt::before {
content: "";
}
q.alt::after {
content: "";
}
.x {
font-size: 30px;
color: white;
text-align: center;
background: blue;
}
.y {
font-size: 30px;
color: white;
text-align: center;
background: red;
}
.block {
display: inline-block;
}
.wrap::before {
content: open-quote;
}
.wrap::after {
content: close-quote;
}
</style>
</head>
<body>
<h1>Study of how browsers collapse whitespace.</h1>
<p>This HTML file serves as a way to demonstrate how browsers collapse whitespace,
in particular how whitespace is collapsed if it exists on both sides of a tag.</p>
<p>Compare this file's source corresponding to the examples below to how they are
rendered in your browser. You can see that when the <code><q></code>
tag corresponds to a visual element, a quotation mark character, then any
whitespace on either side is separately collapsed to a single space. But if
<code><q></code>'s CSS is changed to simply alter the font-weight,
white space on both sides is treated as a single run and collapsed to a single
space before the tag.</p>
<h2>How whitespace is handled differently for two different inline elements,
<code><i></code> and <code><q></code></h2>
<p>Let's compare how whitespace is recognized when it occurs only before,
only after, and both before and after inline tags.</p>
<p>Here's the HTML source using <code><i></code>:</p>
<div class="example">
<pre><code>I said, <i>What I said.</i><br>
I said,<i> What I said.</i><br>
I said, <i> What I said.</i><br>
</code></pre>
</div>
<p>Here's the same source, but using <code><q></code> instead:</p>
<div class="example">
<pre><code>I said, <q>What I said.</q><br>
I said,<q> What I said.</q><br>
I said, <q> What I said.</q><br>
</code></pre>
</div>
<p>Now with <code><code></code>:</p>
<div class="example">
<pre><code>I said, <code>What I said.</code><br>
I said,<code> What I said.</code><br>
I said, <code> What I said.</code><br>
</code></pre>
</div>
<p>Here's how they render. First <code><i></code>:</p>
<div class="example">
I said, <i>What I said.</i><br>
I said,<i> What I said.</i><br>
I said, <i> What I said.</i><br>
</div>
<p>Now <code><q></code></p>
<div class="example">
I said, <q>What I said.</q><br>
I said,<q> What I said.</q><br>
I said, <q> What I said.</q><br>
</div>
<p>Now <code><code></code></p>
<div class="example">
I said, <code>What I said.</code><br>
I said,<code> What I said.</code><br>
I said, <code> What I said.</code><br>
</div>
<p>At first glance it would appear there are different whitespace rules
for <code><i></code> and <code><q></code>. But in fact the
rules are the same:</p>
<ol>
<li>If a tag itself (as opposed to the text it may wrap) has some sort of
inline visible manifestation in the rendering (e.g. the <code>"</code> of a
<code><q></code> tag, that manifestation is treated just like any other
visible piece of text on the line. Spaces serve as word breaks and are
rendered as spaces or line breaks. It is irrelevant if the text's source
is the source HTML or is inserted by CSS styling.
</li>
<li>If the whitespace is a run of consecutive whitespace (spaces, tabs
or newlines), it is collapsed to a single space.</li>
<li>If a run of whitespace spans one or more tags that do NOT have any
corresponding textual manifestation on the line, then only the space
before the first such tag is retained. (See the third <code><i></code>
and <code><code></code> examples above.)</li>
</ol>
<h2>Comparative analysis of spaces <i>before</i>, <i>after</i> and <i>between</i>
inline tags with different rendered manifestation</h2>
This is inspired by this StackOverflow question:
<a href='https://stackoverflow.com/questions/5078239/how-do-i-remove-the-space-between-inline-inline-block-elements'>How do I <strong>remove the space between</strong> inline/inline-block
elements?</a>
<p>We will demonstrate the differences in how your browser handles <code>inline</code>,
<code>inline-block</code>, <code>block</code>, and <code>::before/after</code> wrapped,
elements. We will demonstrate all four using the <em>exact</em> same HTML using
<code><b></code> elements, but using CSS to change the display property to each of
these four types.</p>
<p>The symbols in the result tables have the following meanings:<p>
<ul>
<li><code>✔</code> there was whitespace in that position and it was maintained︎</li>
<li><code>✗</code> there was whitespace in that position and it removed</li>
<li><code>🔲</code> the elements are separated by a block boundary;
any whitespace text in that position is irrelevant.</li>
</ul>
<h3>inline whitespace behavior</h3>
<p>The <code><b></code> element with its default <code>inline</code>
display:</p>
<table>
<colgroup>
<col>
<col span="3">
<col>
</colgroup>
<thead>
<tr>
<td>source</td><td>result</td><td colspan="3">space</td>
</tr>
<tr>
<td></td><td></td><td>before</td><td>between</td><td>after</td>
</tr>
</thead>
<tbody>
<tr>
<td class="code-block"><b>X </b> <b> X</b></td>
<td class="big-left">
<span class="alternating">
<b>X </b> <b> X</b>
</span>
</td>
<td>✔︎</td><td>✗︎</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b> <b> X</b></td>
<td class="big-left">
<span class="alternating">
<b>X</b> <b> X</b>
</span>
</td>
<td></td><td>✔︎</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b> X</b></td>
<td class="big-left">
<span class="alternating">
<b>X</b><b> X</b>
</span>
</td>
<td></td><td></td><td>✔︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b>X</b></td>
<td class="big-left">
<span class="alternating">
<b>X</b><b>X</b>
</span>
</td>
<td></td><td></td><td></td>
</tr>
</tbody>
</table>
<p>Notice that when whitespace straddles a <code><b></code> tag,<p>
<ul>
<li>it is considered a single run of contiguous whitespace,
because that is how it will look to the human eye when rendered.</li>
<li>it collapses to a single space, which will be on the left side of
the left-most <code><b></code> tag that is straddled.</li>
</ul>
<h3>inline-block whitespace behavior</h3>
<p>The exact same HTML source as above, but now with CSS that converts
<code><b></code> elements to <code>inline-block</code> display:</p>
<table>
<colgroup>
<col>
<col span="3">
<col>
</colgroup>
<thead>
<tr>
<td>source</td><td>result</td><td colspan="3">space</td>
</tr>
<tr>
<td></td><td></td><td>before</td><td>between</td><td>after</td>
</tr>
</thead>
<tbody>
<tr>
<td class="code-block"><b>X </b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-inline-block">
<b>X </b> <b> X</b>
</span>
</td>
<td>✗︎</td><td>✔︎</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-inline-block">
<b>X</b> <b> X</b>
</span>
</td>
<td></td><td>✔︎</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b> X</b></td>
<td class="big-left">
<span class="alternating as-inline-block">
<b>X</b><b> X</b>
</span>
</td>
<td></td><td></td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b>X</b></td>
<td class="big-left">
<span class="alternating as-inline-block">
<b>X</b><b>X</b>
</span>
</td>
<td></td><td></td><td></td>
</tr>
</tbody>
</table>
<p>Notice that<p>
<ul>
<li>the leading and trailing spaces withing the element are removed,
just as they are for regular blocks.</li>
<li>a space or lack thereof between the elements is maintained <i>irrespective of whitespace on the other side of the tag.</i></li>
</ul>
<h3>wrapped inline whitespace behavior</h3>
<p>The exact same HTML source as above, but now with CSS that wraps each
<code><b></code> element with quotes
using <code>::before</code> and <code>::after</code> pseudo-properties,
so that it renders just like a <code><q></code> element:<p>
<table>
<colgroup>
<col>
<col span="3">
<col>
</colgroup>
<thead>
<tr>
<td>source</td><td>result</td><td colspan="3">space</td>
</tr>
<tr>
<td></td><td></td><td>before</td><td>between</td><td>after</td>
</tr>
</thead>
<tbody>
<tr>
<td class="code-block"><b>X </b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-quote">
<b>X </b> <b> X</b>
</span>
</td>
<td>✔︎</td><td>✔︎</td><td>✔︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-quote">
<b>X</b> <b> X</b>
</span>
</td>
<td></td><td>✔︎</td><td>✔︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b> X</b></td>
<td class="big-left">
<span class="alternating as-quote">
<b>X</b><b> X</b>
</span>
</td>
<td></td><td></td><td>✔︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b>X</b></td>
<td class="big-left">
<span class="alternating as-quote">
<b>X</b><b>X</b>
</span>
</td>
<td></td><td></td><td></td>
</tr>
</tbody>
</table>
<p>In essence, each <code><b></code> and <code></b></code> tag
is turned into a <code>"</code>, making a space or the lack thereof on either
side significant, just as it would be on either side of a word or
any other text.</p>
<h3>block whitespace behavior</h3>
<p>The exact same HTML source as above, but now with CSS that converts
<code><b></code> elements to <code>block</code> display:</p>
<table>
<colgroup>
<col>
<col span="3">
<col>
</colgroup>
<thead>
<tr>
<td>source</td><td>result</td><td colspan="3">space</td>
</tr>
<tr>
<td></td><td></td><td>before</td><td>between</td><td>after</td>
</tr>
</thead>
<tbody>
<tr>
<td class="code-block"><b>X </b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-block">
<b>X </b> <b> X</b>
</span>
</td>
<td>✗︎</td><td>🔲</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b> <b> X</b></td>
<td class="big-left">
<span class="alternating as-block">
<b>X</b> <b> X</b>
</span>
</td>
<td></td><td>🔲</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b> X</b></td>
<td class="big-left">
<span class="alternating as-block">
<b>X</b><b> X</b>
</span>
</td>
<td></td><td>🔲</td><td>✗︎</td>
</tr>
<tr>
<td class="code-block"><b>X</b><b>X</b></td>
<td class="big-left">
<span class="alternating as-block">
<b>X</b><b>X</b>
</span>
</td>
<td></td><td>🔲</td><td></td>
</tr>
</tbody>
</table>
<p> </p>
<p>As you can see,<p>
<ul>
<li>any whitespace characters between the <code><b></code> elements
are insignificant as they are rendered as separate blocks.</li>
<li>leading and trailing whitespace within the blocks are removed.</li>
</ul>
<h2>Conclusion</h2>
Since we can't know ahead of time what the CSS for an element might be, the
safest thing to do is to assume the tag might manifest as a visual character,
in which case whitespace collapsing should happen independently on either side
of the tag, leaving it to the browser to make the final determination at render
time. (Note, this appears to be what Webkit does internally. Use the developer
tools to examine the DOM for these examples.)