summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_font_family_parsing.html
blob: 526491ebc0fd1cb4ea5502e653fa23efc02a095d (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
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset=utf-8>
  <title>Font family name parsing tests</title>
  <link rel="author" title="John Daggett" href="mailto:jdaggett@mozilla.com">
  <link rel="help" href="http://www.w3.org/TR/css3-fonts/#font-family-prop" />
  <link rel="help" href="http://www.w3.org/TR/css3-fonts/#font-prop" />
  <meta name="assert" content="tests that valid font family names parse and invalid ones don't" />
  <script type="text/javascript" src="/resources/testharness.js"></script>
  <script type="text/javascript" src="/resources/testharnessreport.js"></script>
  <style type="text/css">
  </style>
</head>
<body>
<div id="log"></div>
<pre id="display"></pre>
<style type="text/css" id="testbox"></style>

<script type="text/javascript">

function fontProp(n, size, s1, s2) { return (s1 ? s1 + " " : "") + (s2 ? s2 + " " : "") + size + " " + n; }
function font(n, size, s1, s2) { return "font: " + fontProp(n, size, s1, s2); }
 
// testrules
//   namelist - font family list
//   invalid  - true if declarations won't parse in either font-family or font
//   fontonly - only test with the 'font' property
//   single   - namelist includes only a single name (@font-face rules only allow a single name)

var testFontFamilyLists = [

  /* basic syntax */
  { namelist: "simple", single: true },
  { namelist: "'simple'", single: true },
  { namelist: '"simple"', single: true },
  { namelist: "-simple", single: true },
  { namelist: "_simple", single: true },
  { namelist: "quite simple", single: true },
  { namelist: "quite _simple", single: true },
  { namelist: "quite -simple", single: true },
  { namelist: "0simple", invalid: true, single: true },
  { namelist: "simple!", invalid: true, single: true },
  { namelist: "simple()", invalid: true, single: true },
  { namelist: "quite@simple", invalid: true, single: true },
  { namelist: "#simple", invalid: true, single: true },
  { namelist: "quite 0simple", invalid: true, single: true },
  { namelist: "納豆嫌い", single: true },
  { namelist: "納豆嫌い, ick, patooey" },
  { namelist: "ick, patooey, 納豆嫌い" },
  { namelist: "納豆嫌い, 納豆大嫌い" },
  { namelist: "納豆嫌い, 納豆大嫌い, 納豆本当に嫌い" },
  { namelist: "納豆嫌い, 納豆大嫌い, 納豆本当に嫌い, 納豆は好みではない" },
  { namelist: "arial, helvetica, sans-serif" },
  { namelist: "arial, helvetica, 'times' new roman, sans-serif", invalid: true },
  { namelist: "arial, helvetica, \"times\" new roman, sans-serif", invalid: true },

  { namelist: "arial, helvetica, \"\\\"times new roman\", sans-serif" },
  { namelist: "arial, helvetica, '\\\"times new roman', sans-serif" },
  { namelist: "arial, helvetica, times 'new' roman, sans-serif", invalid: true },
  { namelist: "arial, helvetica, times \"new\" roman, sans-serif", invalid: true },
  { namelist: "\"simple", single: true },
  { namelist: "\\\"simple", single: true },
  { namelist: "\"\\\"simple\"", single: true },
  { namelist: "İsimple", single: true },
  { namelist: "ßsimple", single: true },
  { namelist: "ẙsimple", single: true },

  /* escapes */
  { namelist: "\\s imple", single: true },
  { namelist: "\\073 imple", single: true },

  { namelist: "\\035 simple", single: true },
  { namelist: "sim\\035 ple", single: true },
  { namelist: "simple\\02cinitial", single: true },
  { namelist: "simple, \\02cinitial" },
  { namelist: "sim\\020 \\035 ple", single: true },
  { namelist: "sim\\020 5ple", single: true },
  { namelist: "\\@simple", single: true },
  { namelist: "\\@simple\\;", single: true },
  { namelist: "\\@font-face", single: true },
  { namelist: "\\@font-face\\;", single: true },
  { namelist: "\\031 \\036 px", single: true },
  { namelist: "\\031 \\036 px", single: true },
  { namelist: "\\1f4a9", single: true },
  { namelist: "\\01f4a9", single: true },
  { namelist: "\\0001f4a9", single: true },
  { namelist: "\\AbAb", single: true },

  /* keywords */
  { namelist: "italic", single: true },
  { namelist: "bold", single: true },
  { namelist: "bold italic", single: true },
  { namelist: "italic bold", single: true },
  { namelist: "larger", single: true },
  { namelist: "smaller", single: true },
  { namelist: "bolder", single: true },
  { namelist: "lighter", single: true },
  { namelist: "default", invalid: true, fontonly: true, single: true },
  { namelist: "initial", invalid: true, fontonly: true, single: true },
  { namelist: "inherit", invalid: true, fontonly: true, single: true },
  { namelist: "normal", single: true },
  { namelist: "default, simple", invalid: true },
  { namelist: "initial, simple", invalid: true },
  { namelist: "inherit, simple", invalid: true },
  { namelist: "normal, simple" },
  { namelist: "simple, default", invalid: true },
  { namelist: "simple, initial", invalid: true },
  { namelist: "simple, inherit", invalid: true },
  { namelist: "simple, default bongo" },
  { namelist: "simple, initial bongo" },
  { namelist: "simple, inherit bongo" },
  { namelist: "simple, bongo default" },
  { namelist: "simple, bongo initial" },
  { namelist: "simple, bongo inherit" },
  { namelist: "simple, normal" },
  { namelist: "simple default", single: true },
  { namelist: "simple initial", single: true },
  { namelist: "simple inherit", single: true },
  { namelist: "simple normal", single: true },
  { namelist: "default simple", single: true },
  { namelist: "initial simple", single: true },
  { namelist: "inherit simple", single: true },
  { namelist: "normal simple", single: true },
  { namelist: "caption", single: true }, // these are keywords for the 'font' property but only when in the first position
  { namelist: "icon", single: true },
  { namelist: "menu", single: true }

];

if (window.SpecialPowers && SpecialPowers.getBoolPref("layout.css.unset-value.enabled")) {
  testFontFamilyLists.push(
    { namelist: "unset", invalid: true, fontonly: true, single: true },
    { namelist: "unset, simple", invalid: true },
    { namelist: "simple, unset", invalid: true },
    { namelist: "simple, unset bongo" },
    { namelist: "simple, bongo unset" },
    { namelist: "simple unset", single: true },
    { namelist: "unset simple", single: true });
}

var gTest = 0;

/* strip out just values */
function extractDecl(rule)
{
  var t = rule.replace(/[ \n]+/g, " ");
  t = t.replace(/.*{[ \n]*/, "");
  t = t.replace(/[ \n]*}.*/, "");
  return t;
}


function testStyleRuleParse(decl, invalid) {
  var sheet = document.styleSheets[1];
  var rule = ".test" + gTest++ + " { " + decl + "; }";

  while(sheet.cssRules.length > 0) {
    sheet.deleteRule(0);
  }

  // shouldn't throw but improper handling of punctuation may cause some parsers to throw
  try {
    sheet.insertRule(rule, 0);
  } catch (e) {
    assert_unreached("unexpected error with " + decl + " ==> " + e.name);
  }

  assert_equals(sheet.cssRules.length, 1, 
    "strange number of rules (" + sheet.cssRules.length + ") with " + decl);

  var s = extractDecl(sheet.cssRules[0].cssText);

  if (invalid) {
    assert_equals(s, "", "rule declaration shouldn't parse - " + rule + " ==> " + s);
  } else {
    assert_not_equals(s, "", "rule declaration should parse - " + rule);
    
    // check that the serialization also parses
    var r = ".test" + gTest++ + " { " + s + " }";
    while(sheet.cssRules.length > 0) {
      sheet.deleteRule(0);
    }
    try {
      sheet.insertRule(r, 0);
    } catch (e) {
      assert_unreached("exception occurred parsing serialized form of rule - " + rule + " ==> " + r + " " + e.name);
    }
    var s2 = extractDecl(sheet.cssRules[0].cssText);
    assert_not_equals(s2, "", "serialized form of rule should also parse - " + rule + " ==> " + r);
  }
}

var kDefaultFamilySetting = "onelittlepiggywenttomarket";

function testFontFamilySetterParse(namelist, invalid) {
  var el = document.getElementById("display");

  el.style.fontFamily = kDefaultFamilySetting;
  var def = el.style.fontFamily;
  el.style.fontFamily = namelist;
  if (!invalid) {
    assert_not_equals(el.style.fontFamily, def, "fontFamily setter should parse - " + namelist);
    var parsed = el.style.fontFamily;
    el.style.fontFamily = kDefaultFamilySetting;
    el.style.fontFamily = parsed;
    assert_equals(el.style.fontFamily, parsed, "fontFamily setter should parse serialized form to identical serialization - " + parsed + " ==> " + el.style.fontFamily);
  } else {
    assert_equals(el.style.fontFamily, def, "fontFamily setter shouldn't parse - " + namelist);
  }
}

var kDefaultFontSetting = "16px onelittlepiggywenttomarket";

function testFontSetterParse(n, size, s1, s2, invalid) {
  var el = document.getElementById("display");

  el.style.font = kDefaultFontSetting;
  var def = el.style.font;
  var fp = fontProp(n, size, s1, s2);
  el.style.font = fp;
  if (!invalid) {
    assert_not_equals(el.style.font, def, "font setter should parse - " + fp);
    var parsed = el.style.font;
    el.style.font = kDefaultFontSetting;
    el.style.font = parsed;
    assert_equals(el.style.font, parsed, "font setter should parse serialized form to identical serialization - " + parsed + " ==> " + el.style.font);
  } else {
    assert_equals(el.style.font, def, "font setter shouldn't parse - " + fp);
  }
}

var testFontVariations = [
  { size: "16px" },
  { size: "900px" },
  { size: "900em" },
  { size: "35%" },
  { size: "7832.3%" },
  { size: "xx-large" },
  { size: "larger", s1: "lighter" },
  { size: "16px", s1: "italic" },
  { size: "16px", s1: "italic", s2: "bold" },
  { size: "smaller", s1: "normal" },
  { size: "16px", s1: "normal", s2: "normal" },
  { size: "16px", s1: "400", s2: "normal" },
  { size: "16px", s1: "bolder", s2: "oblique" }
];

function testFamilyNameParsing() {
  var i;
  for (i = 0; i < testFontFamilyLists.length; i++) {
    var tst = testFontFamilyLists[i];
    var n = tst.namelist;
    var t;

    if (!tst.fontonly) {
      t = "font-family: " + n;
      test(function() { testStyleRuleParse(t, tst.invalid); }, t);
      test(function() { testFontFamilySetterParse(n, tst.invalid); }, t + " (setter)");
    }

    var v;
    for (v = 0; v < testFontVariations.length; v++) {
      var f = testFontVariations[v];
      t = font(n, f.size, f.s1, f.s2);
      test(function() { testStyleRuleParse(t, tst.invalid); }, t);
      test(function() { testFontSetterParse(n, f.size, f.s1, f.s2, tst.invalid); }, t + " (setter)");
    }
  }
}

testFamilyNameParsing();

</script>
</body>
</html>