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 reseved.
5 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24 #include "core/page/WindowFeatures.h"
26 #include "platform/geometry/FloatRect.h"
27 #include "wtf/Assertions.h"
28 #include "wtf/MathExtras.h"
29 #include "wtf/text/StringHash.h"
33 // Though isspace() considers \t and \v to be whitespace, Win IE doesn't when parsing window features.
34 static bool isWindowFeaturesSeparator(UChar c)
36 return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || c == ',' || c == '\0';
39 WindowFeatures::WindowFeatures(const String& features)
48 The IE rule is: all features except for channelmode and fullscreen default to YES, but
49 if the user specifies a feature string, all features default to NO. (There is no public
50 standard that applies to this method.)
52 <http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/open_0.asp>
53 We always allow a window to be resized, which is consistent with Firefox.
56 if (features.length() == 0) {
57 menuBarVisible = true;
58 statusBarVisible = true;
59 toolBarVisible = true;
60 locationBarVisible = true;
61 scrollbarsVisible = true;
66 menuBarVisible = false;
67 statusBarVisible = false;
68 toolBarVisible = false;
69 locationBarVisible = false;
70 scrollbarsVisible = false;
73 // Tread lightly in this code -- it was specifically designed to mimic Win IE's parsing behavior.
75 int valueBegin, valueEnd;
78 int length = features.length();
79 String buffer = features.lower();
81 // skip to first non-separator, but don't skip past the end of the string
82 while (i < length && isWindowFeaturesSeparator(buffer[i]))
86 // skip to first separator
87 while (i < length && !isWindowFeaturesSeparator(buffer[i]))
91 ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
93 // skip to first '=', but don't skip past a ',' or the end of the string
94 while (i < length && buffer[i] != '=') {
100 ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
102 // skip to first non-separator, but don't skip past a ',' or the end of the string
103 while (i < length && isWindowFeaturesSeparator(buffer[i])) {
104 if (buffer[i] == ',')
110 ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
112 // skip to first separator
113 while (i < length && !isWindowFeaturesSeparator(buffer[i]))
117 ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
119 String keyString(buffer.substring(keyBegin, keyEnd - keyBegin));
120 String valueString(buffer.substring(valueBegin, valueEnd - valueBegin));
121 setWindowFeature(keyString, valueString);
125 void WindowFeatures::setWindowFeature(const String& keyString, const String& valueString)
129 // Listing a key with no value is shorthand for key=yes
130 if (valueString.isEmpty() || valueString == "yes")
133 value = valueString.toInt();
135 // We treat keyString of "resizable" here as an additional feature rather than setting resizeable to true.
136 // This is consistent with Firefox, but could also be handled at another level.
138 if (keyString == "left" || keyString == "screenx") {
141 } else if (keyString == "top" || keyString == "screeny") {
144 } else if (keyString == "width" || keyString == "innerwidth") {
147 } else if (keyString == "height" || keyString == "innerheight") {
150 } else if (keyString == "menubar")
151 menuBarVisible = value;
152 else if (keyString == "toolbar")
153 toolBarVisible = value;
154 else if (keyString == "location")
155 locationBarVisible = value;
156 else if (keyString == "status")
157 statusBarVisible = value;
158 else if (keyString == "fullscreen")
160 else if (keyString == "scrollbars")
161 scrollbarsVisible = value;
163 additionalFeatures.append(keyString);
166 WindowFeatures::WindowFeatures(const String& dialogFeaturesString, const FloatRect& screenAvailableRect)
169 , menuBarVisible(false)
170 , toolBarVisible(false)
171 , locationBarVisible(false)
175 DialogFeaturesMap features;
176 parseDialogFeatures(dialogFeaturesString, features);
178 const bool trusted = false;
180 // The following features from Microsoft's documentation are not implemented:
181 // - default font settings
182 // - width, height, left, and top specified in units other than "px"
183 // - edge (sunken or raised, default is raised)
184 // - dialogHide: trusted && boolFeature(features, "dialoghide"), makes dialog hide when you print
185 // - help: boolFeature(features, "help", true), makes help icon appear in dialog (what does it do on Windows?)
186 // - unadorned: trusted && boolFeature(features, "unadorned");
188 width = floatFeature(features, "dialogwidth", 100, screenAvailableRect.width(), 620); // default here came from frame size of dialog in MacIE
189 height = floatFeature(features, "dialogheight", 100, screenAvailableRect.height(), 450); // default here came from frame size of dialog in MacIE
191 x = floatFeature(features, "dialogleft", screenAvailableRect.x(), screenAvailableRect.maxX() - width, -1);
193 y = floatFeature(features, "dialogtop", screenAvailableRect.y(), screenAvailableRect.maxY() - height, -1);
196 if (boolFeature(features, "center", true)) {
198 x = screenAvailableRect.x() + (screenAvailableRect.width() - width) / 2;
202 y = screenAvailableRect.y() + (screenAvailableRect.height() - height) / 2;
207 resizable = boolFeature(features, "resizable");
208 scrollbarsVisible = boolFeature(features, "scroll", true);
209 statusBarVisible = boolFeature(features, "status", !trusted);
212 bool WindowFeatures::boolFeature(const DialogFeaturesMap& features, const char* key, bool defaultValue)
214 DialogFeaturesMap::const_iterator it = features.find(key);
215 if (it == features.end())
217 const String& value = it->value;
218 return value.isNull() || value == "1" || value == "yes" || value == "on";
221 float WindowFeatures::floatFeature(const DialogFeaturesMap& features, const char* key, float min, float max, float defaultValue)
223 DialogFeaturesMap::const_iterator it = features.find(key);
224 if (it == features.end())
226 // FIXME: The toDouble function does not offer a way to tell "0q" from string with no digits in it: Both
227 // return the number 0 and false for ok. But "0q" should yield the minimum rather than the default.
229 double parsedNumber = it->value.toDouble(&ok);
230 if ((!parsedNumber && !ok) || std::isnan(parsedNumber))
232 if (parsedNumber < min || max <= min)
234 if (parsedNumber > max)
236 // FIXME: Seems strange to cast a double to int and then convert back to a float. Why is this a good idea?
237 return static_cast<int>(parsedNumber);
240 void WindowFeatures::parseDialogFeatures(const String& string, DialogFeaturesMap& map)
242 Vector<String> vector;
243 string.split(';', vector);
244 size_t size = vector.size();
245 for (size_t i = 0; i < size; ++i) {
246 const String& featureString = vector[i];
248 size_t separatorPosition = featureString.find('=');
249 size_t colonPosition = featureString.find(':');
250 if (separatorPosition != kNotFound && colonPosition != kNotFound)
251 continue; // ignore strings that have both = and :
252 if (separatorPosition == kNotFound)
253 separatorPosition = colonPosition;
255 String key = featureString.left(separatorPosition).stripWhiteSpace().lower();
257 // Null string for value indicates key without value.
259 if (separatorPosition != kNotFound) {
260 value = featureString.substring(separatorPosition + 1).stripWhiteSpace().lower();
261 value = value.left(value.find(' '));
268 } // namespace WebCore