diff options
-rw-r--r-- | LibHTML/CSS/StyleValue.cpp | 15 | ||||
-rw-r--r-- | LibHTML/CSS/StyleValue.h | 28 | ||||
-rw-r--r-- | LibHTML/Parser/CSSParser.cpp | 17 |
3 files changed, 34 insertions, 26 deletions
diff --git a/LibHTML/CSS/StyleValue.cpp b/LibHTML/CSS/StyleValue.cpp index 1d19d602f9..e0d0a1764d 100644 --- a/LibHTML/CSS/StyleValue.cpp +++ b/LibHTML/CSS/StyleValue.cpp @@ -8,18 +8,3 @@ StyleValue::StyleValue(Type type) StyleValue::~StyleValue() { } - -NonnullRefPtr<StyleValue> StyleValue::parse(const StringView& str) -{ - String string(str); - bool ok; - int as_int = string.to_int(ok); - if (ok) - return adopt(*new LengthStyleValue(Length(as_int, Length::Type::Absolute))); - unsigned as_uint = string.to_uint(ok); - if (ok) - return adopt(*new LengthStyleValue(Length(as_uint, Length::Type::Absolute))); - if (string == "auto") - return adopt(*new LengthStyleValue(Length())); - return adopt(*new StringStyleValue(str)); -} diff --git a/LibHTML/CSS/StyleValue.h b/LibHTML/CSS/StyleValue.h index e195ce0594..574fd8215d 100644 --- a/LibHTML/CSS/StyleValue.h +++ b/LibHTML/CSS/StyleValue.h @@ -8,8 +8,6 @@ class StyleValue : public RefCounted<StyleValue> { public: - static NonnullRefPtr<StyleValue> parse(const StringView&); - virtual ~StyleValue(); enum class Type { @@ -33,30 +31,40 @@ private: class StringStyleValue : public StyleValue { public: - virtual ~StringStyleValue() override {} - StringStyleValue(const String& string) - : StyleValue(Type::String) - , m_string(string) + static NonnullRefPtr<StringStyleValue> create(const String& string) { + return adopt(*new StringStyleValue(string)); } + virtual ~StringStyleValue() override {} String to_string() const override { return m_string; } private: + explicit StringStyleValue(const String& string) + : StyleValue(Type::String) + , m_string(string) + { + } + String m_string; }; class LengthStyleValue : public StyleValue { public: - virtual ~LengthStyleValue() override {} - LengthStyleValue(const Length& length) - : StyleValue(Type::Length) - , m_length(length) + static NonnullRefPtr<LengthStyleValue> create(const Length& length) { + return adopt(*new LengthStyleValue(length)); } + virtual ~LengthStyleValue() override {} String to_string() const override { return m_length.to_string(); } private: + explicit LengthStyleValue(const Length& length) + : StyleValue(Type::Length) + , m_length(length) + { + } + Length m_length; }; diff --git a/LibHTML/Parser/CSSParser.cpp b/LibHTML/Parser/CSSParser.cpp index 314895f581..cfc5667884 100644 --- a/LibHTML/Parser/CSSParser.cpp +++ b/LibHTML/Parser/CSSParser.cpp @@ -3,6 +3,21 @@ #include <ctype.h> #include <stdio.h> +NonnullRefPtr<StyleValue> parse_css_value(const StringView& view) +{ + String string(view); + bool ok; + int as_int = string.to_int(ok); + if (ok) + return LengthStyleValue::create(Length(as_int, Length::Type::Absolute)); + unsigned as_uint = string.to_uint(ok); + if (ok) + return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute)); + if (string == "auto") + return LengthStyleValue::create(Length()); + return StringStyleValue::create(string); +} + NonnullRefPtr<StyleSheet> parse_css(const String& css) { NonnullRefPtrVector<StyleRule> rules; @@ -108,7 +123,7 @@ NonnullRefPtr<StyleSheet> parse_css(const String& css) auto property_value = String::copy(buffer); buffer.clear(); consume_specific(';'); - current_rule.declarations.append(StyleDeclaration::create(property_name, StyleValue::parse(property_value))); + current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value))); }; auto parse_declarations = [&] { |