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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WindowFeatures.h"
#include "nsINode.h" // IsSpaceCharacter
#include "nsContentUtils.h" // nsContentUtils
#include "nsDependentSubstring.h" // Substring
#include "nsReadableUtils.h" // ToLowerCase
using mozilla::dom::IsSpaceCharacter;
using mozilla::dom::WindowFeatures;
#ifdef DEBUG
/* static */
bool WindowFeatures::IsLowerCase(const char* text) {
nsAutoCString before(text);
nsAutoCString after;
ToLowerCase(before, after);
return before == after;
}
#endif
static bool IsFeatureSeparator(char aChar) {
// https://html.spec.whatwg.org/multipage/window-object.html#feature-separator
// A code point is a feature separator if it is ASCII whitespace, U+003D (=),
// or U+002C (,).
return IsSpaceCharacter(aChar) || aChar == '=' || aChar == ',';
}
template <class IterT, class CondT>
void AdvanceWhile(IterT& aPosition, const IterT& aEnd, CondT aCondition) {
// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
//
// Step 2. While `position` doesn’t point past the end of `input` and the
// code point at `position` within `input` meets the condition condition:
while (aCondition(*aPosition) && aPosition < aEnd) {
// Step 2.1. Append that code point to the end of `result`.
// (done by caller)
// Step 2.2. Advance `position` by 1.
++aPosition;
}
}
template <class IterT, class CondT>
nsTDependentSubstring<char> CollectSequence(IterT& aPosition, const IterT& aEnd,
CondT aCondition) {
// https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
// To collect a sequence of code points meeting a condition `condition` from
// a string `input`, given a position variable `position` tracking the
// position of the calling algorithm within `input`:
// Step 1. Let `result` be the empty string.
auto start = aPosition;
// Step 2.
AdvanceWhile(aPosition, aEnd, aCondition);
// Step 3. Return `result`.
return Substring(start, aPosition);
}
static void NormalizeName(nsAutoCString& aName) {
// https://html.spec.whatwg.org/multipage/window-object.html#normalizing-the-feature-name
if (aName == "screenx") {
aName = "left";
} else if (aName == "screeny") {
aName = "top";
} else if (aName == "innerwidth") {
aName = "width";
} else if (aName == "innerheight") {
aName = "height";
}
}
/* static */
int32_t WindowFeatures::ParseIntegerWithFallback(const nsCString& aValue) {
// https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-parse-boolean
//
// Step 3. Let `parsed` be the result of parsing value as an integer.
nsContentUtils::ParseHTMLIntegerResultFlags parseResult;
int32_t parsed = nsContentUtils::ParseHTMLInteger(aValue, &parseResult);
// Step 4. If `parsed` is an error, then set it to 0.
//
// https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-integers
//
// eParseHTMLInteger_NonStandard is not tested given:
// * Step 4 allows leading whitespaces
// * Step 6 allows a plus sign
// * Step 8 does not disallow leading zeros
// * Steps 6 and 9 allow `-0`
//
// eParseHTMLInteger_DidNotConsumeAllInput is not tested given:
// * Step 8 collects digits and ignores remaining part
//
if (parseResult & nsContentUtils::eParseHTMLInteger_Error) {
parsed = 0;
}
return parsed;
}
/* static */
bool WindowFeatures::ParseBool(const nsCString& aValue) {
// https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-parse-boolean
// To parse a boolean feature given a string `value`:
// Step 1. If `value` is the empty string, then return true.
if (aValue.IsEmpty()) {
return true;
}
// Step 2. If `value` is "yes", then return true.
if (aValue == "yes") {
return true;
}
// Step 3. If `value` is "true", then return
if (aValue == "true") {
return true;
}
// Steps 4-5.
int32_t parsed = ParseIntegerWithFallback(aValue);
// Step 6. Return false if `parsed` is 0, and true otherwise.
return parsed != 0;
}
bool WindowFeatures::Tokenize(const nsACString& aFeatures) {
// https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-tokenize
// To tokenize the `features` argument:
// Step 1. Let `tokenizedFeatures` be a new ordered map.
// (implicit)
// Step 2. Let `position` point at the first code point of features.
auto position = aFeatures.BeginReading();
// Step 3. While `position` is not past the end of `features`:
auto end = aFeatures.EndReading();
while (position < end) {
// Step 3.1. Let `name` be the empty string.
// (implicit)
// Step 3.2. Let `value` be the empty string.
nsAutoCString value;
// Step 3.3. Collect a sequence of code points that are feature separators
// from `features` given `position`. This skips past leading separators
// before the name.
//
// NOTE: Do not collect given this value is unused.
AdvanceWhile(position, end, IsFeatureSeparator);
// Step 3.4. Collect a sequence of code points that are not feature
// separators from `features` given `position`. Set `name` to the collected
// characters, converted to ASCII lowercase.
nsAutoCString name(CollectSequence(
position, end, [](char c) { return !IsFeatureSeparator(c); }));
ToLowerCase(name);
// Step 3.5. Set `name` to the result of normalizing the feature name
// `name`.
NormalizeName(name);
// Step 3.6. While `position` is not past the end of `features` and the
// code point at `position` in features is not U+003D (=):
//
// Step 3.6.1. If the code point at `position` in features is U+002C (,),
// or if it is not a feature separator, then break.
//
// Step 3.6.2. Advance `position` by 1.
//
// NOTE: This skips to the first U+003D (=) but does not skip past a U+002C
// (,) or a non-separator.
//
// The above means skip all whitespaces.
AdvanceWhile(position, end, [](char c) { return IsSpaceCharacter(c); });
// Step 3.7. If the code point at `position` in `features` is a feature
// separator:
if (position < end && IsFeatureSeparator(*position)) {
// Step 3.7.1. While `position` is not past the end of `features` and the
// code point at `position` in `features` is a feature separator:
//
// Step 3.7.1.1. If the code point at `position` in `features` is
// U+002C (,), then break.
//
// Step 3.7.1.2. Advance `position` by 1.
//
// NOTE: This skips to the first non-separator but does not skip past a
// U+002C (,).
AdvanceWhile(position, end,
[](char c) { return IsFeatureSeparator(c) && c != ','; });
// Step 3.7.2. Collect a sequence of code points that are not feature
// separators code points from `features` given `position`. Set `value` to
// the collected code points, converted to ASCII lowercase.
value = CollectSequence(position, end,
[](char c) { return !IsFeatureSeparator(c); });
ToLowerCase(value);
}
// Step 3.8. If `name` is not the empty string, then set
// `tokenizedFeatures[name]` to `value`.
if (!name.IsEmpty()) {
if (!tokenizedFeatures_.put(name, value)) {
return false;
}
}
}
// Step 4. Return `tokenizedFeatures`.
return true;
}
void WindowFeatures::Stringify(nsAutoCString& aOutput) {
MOZ_ASSERT(aOutput.IsEmpty());
for (auto r = tokenizedFeatures_.all(); !r.empty(); r.popFront()) {
if (!aOutput.IsEmpty()) {
aOutput.Append(',');
}
const nsCString& name = r.front().key();
const nsCString& value = r.front().value();
aOutput.Append(name);
if (!value.IsEmpty()) {
aOutput.Append('=');
aOutput.Append(value);
}
}
}
|