SVGPropertyTraits.h [plain text]
#pragma once
#include "CSSParser.h"
#include "Color.h"
#include "ColorSerialization.h"
#include "FloatPoint.h"
#include "FloatRect.h"
#include "QualifiedName.h"
#include "SVGParserUtilities.h"
namespace WebCore {
template<typename PropertyType>
struct SVGPropertyTraits { };
template<>
struct SVGPropertyTraits<bool> {
static bool initialValue() { return false; }
static bool fromString(const String& string) { return string == "true"; }
static Optional<bool> parse(const QualifiedName&, const String&) { ASSERT_NOT_REACHED(); return initialValue(); }
static String toString(bool type) { return type ? "true" : "false"; }
};
template<>
struct SVGPropertyTraits<Color> {
static Color initialValue() { return Color(); }
static Color fromString(const String& string) { return CSSParser::parseColor(string.stripWhiteSpace()); }
static Optional<Color> parse(const QualifiedName&, const String& string)
{
Color color = CSSParser::parseColor(string.stripWhiteSpace());
if (!color.isValid())
return WTF::nullopt;
return color;
}
static String toString(const Color& type) { return serializationForHTML(type); }
};
template<>
struct SVGPropertyTraits<unsigned> {
static unsigned initialValue() { return 0; }
static Optional<unsigned> parse(const QualifiedName&, const String&) { ASSERT_NOT_REACHED(); return initialValue(); }
static String toString(unsigned type) { return String::number(type); }
};
template<>
struct SVGPropertyTraits<int> {
static int initialValue() { return 0; }
static int fromString(const String&string) { return string.toIntStrict(); }
static Optional<int> parse(const QualifiedName&, const String&) { ASSERT_NOT_REACHED(); return initialValue(); }
static String toString(int type) { return String::number(type); }
};
template<>
struct SVGPropertyTraits<std::pair<int, int>> {
static std::pair<int, int> initialValue() { return { }; }
static std::pair<int, int> fromString(const String& string)
{
auto result = parseNumberOptionalNumber(string);
if (!result)
return { };
return std::make_pair(static_cast<int>(std::round(result->first)), static_cast<int>(std::round(result->second)));
}
static Optional<std::pair<int, int>> parse(const QualifiedName&, const String&) { ASSERT_NOT_REACHED(); return initialValue(); }
static String toString(std::pair<int, int>) { ASSERT_NOT_REACHED(); return emptyString(); }
};
template<>
struct SVGPropertyTraits<float> {
static float initialValue() { return 0; }
static float fromString(const String& string)
{
return parseNumber(string).valueOr(0);
}
static Optional<float> parse(const QualifiedName&, const String& string)
{
return parseNumber(string);
}
static String toString(float type) { return String::number(type); }
};
template<>
struct SVGPropertyTraits<std::pair<float, float>> {
static std::pair<float, float> initialValue() { return { }; }
static std::pair<float, float> fromString(const String& string)
{
return parseNumberOptionalNumber(string).valueOr(std::pair<float, float> { });
}
static Optional<std::pair<float, float>> parse(const QualifiedName&, const String&) { ASSERT_NOT_REACHED(); return initialValue(); }
static String toString(std::pair<float, float>) { ASSERT_NOT_REACHED(); return emptyString(); }
};
template<>
struct SVGPropertyTraits<FloatPoint> {
static FloatPoint initialValue() { return FloatPoint(); }
static FloatPoint fromString(const String& string)
{
return parsePoint(string).valueOr(FloatPoint { });
}
static Optional<FloatPoint> parse(const QualifiedName&, const String& string)
{
return parsePoint(string);
}
static String toString(const FloatPoint& type)
{
return makeString(type.x(), ' ', type.y());
}
};
template<>
struct SVGPropertyTraits<FloatRect> {
static FloatRect initialValue() { return FloatRect(); }
static FloatRect fromString(const String& string)
{
return parseRect(string).valueOr(FloatRect { });
}
static Optional<FloatRect> parse(const QualifiedName&, const String& string)
{
return parseRect(string);
}
static String toString(const FloatRect& type)
{
return makeString(type.x(), ' ', type.y(), ' ', type.width(), ' ', type.height());
}
};
template<>
struct SVGPropertyTraits<String> {
static String initialValue() { return String(); }
static String fromString(const String& string) { return string; }
static Optional<String> parse(const QualifiedName&, const String& string) { return string; }
static String toString(const String& string) { return string; }
};
template<typename EnumType>
struct SVGIDLEnumLimits {
static unsigned highestExposedEnumValue() { return SVGPropertyTraits<EnumType>::highestEnumValue(); }
};
}