/* * Copyright (c) 2020-2021, the SerenityOS developers. * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::CSS::Parser { class ParsingContext { public: ParsingContext(); explicit ParsingContext(JS::Realm&); explicit ParsingContext(DOM::Document const&); explicit ParsingContext(DOM::Document const&, AK::URL); explicit ParsingContext(DOM::ParentNode&); bool in_quirks_mode() const; DOM::Document const* document() const { return m_document; } AK::URL complete_url(String const&) const; PropertyID current_property_id() const { return m_current_property_id; } void set_current_property_id(PropertyID property_id) { m_current_property_id = property_id; } JS::Realm& realm() const { return m_realm; } private: JS::Realm& m_realm; DOM::Document const* m_document { nullptr }; PropertyID m_current_property_id { PropertyID::Invalid }; AK::URL m_url; }; class Parser { public: Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8"); ~Parser() = default; CSSStyleSheet* parse_as_css_stylesheet(Optional location); ElementInlineCSSStyleDeclaration* parse_as_style_attribute(DOM::Element&); CSSRule* parse_as_css_rule(); Optional parse_as_supports_condition(); enum class SelectorParsingMode { Standard, // `` and `` // are handled with this parameter, not as separate functions. // https://drafts.csswg.org/selectors/#forgiving-selector Forgiving }; // Contrary to the name, these parse a comma-separated list of selectors, according to the spec. Optional parse_as_selector(SelectorParsingMode = SelectorParsingMode::Standard); Optional parse_as_relative_selector(SelectorParsingMode = SelectorParsingMode::Standard); NonnullRefPtrVector parse_as_media_query_list(); RefPtr parse_as_media_query(); RefPtr parse_as_supports(); RefPtr parse_as_css_value(PropertyID); static RefPtr parse_css_value(Badge, ParsingContext const&, PropertyID, Vector const&); private: enum class ParseError { IncludesIgnoredVendorPrefix, SyntaxError, }; template using ParseErrorOr = ErrorOr; // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets. struct ParsedStyleSheet { Optional location; NonnullRefPtrVector rules; }; template ParsedStyleSheet parse_a_stylesheet(TokenStream&, Optional location); // "Parse a list of rules" is intended for the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of and . template NonnullRefPtrVector parse_a_list_of_rules(TokenStream&); // "Parse a rule" is intended for use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule. template RefPtr parse_a_rule(TokenStream&); // "Parse a declaration" is used in @supports conditions. [CSS3-CONDITIONAL] template Optional parse_a_declaration(TokenStream&); template Vector parse_a_style_blocks_contents(TokenStream&); // "Parse a list of declarations" is for the contents of a style attribute, which parses text into the contents of a single style rule. template Vector parse_a_list_of_declarations(TokenStream&); // "Parse a component value" is for things that need to consume a single value, like the parsing rules for attr(). template Optional parse_a_component_value(TokenStream&); // "Parse a list of component values" is for the contents of presentational attributes, which parse text into a single declaration’s value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute. template Vector parse_a_list_of_component_values(TokenStream&); template Vector> parse_a_comma_separated_list_of_component_values(TokenStream&); enum class SelectorType { Standalone, Relative }; template ParseErrorOr parse_a_selector_list(TokenStream&, SelectorType, SelectorParsingMode = SelectorParsingMode::Standard); template NonnullRefPtrVector parse_a_media_query_list(TokenStream&); template RefPtr parse_a_supports(TokenStream&); Optional parse_a_n_plus_b_pattern(TokenStream&); enum class TopLevel { No, Yes }; template [[nodiscard]] NonnullRefPtrVector consume_a_list_of_rules(TokenStream&, TopLevel); template [[nodiscard]] NonnullRefPtr consume_an_at_rule(TokenStream&); template RefPtr consume_a_qualified_rule(TokenStream&); template [[nodiscard]] Vector consume_a_style_blocks_contents(TokenStream&); template [[nodiscard]] Vector consume_a_list_of_declarations(TokenStream&); template Optional consume_a_declaration(TokenStream&); template [[nodiscard]] ComponentValue consume_a_component_value(TokenStream&); template NonnullRefPtr consume_a_simple_block(TokenStream&); template NonnullRefPtr consume_a_function(TokenStream&); Optional parse_general_enclosed(TokenStream&); CSSRule* parse_font_face_rule(TokenStream&); Vector parse_font_face_src(TokenStream&); CSSRule* convert_to_rule(NonnullRefPtr); PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector const& declarations); Optional convert_to_style_property(Declaration const&); class Dimension { public: Dimension(Angle&& value) : m_value(move(value)) { } Dimension(Frequency&& value) : m_value(move(value)) { } Dimension(Length&& value) : m_value(move(value)) { } Dimension(Percentage&& value) : m_value(move(value)) { } Dimension(Resolution&& value) : m_value(move(value)) { } Dimension(Time&& value) : m_value(move(value)) { } bool is_angle() const; Angle angle() const; bool is_angle_percentage() const; AnglePercentage angle_percentage() const; bool is_frequency() const; Frequency frequency() const; bool is_frequency_percentage() const; FrequencyPercentage frequency_percentage() const; bool is_length() const; Length length() const; bool is_length_percentage() const; LengthPercentage length_percentage() const; bool is_percentage() const; Percentage percentage() const; bool is_resolution() const; Resolution resolution() const; bool is_time() const; Time time() const; bool is_time_percentage() const; TimePercentage time_percentage() const; private: Variant m_value; }; Optional parse_dimension(ComponentValue const&); Optional parse_rgb_or_hsl_color(StringView function_name, Vector const&); Optional parse_color(ComponentValue const&); Optional parse_length(ComponentValue const&); Optional parse_ratio(TokenStream&); Optional parse_unicode_range(TokenStream&); Optional parse_unicode_range(StringView); Optional parse_grid_size(ComponentValue const&); Optional parse_min_max(Vector const&); Optional parse_repeat(Vector const&); Optional parse_track_sizing_function(ComponentValue const&); Optional parse_position(TokenStream&); enum class AllowedDataUrlType { None, Image, Font, }; Optional parse_url_function(ComponentValue const&, AllowedDataUrlType = AllowedDataUrlType::None); RefPtr parse_linear_gradient_function(ComponentValue const&); RefPtr parse_conic_gradient_function(ComponentValue const&); ParseErrorOr> parse_css_value(PropertyID, TokenStream&); RefPtr parse_css_value(ComponentValue const&); RefPtr parse_builtin_value(ComponentValue const&); RefPtr parse_dynamic_value(ComponentValue const&); RefPtr parse_calculated_value(Vector const&); RefPtr parse_dimension_value(ComponentValue const&); RefPtr parse_numeric_value(ComponentValue const&); RefPtr parse_identifier_value(ComponentValue const&); RefPtr parse_color_value(ComponentValue const&); RefPtr parse_rect_value(ComponentValue const&); RefPtr parse_string_value(ComponentValue const&); RefPtr parse_image_value(ComponentValue const&); template RefPtr parse_comma_separated_value_list(Vector const&, ParseFunction); RefPtr parse_simple_comma_separated_value_list(Vector const&); RefPtr parse_filter_value_list_value(Vector const&); RefPtr parse_background_value(Vector const&); RefPtr parse_single_background_position_value(TokenStream&); RefPtr parse_single_background_repeat_value(TokenStream&); RefPtr parse_single_background_size_value(TokenStream&); RefPtr parse_border_value(Vector const&); RefPtr parse_border_radius_value(Vector const&); RefPtr parse_border_radius_shorthand_value(Vector const&); RefPtr parse_content_value(Vector const&); RefPtr parse_flex_value(Vector const&); RefPtr parse_flex_flow_value(Vector const&); RefPtr parse_font_value(Vector const&); RefPtr parse_font_family_value(Vector const&, size_t start_index = 0); RefPtr parse_list_style_value(Vector const&); RefPtr parse_overflow_value(Vector const&); enum class AllowInsetKeyword { No, Yes, }; RefPtr parse_shadow_value(Vector const&, AllowInsetKeyword); RefPtr parse_single_shadow_value(TokenStream&, AllowInsetKeyword); RefPtr parse_text_decoration_value(Vector const&); RefPtr parse_text_decoration_line_value(TokenStream&); RefPtr parse_transform_value(Vector const&); RefPtr parse_transform_origin_value(Vector const&); RefPtr parse_grid_track_sizes(Vector const&); RefPtr parse_grid_track_placement(Vector const&); RefPtr parse_grid_track_placement_shorthand_value(Vector const&); // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax OwnPtr parse_calc_sum(TokenStream&); OwnPtr parse_calc_product(TokenStream&); Optional parse_calc_value(TokenStream&); OwnPtr parse_calc_number_sum(TokenStream&); OwnPtr parse_calc_number_product(TokenStream&); Optional parse_calc_number_value(TokenStream&); OwnPtr parse_calc_product_part_with_operator(TokenStream&); OwnPtr parse_calc_sum_part_with_operator(TokenStream&); OwnPtr parse_calc_number_product_part_with_operator(TokenStream& tokens); OwnPtr parse_calc_number_sum_part_with_operator(TokenStream&); OwnPtr parse_calc_expression(Vector const&); ParseErrorOr> parse_complex_selector(TokenStream&, SelectorType); ParseErrorOr> parse_compound_selector(TokenStream&); Optional parse_selector_combinator(TokenStream&); ParseErrorOr parse_attribute_simple_selector(ComponentValue const&); ParseErrorOr parse_pseudo_simple_selector(TokenStream&); ParseErrorOr> parse_simple_selector(TokenStream&); NonnullRefPtr parse_media_query(TokenStream&); OwnPtr parse_media_condition(TokenStream&, MediaCondition::AllowOr allow_or); Optional parse_media_feature(TokenStream&); Optional parse_media_type(TokenStream&); OwnPtr parse_media_in_parens(TokenStream&); Optional parse_media_feature_value(MediaFeatureID, TokenStream&); OwnPtr parse_supports_condition(TokenStream&); Optional parse_supports_in_parens(TokenStream&); Optional parse_supports_feature(TokenStream&); static bool has_ignored_vendor_prefix(StringView); static bool is_builtin(StringView); struct PropertiesAndCustomProperties { Vector properties; HashMap custom_properties; }; PropertiesAndCustomProperties extract_properties(Vector const&); ParsingContext m_context; Tokenizer m_tokenizer; Vector m_tokens; TokenStream m_token_stream; }; } namespace Web { CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const&, StringView, Optional location = {}); CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&); RefPtr parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid); Optional parse_selector(CSS::Parser::ParsingContext const&, StringView); CSS::CSSRule* parse_css_rule(CSS::Parser::ParsingContext const&, StringView); RefPtr parse_media_query(CSS::Parser::ParsingContext const&, StringView); NonnullRefPtrVector parse_media_query_list(CSS::Parser::ParsingContext const&, StringView); RefPtr parse_css_supports(CSS::Parser::ParsingContext const&, StringView); }