1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

LibWeb: Plumb calculated StyleValues into CSS::Length

This is a bit hackish, but this way the existance of the calc()
becomes transparent to the user who just wants a Length and doesn't
care where it came from.
This commit is contained in:
Tobias Christiansen 2021-06-12 00:03:15 +02:00 committed by Ali Mohammad Pur
parent 328afa32c6
commit 20667dfff5
4 changed files with 20 additions and 1 deletions

View file

@ -76,6 +76,8 @@ const char* Length::unit_name() const
return "vmax"; return "vmax";
case Type::Vmin: case Type::Vmin:
return "vmin"; return "vmin";
case Type::Calculated:
return "calculated";
} }
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }

View file

@ -16,6 +16,7 @@ public:
enum class Type { enum class Type {
Undefined, Undefined,
Percentage, Percentage,
Calculated,
Auto, Auto,
Cm, Cm,
In, In,
@ -54,6 +55,8 @@ public:
return fallback_for_undefined; return fallback_for_undefined;
if (is_percentage()) if (is_percentage())
return make_px(raw_value() / 100.0f * reference_for_percent); return make_px(raw_value() / 100.0f * reference_for_percent);
if (is_calculated())
return {};
if (is_relative()) if (is_relative())
return make_px(to_px(layout_node)); return make_px(to_px(layout_node));
return *this; return *this;
@ -71,8 +74,9 @@ public:
bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; } bool is_undefined_or_auto() const { return m_type == Type::Undefined || m_type == Type::Auto; }
bool is_undefined() const { return m_type == Type::Undefined; } bool is_undefined() const { return m_type == Type::Undefined; }
bool is_percentage() const { return m_type == Type::Percentage; } bool is_percentage() const { return m_type == Type::Percentage || m_type == Type::Calculated; }
bool is_auto() const { return m_type == Type::Auto; } bool is_auto() const { return m_type == Type::Auto; }
bool is_calculated() const { return m_type == Type::Calculated; }
bool is_absolute() const bool is_absolute() const
{ {
@ -122,6 +126,7 @@ public:
return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm return m_value * ((1.0f / 40.0f) * centimeter_pixels); // 1Q = 1/40th of 1cm
case Type::Undefined: case Type::Undefined:
case Type::Percentage: case Type::Percentage:
case Type::Calculated:
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
@ -144,6 +149,8 @@ public:
return !(*this == other); return !(*this == other);
} }
void set_calculated_style(CalculatedStyleValue* value) { m_calculated_style = value; }
private: private:
float relative_length_to_px(const Layout::Node&) const; float relative_length_to_px(const Layout::Node&) const;
@ -151,6 +158,8 @@ private:
Type m_type { Type::Undefined }; Type m_type { Type::Undefined };
float m_value { 0 }; float m_value { 0 };
CalculatedStyleValue* m_calculated_style { nullptr };
}; };
} }

View file

@ -55,6 +55,13 @@ Length StyleProperties::length_or_fallback(CSS::PropertyID id, const Length& fal
auto value = property(id); auto value = property(id);
if (!value.has_value()) if (!value.has_value())
return fallback; return fallback;
if (value.value()->is_calculated()) {
Length length = Length(0, Length::Type::Calculated);
length.set_calculated_style(verify_cast<CalculatedStyleValue>(value.value().ptr()));
return length;
}
return value.value()->to_length(); return value.value()->to_length();
} }

View file

@ -14,6 +14,7 @@ enum class Source;
} }
namespace Web::CSS { namespace Web::CSS {
class CalculatedStyleValue;
class CSSRule; class CSSRule;
class CSSImportRule; class CSSImportRule;
class CSSStyleDeclaration; class CSSStyleDeclaration;