diff --git a/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Libraries/LibWeb/CSS/Parser/Parser.cpp index cc16c88ff32d2..5abc10937a845 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -3413,7 +3413,7 @@ RefPtr Parser::parse_color_value(TokenStream& tok auto color = Color::from_string(ident); if (color.has_value()) { transaction.commit(); - return CSSColorValue::create_from_color(color.release_value()); + return CSSColorValue::create_from_color(color.release_value(), ident); } // Otherwise, fall through to the hashless-hex-color case } diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp index ac8e688d89e89..d01b603478c1d 100644 --- a/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -2425,6 +2425,17 @@ Optional StyleComputer::compute_style_impl(DOM::Element& elemen start_needed_transitions(*previous_style, style, element, pseudo_element); } + // Remove color names from CSS color values. This is needed because computed values cannot be named colors. + for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) { + auto property_id = (CSS::PropertyID)i; + auto* property = style.maybe_null_property(property_id); + if (property && property->is_color()) { + auto& color_value = property->as_color(); + if (color_value.color_type() == CSSColorValue::ColorType::RGB) + style.set_property(property_id, CSSColorValue::create_from_color(color_value.to_color({}))); + } + } + return style; } diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.cpp b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.cpp index 71d2625c64875..5bf5781d02736 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.cpp @@ -17,14 +17,15 @@ namespace Web::CSS { -ValueComparingNonnullRefPtr CSSColorValue::create_from_color(Color color) +ValueComparingNonnullRefPtr CSSColorValue::create_from_color(Color color, Optional name) { - auto make_rgb_color = [](Color const& color) { + auto make_rgb_color = [name](Color const& color) { return CSSRGB::create( NumberStyleValue::create(color.red()), NumberStyleValue::create(color.green()), NumberStyleValue::create(color.blue()), - NumberStyleValue::create(color.alpha() / 255.0)); + NumberStyleValue::create(color.alpha() / 255.0), + name); }; if (color.value() == 0) { diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h index 8276dcff90f9d..c73d006e24e8d 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/CSSColorValue.h @@ -9,6 +9,7 @@ #pragma once +#include #include #include @@ -17,7 +18,7 @@ namespace Web::CSS { // https://drafts.css-houdini.org/css-typed-om-1/#csscolorvalue class CSSColorValue : public CSSStyleValue { public: - static ValueComparingNonnullRefPtr create_from_color(Color color); + static ValueComparingNonnullRefPtr create_from_color(Color color, Optional name = {}); virtual ~CSSColorValue() override = default; virtual bool has_color() const override { return true; } diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSRGB.cpp b/Libraries/LibWeb/CSS/StyleValues/CSSRGB.cpp index 1db367101cb74..ce3f80be9fb58 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSRGB.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/CSSRGB.cpp @@ -71,6 +71,8 @@ bool CSSRGB::equals(CSSStyleValue const& other) const String CSSRGB::to_string() const { // FIXME: Do this properly, taking unresolved calculated values into account. + if (m_properties.name.has_value()) + return m_properties.name.value().to_string().to_ascii_lowercase(); return serialize_a_srgb_value(to_color({})); } diff --git a/Libraries/LibWeb/CSS/StyleValues/CSSRGB.h b/Libraries/LibWeb/CSS/StyleValues/CSSRGB.h index 6fcfcdc843d60..711fcf7369973 100644 --- a/Libraries/LibWeb/CSS/StyleValues/CSSRGB.h +++ b/Libraries/LibWeb/CSS/StyleValues/CSSRGB.h @@ -14,13 +14,13 @@ namespace Web::CSS { // https://drafts.css-houdini.org/css-typed-om-1/#cssrgb class CSSRGB final : public CSSColorValue { public: - static ValueComparingNonnullRefPtr create(ValueComparingNonnullRefPtr r, ValueComparingNonnullRefPtr g, ValueComparingNonnullRefPtr b, ValueComparingRefPtr alpha = {}) + static ValueComparingNonnullRefPtr create(ValueComparingNonnullRefPtr r, ValueComparingNonnullRefPtr g, ValueComparingNonnullRefPtr b, ValueComparingRefPtr alpha = {}, Optional name = {}) { // alpha defaults to 1 if (!alpha) - return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), NumberStyleValue::create(1))); + return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), NumberStyleValue::create(1), name)); - return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), alpha.release_nonnull())); + return adopt_ref(*new (nothrow) CSSRGB(move(r), move(g), move(b), alpha.release_nonnull(), name)); } virtual ~CSSRGB() override = default; @@ -36,9 +36,9 @@ class CSSRGB final : public CSSColorValue { virtual bool equals(CSSStyleValue const& other) const override; private: - CSSRGB(ValueComparingNonnullRefPtr r, ValueComparingNonnullRefPtr g, ValueComparingNonnullRefPtr b, ValueComparingNonnullRefPtr alpha) + CSSRGB(ValueComparingNonnullRefPtr r, ValueComparingNonnullRefPtr g, ValueComparingNonnullRefPtr b, ValueComparingNonnullRefPtr alpha, Optional name = {}) : CSSColorValue(ColorType::RGB) - , m_properties { .r = move(r), .g = move(g), .b = move(b), .alpha = move(alpha) } + , m_properties { .r = move(r), .g = move(g), .b = move(b), .alpha = move(alpha), .name = name } { } @@ -47,6 +47,7 @@ class CSSRGB final : public CSSColorValue { ValueComparingNonnullRefPtr g; ValueComparingNonnullRefPtr b; ValueComparingNonnullRefPtr alpha; + Optional name; bool operator==(Properties const&) const = default; } m_properties; }; diff --git a/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt b/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt new file mode 100644 index 0000000000000..349d7ed6d4d95 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt @@ -0,0 +1,15 @@ +Summary + +Harness status: OK + +Rerun + +Found 4 tests + +2 Pass +2 Fail +Details +Result Test Name MessagePass Parsing of initial style attribute +Fail Parsing of invalid style attribute +Fail Parsing of style attribute +Pass Update style.backgroundColor \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/domparsing/style_attribute_html.html b/Tests/LibWeb/Text/input/wpt-import/domparsing/style_attribute_html.html new file mode 100644 index 0000000000000..fa34a2cf5295a --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/domparsing/style_attribute_html.html @@ -0,0 +1,52 @@ + + +Style attribute in HTML + + +