OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2000 Harri Porten (porten@kde.org) | |
3 * Copyright (C) 2006 Jon Shier (jshier@iastate.edu) | |
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights | |
5 * reseved. | |
6 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) | |
7 * | |
8 * This library is free software; you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public | |
10 * License as published by the Free Software Foundation; either | |
11 * version 2 of the License, or (at your option) any later version. | |
12 * | |
13 * This library is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this library; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
21 * USA | |
22 */ | |
23 | |
24 #include "core/page/WindowFeatures.h" | |
25 | |
26 #include "platform/geometry/IntRect.h" | |
27 #include "platform/wtf/Assertions.h" | |
28 #include "platform/wtf/MathExtras.h" | |
29 #include "platform/wtf/text/StringHash.h" | |
30 | |
31 namespace blink { | |
32 | |
33 // Though isspace() considers \t and \v to be whitespace, Win IE doesn't when | |
34 // parsing window features. | |
35 static bool IsWindowFeaturesSeparator(UChar c) { | |
36 return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || | |
37 c == ',' || c == '\0'; | |
38 } | |
39 | |
40 WindowFeatures::WindowFeatures(const String& features) | |
41 : x(0), | |
42 x_set(false), | |
43 y(0), | |
44 y_set(false), | |
45 width(0), | |
46 width_set(false), | |
47 height(0), | |
48 height_set(false), | |
49 resizable(true), | |
50 fullscreen(false), | |
51 dialog(false), | |
52 noopener(false) { | |
53 /* | |
54 The IE rule is: all features except for channelmode and fullscreen default | |
55 to YES, but if the user specifies a feature string, all features default to | |
56 NO. (There is no public standard that applies to this method.) | |
57 | |
58 <http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/open_0.a
sp> | |
59 We always allow a window to be resized, which is consistent with Firefox. | |
60 */ | |
61 | |
62 if (features.IsEmpty()) { | |
63 menu_bar_visible = true; | |
64 status_bar_visible = true; | |
65 tool_bar_visible = true; | |
66 location_bar_visible = true; | |
67 scrollbars_visible = true; | |
68 return; | |
69 } | |
70 | |
71 menu_bar_visible = false; | |
72 status_bar_visible = false; | |
73 tool_bar_visible = false; | |
74 location_bar_visible = false; | |
75 scrollbars_visible = false; | |
76 | |
77 // Tread lightly in this code -- it was specifically designed to mimic Win | |
78 // IE's parsing behavior. | |
79 unsigned key_begin, key_end; | |
80 unsigned value_begin, value_end; | |
81 | |
82 String buffer = features.DeprecatedLower(); | |
83 unsigned length = buffer.length(); | |
84 for (unsigned i = 0; i < length;) { | |
85 // skip to first non-separator, but don't skip past the end of the string | |
86 while (i < length && IsWindowFeaturesSeparator(buffer[i])) | |
87 i++; | |
88 key_begin = i; | |
89 | |
90 // skip to first separator | |
91 while (i < length && !IsWindowFeaturesSeparator(buffer[i])) | |
92 i++; | |
93 key_end = i; | |
94 | |
95 SECURITY_DCHECK(i <= length); | |
96 | |
97 // skip to first '=', but don't skip past a ',' or the end of the string | |
98 while (i < length && buffer[i] != '=') { | |
99 if (buffer[i] == ',') | |
100 break; | |
101 i++; | |
102 } | |
103 | |
104 SECURITY_DCHECK(i <= length); | |
105 | |
106 // Skip to first non-separator, but don't skip past a ',' or the end of the | |
107 // string. | |
108 while (i < length && IsWindowFeaturesSeparator(buffer[i])) { | |
109 if (buffer[i] == ',') | |
110 break; | |
111 i++; | |
112 } | |
113 value_begin = i; | |
114 | |
115 SECURITY_DCHECK(i <= length); | |
116 | |
117 // skip to first separator | |
118 while (i < length && !IsWindowFeaturesSeparator(buffer[i])) | |
119 i++; | |
120 value_end = i; | |
121 | |
122 SECURITY_DCHECK(i <= length); | |
123 | |
124 String key_string(buffer.Substring(key_begin, key_end - key_begin)); | |
125 String value_string(buffer.Substring(value_begin, value_end - value_begin)); | |
126 | |
127 SetWindowFeature(key_string, value_string); | |
128 } | |
129 } | |
130 | |
131 void WindowFeatures::SetWindowFeature(const String& key_string, | |
132 const String& value_string) { | |
133 int value; | |
134 | |
135 // Listing a key with no value is shorthand for key=yes | |
136 if (value_string.IsEmpty() || value_string == "yes") | |
137 value = 1; | |
138 else | |
139 value = value_string.ToInt(); | |
140 | |
141 // We treat keyString of "resizable" here as an additional feature rather than | |
142 // setting resizeable to true. This is consistent with Firefox, but could | |
143 // also be handled at another level. | |
144 | |
145 if (key_string == "left" || key_string == "screenx") { | |
146 x_set = true; | |
147 x = value; | |
148 } else if (key_string == "top" || key_string == "screeny") { | |
149 y_set = true; | |
150 y = value; | |
151 } else if (key_string == "width" || key_string == "innerwidth") { | |
152 width_set = true; | |
153 width = value; | |
154 } else if (key_string == "height" || key_string == "innerheight") { | |
155 height_set = true; | |
156 height = value; | |
157 } else if (key_string == "menubar") { | |
158 menu_bar_visible = value; | |
159 } else if (key_string == "toolbar") { | |
160 tool_bar_visible = value; | |
161 } else if (key_string == "location") { | |
162 location_bar_visible = value; | |
163 } else if (key_string == "status") { | |
164 status_bar_visible = value; | |
165 } else if (key_string == "fullscreen") { | |
166 fullscreen = value; | |
167 } else if (key_string == "scrollbars") { | |
168 scrollbars_visible = value; | |
169 } else if (key_string == "noopener") { | |
170 noopener = true; | |
171 } else if (value == 1) { | |
172 additional_features.push_back(key_string); | |
173 } | |
174 } | |
175 | |
176 WindowFeatures::WindowFeatures(const String& dialog_features_string, | |
177 const IntRect& screen_available_rect) | |
178 : width_set(true), | |
179 height_set(true), | |
180 menu_bar_visible(false), | |
181 tool_bar_visible(false), | |
182 location_bar_visible(false), | |
183 fullscreen(false), | |
184 dialog(true), | |
185 noopener(false) { | |
186 DialogFeaturesMap features; | |
187 ParseDialogFeatures(dialog_features_string, features); | |
188 | |
189 const bool kTrusted = false; | |
190 | |
191 // The following features from Microsoft's documentation are not implemented: | |
192 // - default font settings | |
193 // - width, height, left, and top specified in units other than "px" | |
194 // - edge (sunken or raised, default is raised) | |
195 // - dialogHide: trusted && boolFeature(features, "dialoghide"), makes dialog | |
196 // hide when you print | |
197 // - help: boolFeature(features, "help", true), makes help icon appear in | |
198 // dialog (what does it do on Windows?) | |
199 // - unadorned: trusted && boolFeature(features, "unadorned"); | |
200 | |
201 // default here came from frame size of dialog in MacIE | |
202 width = IntFeature(features, "dialogwidth", 100, | |
203 screen_available_rect.Width(), 620); | |
204 // default here came from frame size of dialog in MacIE | |
205 height = IntFeature(features, "dialogheight", 100, | |
206 screen_available_rect.Height(), 450); | |
207 | |
208 x = IntFeature(features, "dialogleft", screen_available_rect.X(), | |
209 screen_available_rect.MaxX() - width, -1); | |
210 x_set = x > 0; | |
211 y = IntFeature(features, "dialogtop", screen_available_rect.Y(), | |
212 screen_available_rect.MaxY() - height, -1); | |
213 y_set = y > 0; | |
214 | |
215 if (BoolFeature(features, "center", true)) { | |
216 if (!x_set) { | |
217 x = screen_available_rect.X() + | |
218 (screen_available_rect.Width() - width) / 2; | |
219 x_set = true; | |
220 } | |
221 if (!y_set) { | |
222 y = screen_available_rect.Y() + | |
223 (screen_available_rect.Height() - height) / 2; | |
224 y_set = true; | |
225 } | |
226 } | |
227 | |
228 resizable = BoolFeature(features, "resizable"); | |
229 scrollbars_visible = BoolFeature(features, "scroll", true); | |
230 status_bar_visible = BoolFeature(features, "status", !kTrusted); | |
231 } | |
232 | |
233 bool WindowFeatures::BoolFeature(const DialogFeaturesMap& features, | |
234 const char* key, | |
235 bool default_value) { | |
236 DialogFeaturesMap::const_iterator it = features.find(key); | |
237 if (it == features.end()) | |
238 return default_value; | |
239 const String& value = it->value; | |
240 return value.IsNull() || value == "1" || value == "yes" || value == "on"; | |
241 } | |
242 | |
243 int WindowFeatures::IntFeature(const DialogFeaturesMap& features, | |
244 const char* key, | |
245 int min, | |
246 int max, | |
247 int default_value) { | |
248 DialogFeaturesMap::const_iterator it = features.find(key); | |
249 if (it == features.end()) | |
250 return default_value; | |
251 bool ok; | |
252 int parsed_number = it->value.ToInt(&ok); | |
253 if (!ok) | |
254 return default_value; | |
255 if (parsed_number < min || max <= min) | |
256 return min; | |
257 if (parsed_number > max) | |
258 return max; | |
259 return parsed_number; | |
260 } | |
261 | |
262 void WindowFeatures::ParseDialogFeatures(const String& string, | |
263 DialogFeaturesMap& map) { | |
264 Vector<String> vector; | |
265 string.Split(';', vector); | |
266 size_t size = vector.size(); | |
267 for (size_t i = 0; i < size; ++i) { | |
268 const String& feature_string = vector[i]; | |
269 | |
270 size_t separator_position = feature_string.find('='); | |
271 size_t colon_position = feature_string.find(':'); | |
272 if (separator_position != kNotFound && colon_position != kNotFound) | |
273 continue; // ignore strings that have both = and : | |
274 if (separator_position == kNotFound) | |
275 separator_position = colon_position; | |
276 | |
277 String key = feature_string.Left(separator_position) | |
278 .StripWhiteSpace() | |
279 .DeprecatedLower(); | |
280 | |
281 // Null string for value indicates key without value. | |
282 String value; | |
283 if (separator_position != kNotFound) { | |
284 value = feature_string.Substring(separator_position + 1) | |
285 .StripWhiteSpace() | |
286 .DeprecatedLower(); | |
287 value = value.Left(value.find(' ')); | |
288 } | |
289 | |
290 map.Set(key, value); | |
291 } | |
292 } | |
293 | |
294 } // namespace blink | |
OLD | NEW |