This source file includes following definitions.
- isWindowFeaturesSeparator
- dialog
- setWindowFeature
- dialog
- boolFeature
- floatFeature
- parseDialogFeatures
#include "config.h"
#include "core/page/WindowFeatures.h"
#include "platform/geometry/FloatRect.h"
#include "wtf/Assertions.h"
#include "wtf/MathExtras.h"
#include "wtf/text/StringHash.h"
namespace WebCore {
static bool isWindowFeaturesSeparator(UChar c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '=' || c == ',' || c == '\0';
}
WindowFeatures::WindowFeatures(const String& features)
: xSet(false)
, ySet(false)
, widthSet(false)
, heightSet(false)
, fullscreen(false)
, dialog(false)
{
if (features.length() == 0) {
menuBarVisible = true;
statusBarVisible = true;
toolBarVisible = true;
locationBarVisible = true;
scrollbarsVisible = true;
resizable = true;
return;
}
menuBarVisible = false;
statusBarVisible = false;
toolBarVisible = false;
locationBarVisible = false;
scrollbarsVisible = false;
resizable = true;
int keyBegin, keyEnd;
int valueBegin, valueEnd;
int i = 0;
int length = features.length();
String buffer = features.lower();
while (i < length) {
while (isWindowFeaturesSeparator(buffer[i])) {
if (i >= length)
break;
i++;
}
keyBegin = i;
while (!isWindowFeaturesSeparator(buffer[i]))
i++;
keyEnd = i;
while (buffer[i] != '=') {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
while (isWindowFeaturesSeparator(buffer[i])) {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
valueBegin = i;
while (!isWindowFeaturesSeparator(buffer[i]))
i++;
valueEnd = i;
ASSERT_WITH_SECURITY_IMPLICATION(i <= length);
String keyString(buffer.substring(keyBegin, keyEnd - keyBegin));
String valueString(buffer.substring(valueBegin, valueEnd - valueBegin));
setWindowFeature(keyString, valueString);
}
}
void WindowFeatures::setWindowFeature(const String& keyString, const String& valueString)
{
int value;
if (valueString.isEmpty() || valueString == "yes")
value = 1;
else
value = valueString.toInt();
if (keyString == "left" || keyString == "screenx") {
xSet = true;
x = value;
} else if (keyString == "top" || keyString == "screeny") {
ySet = true;
y = value;
} else if (keyString == "width" || keyString == "innerwidth") {
widthSet = true;
width = value;
} else if (keyString == "height" || keyString == "innerheight") {
heightSet = true;
height = value;
} else if (keyString == "menubar")
menuBarVisible = value;
else if (keyString == "toolbar")
toolBarVisible = value;
else if (keyString == "location")
locationBarVisible = value;
else if (keyString == "status")
statusBarVisible = value;
else if (keyString == "fullscreen")
fullscreen = value;
else if (keyString == "scrollbars")
scrollbarsVisible = value;
else if (value == 1)
additionalFeatures.append(keyString);
}
WindowFeatures::WindowFeatures(const String& dialogFeaturesString, const FloatRect& screenAvailableRect)
: widthSet(true)
, heightSet(true)
, menuBarVisible(false)
, toolBarVisible(false)
, locationBarVisible(false)
, fullscreen(false)
, dialog(true)
{
DialogFeaturesMap features;
parseDialogFeatures(dialogFeaturesString, features);
const bool trusted = false;
width = floatFeature(features, "dialogwidth", 100, screenAvailableRect.width(), 620);
height = floatFeature(features, "dialogheight", 100, screenAvailableRect.height(), 450);
x = floatFeature(features, "dialogleft", screenAvailableRect.x(), screenAvailableRect.maxX() - width, -1);
xSet = x > 0;
y = floatFeature(features, "dialogtop", screenAvailableRect.y(), screenAvailableRect.maxY() - height, -1);
ySet = y > 0;
if (boolFeature(features, "center", true)) {
if (!xSet) {
x = screenAvailableRect.x() + (screenAvailableRect.width() - width) / 2;
xSet = true;
}
if (!ySet) {
y = screenAvailableRect.y() + (screenAvailableRect.height() - height) / 2;
ySet = true;
}
}
resizable = boolFeature(features, "resizable");
scrollbarsVisible = boolFeature(features, "scroll", true);
statusBarVisible = boolFeature(features, "status", !trusted);
}
bool WindowFeatures::boolFeature(const DialogFeaturesMap& features, const char* key, bool defaultValue)
{
DialogFeaturesMap::const_iterator it = features.find(key);
if (it == features.end())
return defaultValue;
const String& value = it->value;
return value.isNull() || value == "1" || value == "yes" || value == "on";
}
float WindowFeatures::floatFeature(const DialogFeaturesMap& features, const char* key, float min, float max, float defaultValue)
{
DialogFeaturesMap::const_iterator it = features.find(key);
if (it == features.end())
return defaultValue;
bool ok;
double parsedNumber = it->value.toDouble(&ok);
if ((!parsedNumber && !ok) || std::isnan(parsedNumber))
return defaultValue;
if (parsedNumber < min || max <= min)
return min;
if (parsedNumber > max)
return max;
return static_cast<int>(parsedNumber);
}
void WindowFeatures::parseDialogFeatures(const String& string, DialogFeaturesMap& map)
{
Vector<String> vector;
string.split(';', vector);
size_t size = vector.size();
for (size_t i = 0; i < size; ++i) {
const String& featureString = vector[i];
size_t separatorPosition = featureString.find('=');
size_t colonPosition = featureString.find(':');
if (separatorPosition != kNotFound && colonPosition != kNotFound)
continue;
if (separatorPosition == kNotFound)
separatorPosition = colonPosition;
String key = featureString.left(separatorPosition).stripWhiteSpace().lower();
String value;
if (separatorPosition != kNotFound) {
value = featureString.substring(separatorPosition + 1).stripWhiteSpace().lower();
value = value.left(value.find(' '));
}
map.set(key, value);
}
}
}