summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_parser_diagnostics_unprintables.html
blob: d5a01e3299dbbe9fe0f14d8bff6bab0dcd5c32a6 (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
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for CSS parser diagnostics escaping unprintable
         characters correctly</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<a target="_blank"
   href="https://bugzilla.mozilla.org/show_bug.cgi?id=229827"
>Mozilla Bug 229827</a>
<style id="testbench"></style>
<script type="application/javascript">
// This test has intimate knowledge of how to get the CSS parser to
// emit diagnostics that contain text under control of the user.
// That's not the point of the test, though; the point is only that
// *that text* is properly escaped.

SpecialPowers.wrap(window).docShell.cssErrorReportingEnabled = true;

// There is one "pattern" for each code path through the error reporter
// that might need to escape some kind of user-supplied text.
// Each "pattern" is tested once with each of the "substitution"s below:
// <t>, <i>, and <s> are replaced by the t:, i:, and s: fields of
// each substitution object in turn.
let patterns = [
  // REPORT_UNEXPECTED_P (only ever used in contexts where identifier-like
  // escaping is appropriate)
  { i: "<t>|x{}",                 o: "prefix \u2018<i>\u2019" },
  // REPORT_UNEXPECTED_TOKEN with:
  // _Ident
  { i: "@namespace fnord <t>;",    o: "within @namespace: \u2018<i>\u2019" },
  // _Ref
  { i: "@namespace fnord #<t>;",   o: "within @namespace: \u2018#<i>\u2019" },
  // _Function
  { i: "@namespace fnord <t>();",  o: "within @namespace: \u2018<i>(\u2019" },
  // _Dimension
  { i: "@namespace fnord 14<t>;",  o: "within @namespace: \u201814<i>\u2019" },
  // _AtKeyword
  { i: "x{@<t>: }",        o: "declaration but found \u2018@<i>\u2019." },
  // _String
  { i: "x{ '<t>'}" ,       o: 'declaration but found \u2018"<s>"\u2019.' },
  // _Bad_String
  { i: "x{ '<t>\n}",       o: 'declaration but found \u2018"<s>\u2019.' },
];

// Blocks of characters to test, and how they should be escaped when
// they appear in identifiers and string constants.
const substitutions = [
  // ASCII printables that _can_ normally appear in identifiers,
  // so should of course _not_ be escaped.
  { t: "-_0123456789",               i: "-_0123456789",
                                     s: "-_0123456789" },
  { t: "abcdefghijklmnopqrstuvwxyz", i: "abcdefghijklmnopqrstuvwxyz",
                                     s: "abcdefghijklmnopqrstuvwxyz" },
  { t: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", i: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                                     s: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" },

  // ASCII printables that are not normally valid as the first character
  // of an identifier, or the character immediately after a leading dash,
  // but can be forced into that position with escapes.
  { t: "\\-",    i: "\\-",    s: "-"  },
  { t: "\\30 ",  i: "\\30 ",  s: "0"  },
  { t: "\\31 ",  i: "\\31 ",  s: "1"  },
  { t: "\\32 ",  i: "\\32 ",  s: "2"  },
  { t: "\\33 ",  i: "\\33 ",  s: "3"  },
  { t: "\\34 ",  i: "\\34 ",  s: "4"  },
  { t: "\\35 ",  i: "\\35 ",  s: "5"  },
  { t: "\\36 ",  i: "\\36 ",  s: "6"  },
  { t: "\\37 ",  i: "\\37 ",  s: "7"  },
  { t: "\\38 ",  i: "\\38 ",  s: "8"  },
  { t: "\\39 ",  i: "\\39 ",  s: "9"  },
  { t: "-\\-",   i: "--",     s: "--" },
  { t: "-\\30 ", i: "-\\30 ", s: "-0" },
  { t: "-\\31 ", i: "-\\31 ", s: "-1" },
  { t: "-\\32 ", i: "-\\32 ", s: "-2" },
  { t: "-\\33 ", i: "-\\33 ", s: "-3" },
  { t: "-\\34 ", i: "-\\34 ", s: "-4" },
  { t: "-\\35 ", i: "-\\35 ", s: "-5" },
  { t: "-\\36 ", i: "-\\36 ", s: "-6" },
  { t: "-\\37 ", i: "-\\37 ", s: "-7" },
  { t: "-\\38 ", i: "-\\38 ", s: "-8" },
  { t: "-\\39 ", i: "-\\39 ", s: "-9" },

  // ASCII printables that must be escaped in identifiers.
  // Most of these should not be escaped in strings.
  { t: "\\!\\\"\\#\\$",   i: "\\!\\\"\\#\\$",   s: "!\\\"#$" },
  { t: "\\%\\&\\'\\(",    i: "\\%\\&\\'\\(",    s: "%&'(" },
  { t: "\\)\\*\\+\\,",    i: "\\)\\*\\+\\,",    s: ")*+," },
  { t: "\\.\\/\\:\\;",    i: "\\.\\/\\:\\;",    s: "./:;" },
  { t: "\\<\\=\\>\\?",    i: "\\<\\=\\>\\?",    s: "<=>?", },
  { t: "\\@\\[\\\\\\]",   i: "\\@\\[\\\\\\]",   s: "@[\\\\]" },
  { t: "\\^\\`\\{\\}\\~", i: "\\^\\`\\{\\}\\~", s: "^`{}~" },

  // U+0000 - U+0020 (C0 controls, space)
  // U+000A LINE FEED, U+000C FORM FEED, and U+000D CARRIAGE RETURN
  // cannot be put into a CSS token as escaped literal characters, so
  // we do them with hex escapes instead.
  // The parser replaces U+0000 with U+FFFD.
  { t: "\\\x00\\\x01\\\x02\\\x03",       i: "�\\1 \\2 \\3 ",
                                         s: "�\\1 \\2 \\3 " },
  { t: "\\\x04\\\x05\\\x06\\\x07",       i: "\\4 \\5 \\6 \\7 ",
                                         s: "\\4 \\5 \\6 \\7 " },
  { t: "\\\x08\\\x09\\000A\\\x0B",       i: "\\8 \\9 \\a \\b ",
                                         s: "\\8 \\9 \\a \\b " },
  { t: "\\000C\\000D\\\x0E\\\x0F",       i: "\\c \\d \\e \\f ",
                                         s: "\\c \\d \\e \\f " },
  { t: "\\\x10\\\x11\\\x12\\\x13",       i: "\\10 \\11 \\12 \\13 ",
                                         s: "\\10 \\11 \\12 \\13 " },
  { t: "\\\x14\\\x15\\\x16\\\x17",       i: "\\14 \\15 \\16 \\17 ",
                                         s: "\\14 \\15 \\16 \\17 " },
  { t: "\\\x18\\\x19\\\x1A\\\x1B",       i: "\\18 \\19 \\1a \\1b ",
                                         s: "\\18 \\19 \\1a \\1b " },
  { t: "\\\x1C\\\x1D\\\x1E\\\x1F\\ ",    i: "\\1c \\1d \\1e \\1f \\ ",
                                         s: "\\1c \\1d \\1e \\1f  " },

  // U+007F (DELETE) and U+0080 - U+009F (C1 controls)
  { t: "\\\x7f\\\x80\\\x81\\\x82",       i: "\\7f \x80\x81\x82",
                                         s: "\\7f \x80\x81\x82" },
  { t: "\\\x83\\\x84\\\x85\\\x86",       i: "\x83\x84\x85\x86",
                                         s: "\x83\x84\x85\x86" },
  { t: "\\\x87\\\x88\\\x89\\\x8A",       i: "\x87\x88\x89\x8A",
                                         s: "\x87\x88\x89\x8A" },
  { t: "\\\x8B\\\x8C\\\x8D\\\x8E",       i: "\x8B\x8C\x8D\x8E",
                                         s: "\x8B\x8C\x8D\x8E" },
  { t: "\\\x8F\\\x90\\\x91\\\x92",       i: "\x8F\x90\x91\x92",
                                         s: "\x8F\x90\x91\x92" },
  { t: "\\\x93\\\x94\\\x95\\\x96",       i: "\x93\x94\x95\x96",
                                         s: "\x93\x94\x95\x96" },
  { t: "\\\x97\\\x98\\\x99\\\x9A",       i: "\x97\x98\x99\x9A",
                                         s: "\x97\x98\x99\x9A" },
  { t: "\\\x9B\\\x9C\\\x9D\\\x9E\\\x9F", i: "\x9B\x9C\x9D\x9E\x9F",
                                         s: "\x9B\x9C\x9D\x9E\x9F" },

  // CSS doesn't bother with the full Unicode rules for identifiers,
  // instead declaring that any code point greater than or equal to
  // U+0080 is a valid identifier character.  Test a small handful
  // of both basic and astral plane characters.

  // Arabic (caution to editors: there is a possibly-invisible U+200E
  // LEFT-TO-RIGHT MARK in each string, just before the close quote)
  { t: "أبجدهوزحطيكلمنسعفصقرشتثخذضظغ‎",
    i: "أبجدهوزحطيكلمنسعفصقرشتثخذضظغ‎",
    s: "أبجدهوزحطيكلمنسعفصقرشتثخذضظغ‎" },

  // Box drawing
  { t: "─│┌┐└┘├┤┬┴┼╭╮╯╰╴╵╶╷",
    i: "─│┌┐└┘├┤┬┴┼╭╮╯╰╴╵╶╷",
    s: "─│┌┐└┘├┤┬┴┼╭╮╯╰╴╵╶╷" },

  // CJK Unified Ideographs
  { t: "一丁丂七丄丅丆万丈三上下丌不与丏",
    i: "一丁丂七丄丅丆万丈三上下丌不与丏",
    s: "一丁丂七丄丅丆万丈三上下丌不与丏" },

  // CJK Unified Ideographs Extension B (astral)
  { t: "𠀀𠀁𠀂𠀃𠀄𠀅𠀆𠀇𠀈𠀉𠀊𠀋𠀌𠀍𠀎𠀏",
    i: "𠀀𠀁𠀂𠀃𠀄𠀅𠀆𠀇𠀈𠀉𠀊𠀋𠀌𠀍𠀎𠀏",
    s: "𠀀𠀁𠀂𠀃𠀄𠀅𠀆𠀇𠀈𠀉𠀊𠀋𠀌𠀍𠀎𠀏" },

  // Devanagari
  { t: "कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह",
    i: "कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह",
    s: "कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह" },

  // Emoticons (astral)
  { t: "😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐",
    i: "😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐",
    s: "😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐" },

  // Greek
  { t: "αβγδεζηθικλμνξοπρςστυφχψω",
    i: "αβγδεζηθικλμνξοπρςστυφχψω",
    s: "αβγδεζηθικλμνξοπρςστυφχψω" }
];

const npatterns = patterns.length;
const nsubstitutions = substitutions.length;

function quotemeta(str) {
  return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
function subst(str, sub) {
  return str.replace("<t>", sub.t)
    .replace("<i>", sub.i)
    .replace("<s>", sub.s);
}

var curpat = 0;
var cursubst = -1;
var testbench = document.getElementById("testbench");

function nextTest() {
  cursubst++;
  if (cursubst == nsubstitutions) {
    curpat++;
    cursubst = 0;
  }
  if (curpat == npatterns) {
    SimpleTest.finish();
    return;
  }

  let css = subst(patterns[curpat].i, substitutions[cursubst]);
  let msg = quotemeta(subst(patterns[curpat].o, substitutions[cursubst]));

  SimpleTest.expectConsoleMessages(function () { testbench.innerHTML = css },
                                   [{ errorMessage: new RegExp(msg) }],
                                   nextTest);
}

SimpleTest.waitForExplicitFinish();
nextTest();
</script>
</body>
</html>