WindowFeatures.cpp [plain text]
#include "config.h"
#include "WindowFeatures.h"
#include "PlatformString.h"
#include "StringHash.h"
#include <wtf/Assertions.h>
#include <wtf/HashMap.h>
#include <wtf/MathExtras.h>
namespace WebCore {
static bool isSeparator(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 (isSeparator(buffer[i])) {
if (i >= length)
break;
i++;
}
keyBegin = i;
while (!isSeparator(buffer[i]))
i++;
keyEnd = i;
while (buffer[i] != '=') {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
while (isSeparator(buffer[i])) {
if (buffer[i] == ',' || i >= length)
break;
i++;
}
valueBegin = i;
while (!isSeparator(buffer[i]))
i++;
valueEnd = i;
ASSERT(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.length() == 0 || 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;
}
bool WindowFeatures::boolFeature(const HashMap<String, String>& features, const char* key, bool defaultValue)
{
HashMap<String, String>::const_iterator it = features.find(key);
if (it == features.end())
return defaultValue;
const String& value = it->second;
return value.isNull() || value == "1" || value == "yes" || value == "on";
}
float WindowFeatures::floatFeature(const HashMap<String, String>& features, const char* key, float min, float max, float defaultValue)
{
HashMap<String, String>::const_iterator it = features.find(key);
if (it == features.end())
return defaultValue;
bool ok;
double d = it->second.toDouble(&ok);
if ((d == 0 && !ok) || isnan(d))
return defaultValue;
if (d < min || max <= min)
return min;
if (d > max)
return max;
return static_cast<int>(d);
}
}