Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb/CSS: Consider unresolved calc() when serializing in Normal mode #3010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions Libraries/LibWeb/CSS/StyleValues/CSSColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <AK/TypeCasts.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>

namespace Web::CSS {

Expand Down Expand Up @@ -90,9 +91,42 @@ CSSColor::Resolved CSSColor::resolve_properties() const
}

// https://www.w3.org/TR/css-color-4/#serializing-color-function-values
String CSSColor::to_string(SerializationMode) const
String CSSColor::to_string(SerializationMode mode) const
{
// FIXME: Do this properly, taking unresolved calculated values into account.
if (mode == SerializationMode::Normal) {
auto convert_percentage = [](ValueComparingNonnullRefPtr<CSSStyleValue> const& value) -> RemoveReference<decltype(value)> {
if (is<PercentageStyleValue>(*value))
return NumberStyleValue::create(value->as_percentage().value() / 100);
return value;
};

auto alpha = convert_percentage(m_properties.alpha);

bool const is_alpha_required = [&]() {
if (is<NumberStyleValue>(*alpha))
return alpha->as_number().value() < 1;
return true;
}();

if (is<NumberStyleValue>(*alpha) && alpha->as_number().value() < 0)
alpha = NumberStyleValue::create(0);

if (is_alpha_required) {
return MUST(String::formatted("color({} {} {} {} / {})",
string_view_from_color_type(m_color_type),
convert_percentage(m_properties.channels[0])->to_string(mode),
convert_percentage(m_properties.channels[1])->to_string(mode),
convert_percentage(m_properties.channels[2])->to_string(mode),
alpha->to_string(mode)));
}

return MUST(String::formatted("color({} {} {} {})",
string_view_from_color_type(m_color_type),
convert_percentage(m_properties.channels[0])->to_string(mode),
convert_percentage(m_properties.channels[1])->to_string(mode),
convert_percentage(m_properties.channels[2])->to_string(mode)));
}

auto resolved = resolve_properties();
if (resolved.alpha == 1) {
return MUST(String::formatted("color({} {} {} {})",
Expand Down
Loading
Loading