diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index dc336ed7700..755ecd036b9 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -953,7 +953,13 @@ Optional Parser::consume_a_declaration(TokenStream& input, Neste // 8. If decl’s name is a custom property name string, then set decl’s original text to the segment // of the original source text string corresponding to the tokens of decl’s value. if (is_a_custom_property_name_string(declaration.name)) { - // FIXME: Set the original source text + // TODO: If we could reach inside the source string that the TokenStream uses, we could grab this as + // a single substring instead of having to reconstruct it. + StringBuilder original_text; + for (auto const& value : declaration.value) { + original_text.append(value.original_source_text()); + } + declaration.original_text = original_text.to_string_without_validation(); } // Otherwise, if decl’s value contains a top-level simple block with an associated token of <{-token>, // and also contains any other non- value, return nothing. @@ -1738,7 +1744,7 @@ Optional Parser::convert_to_style_property(Declaration const& dec } auto value_token_stream = TokenStream(declaration.value); - auto value = parse_css_value(property_id.value(), value_token_stream); + auto value = parse_css_value(property_id.value(), value_token_stream, declaration.original_text); if (value.is_error()) { if (value.error() == ParseError::SyntaxError) { dbgln_if(CSS_PARSER_DEBUG, "Unable to parse value for CSS property '{}'.", property_name); @@ -7636,7 +7642,7 @@ bool block_contains_var_or_attr(SimpleBlock const& block) return false; } -Parser::ParseErrorOr> Parser::parse_css_value(PropertyID property_id, TokenStream& unprocessed_tokens) +Parser::ParseErrorOr> Parser::parse_css_value(PropertyID property_id, TokenStream& unprocessed_tokens, Optional original_source_text) { m_context.set_current_property_id(property_id); Vector component_values; @@ -7670,7 +7676,7 @@ Parser::ParseErrorOr> Parser::parse_css_value(Prope } if (property_id == PropertyID::Custom || contains_var_or_attr) - return UnresolvedStyleValue::create(move(component_values), contains_var_or_attr); + return UnresolvedStyleValue::create(move(component_values), contains_var_or_attr, original_source_text); if (component_values.is_empty()) return ParseError::SyntaxError; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 53dc680b51b..df7b98cbe9e 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -229,7 +229,7 @@ private: RefPtr parse_conic_gradient_function(TokenStream&); RefPtr parse_radial_gradient_function(TokenStream&); - ParseErrorOr> parse_css_value(PropertyID, TokenStream&); + ParseErrorOr> parse_css_value(PropertyID, TokenStream&, Optional original_source_text = {}); RefPtr parse_css_value_for_property(PropertyID, TokenStream&); struct PropertyAndValue { PropertyID property; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp index eb72f9cbcf5..fcb3876d403 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2021, Tobias Christiansen - * Copyright (c) 2021-2023, Sam Atkins + * Copyright (c) 2021-2024, Sam Atkins * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause @@ -14,6 +14,9 @@ namespace Web::CSS { String UnresolvedStyleValue::to_string() const { + if (m_original_source_text.has_value()) + return *m_original_source_text; + StringBuilder builder; for (auto& value : m_values) builder.append(value.to_string()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h index 2114c6ba998..de80a1c1afb 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/UnresolvedStyleValue.h @@ -17,9 +17,9 @@ namespace Web::CSS { class UnresolvedStyleValue final : public CSSStyleValue { public: - static ValueComparingNonnullRefPtr create(Vector&& values, bool contains_var_or_attr) + static ValueComparingNonnullRefPtr create(Vector&& values, bool contains_var_or_attr, Optional original_source_text) { - return adopt_ref(*new (nothrow) UnresolvedStyleValue(move(values), contains_var_or_attr)); + return adopt_ref(*new (nothrow) UnresolvedStyleValue(move(values), contains_var_or_attr, move(original_source_text))); } virtual ~UnresolvedStyleValue() override = default; @@ -31,15 +31,17 @@ public: virtual bool equals(CSSStyleValue const& other) const override; private: - UnresolvedStyleValue(Vector&& values, bool contains_var_or_attr) + UnresolvedStyleValue(Vector&& values, bool contains_var_or_attr, Optional original_source_text) : CSSStyleValue(Type::Unresolved) , m_values(move(values)) , m_contains_var_or_attr(contains_var_or_attr) + , m_original_source_text(move(original_source_text)) { } Vector m_values; bool m_contains_var_or_attr { false }; + Optional m_original_source_text; }; }