summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-fonts/parsing/font-computed.html
blob: ab2694d211468b62b7d82d8ec353005d058b4f24 (plain)
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS Fonts Module Level 4: getComputedStyle().font</title>
<link rel="help" href="https://drafts.csswg.org/css-fonts-4/#font-prop">
<meta name="assert" content="font computed value round-trips.">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/css/support/computed-testcommon.js"></script>
<style>
  #container {
    font-weight: 800;
    font-size: 40px;
  }
</style>
</head>
<body>
<div id="container">
  <div id="target"></div>
</div>
<script>
'use strict';

// Firefox and Edge 18 serialize these as supplied.
// Blink and Safari have implementation-dependent or platform-dependent serializations.
function test_system_font(keyword) {
  test(() => {
    const target = document.getElementById('target');
    const previousValue = 'italic xx-large/0px fantasy';
    target.style.font = previousValue;
    target.style.font = keyword;
    const readValue = getComputedStyle(target).font;
    assert_not_equals(readValue, '', 'font should be set');
    assert_not_equals(readValue, previousValue, 'font should be updated');
    target.style.font = previousValue;
    target.style.font = readValue;
    assert_equals(getComputedStyle(target).font, readValue, "serialization should round-trip");
  }, keyword + ' should be a supported system font.');
}

test_system_font('caption');
test_system_font('icon');
test_system_font('menu');
test_system_font('message-box');
test_system_font('small-caption');
test_system_font('status-bar');

// a value other than normal
const generate_style = () => 'italic';

// value other than normal
const generate_variant = () => 'small-caps';

// values other than normal
const generate_weight = (() => {
  const alternatives = [
    'bold',
    'bolder',
    'lighter',
    '100',
    '900'
  ];
  let counter = 0;
  return () => alternatives[counter++ % alternatives.length];
})();

const compute_weight = (() => {
  const cache = {}
  return (weight) => {
    if (!(weight in cache)) {
      const weight_reference = document.createElement('div');
      document.getElementById('container').appendChild(weight_reference);
      weight_reference.style.fontWeight = weight;
      cache[weight] = getComputedStyle(weight_reference).fontWeight;
      weight_reference.remove();
    }
    return cache[weight];
  }
})();

// values other than normal
const generate_stretch = (() => {
  const alternatives = [
    'ultra-condensed',
    'extra-condensed',
    'condensed',
    'semi-condensed',
    'semi-expanded',
    'expanded',
    'extra-expanded',
    'ultra-expanded'
  ];
  let counter = 0;
  return () => alternatives[counter++ % alternatives.length];
})();

const generate_size = (() => {
  const alternatives = [
    // <absolute-size>
    'xx-small',
    'medium',
    'xx-large',

    // <relative-size>
    'larger',
    'smaller',

    // <length-percentage>
    '10px',
    '20%',
    'calc(30% - 40px)',
  ];
  let counter = 0;
  return () => alternatives[counter++ % alternatives.length];
})();

const generate_line_height = (() => {
  const alternatives = [
    null,
    'normal',
    '1.2',
    'calc(120% + 1.2em)'
  ];
  let counter = 0;
  return () => alternatives[counter++ % alternatives.length];
})();

const generate_family = (() => {
  const alternatives = [
    'serif',
    'sans-serif',
    'cursive',
    'fantasy',
    'monospace',
    'Menu',
    '"Non-Generic Example Family Name"'
  ];
  let counter = 0;
  return () => alternatives[counter++ % alternatives.length];
})();

function test_specific(prefix) {
  const reference = document.createElement('div');
  document.getElementById('container').appendChild(reference);

  let parts = [];
  let canonical = [];
  let style = null;
  let variant = null;
  let weight = null;
  let stretch = null;
  for (let entry of prefix) {
    if (entry === 'style') {
      style = generate_style();
      parts.push(style);
    } else if (entry === 'variant') {
      variant = generate_variant();
      parts.push(variant);
    } else if (entry === 'weight') {
      weight = generate_weight();
      parts.push(weight);
    } else if (entry === 'stretch') {
      stretch = generate_stretch();
      parts.push(stretch);
    } else {
      // normal
      parts.push('normal');
    }
  }

  if (style) {
    canonical.push(style);
    reference.style.fontStyle = style;
  }

  if (variant) {
    canonical.push(variant);
    reference.style.fontVariant = style;
  }
  if (weight) {
    canonical.push(compute_weight(weight));
    reference.style.fontWeight = style;
  }
  if (stretch) {
    canonical.push(stretch);
    reference.style.fontStretch = style;
  }

  const size = generate_size();
  reference.style.fontSize = size;
  const line_height = generate_line_height();
  if (line_height) {
    parts.push(size + '/' + line_height);
    reference.style.lineHeight = line_height;
  } else {
    parts.push(size);
  }

  const family = generate_family();
  parts.push(family);
  reference.style.fontFamily = family;

  if (!line_height || line_height === 'normal') {
    canonical.push(getComputedStyle(reference).fontSize);
  } else {
    // Implementations differ on adjacent space when serializing '/'
    // https://github.com/w3c/csswg-drafts/issues/4282
    canonical.push(getComputedStyle(reference).fontSize + ' / ' + getComputedStyle(reference).lineHeight);
  }

  canonical.push(family);

  reference.remove();

  test_computed_value('font', parts.join(' '), canonical.join(' '));
}

// Font style, variant, weight and stretch may appear in any order.
// Any or all may be omitted. Each accepts the keyword 'normal'.
// We generate every permutation of these four properties, treating
// the cases of a property value being omitted or being explicitly
// 'normal' as being distinct permutations from when the property
// has a value other than 'normal'.
function test_various(prefix) {
  test_specific(prefix);
  if (prefix.length === 4) {
    // Font style, variant, weight and stretch may not appear
    // more than once.
    return;
  }

  const alternatives = [
    'normal',
    'style',
    'variant',
    'weight',
    'stretch'
  ];
  for (let alternative of alternatives) {
    // Since this is called recursively, check prefix for existing
    // alternatives, otherwise we may have two styles or two variants, etc.
    if (alternative === 'normal' || !prefix.includes(alternative))
      test_various(prefix.concat(alternative));
  }
}

test_various([]);
</script>
</body>
</html>