Serenity Operating System
at master 383 lines 18 kB view raw
1/* 2 * Copyright (c) 2020-2021, the SerenityOS developers. 3 * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/Error.h> 11#include <AK/RefPtr.h> 12#include <AK/Vector.h> 13#include <LibWeb/CSS/CSSStyleDeclaration.h> 14#include <LibWeb/CSS/FontFace.h> 15#include <LibWeb/CSS/GeneralEnclosed.h> 16#include <LibWeb/CSS/MediaQuery.h> 17#include <LibWeb/CSS/Parser/Block.h> 18#include <LibWeb/CSS/Parser/ComponentValue.h> 19#include <LibWeb/CSS/Parser/Declaration.h> 20#include <LibWeb/CSS/Parser/DeclarationOrAtRule.h> 21#include <LibWeb/CSS/Parser/Function.h> 22#include <LibWeb/CSS/Parser/Rule.h> 23#include <LibWeb/CSS/Parser/TokenStream.h> 24#include <LibWeb/CSS/Parser/Tokenizer.h> 25#include <LibWeb/CSS/Ratio.h> 26#include <LibWeb/CSS/Selector.h> 27#include <LibWeb/CSS/StyleValue.h> 28#include <LibWeb/CSS/Supports.h> 29#include <LibWeb/CSS/UnicodeRange.h> 30#include <LibWeb/Forward.h> 31 32namespace Web::CSS::Parser { 33 34class ParsingContext { 35public: 36 explicit ParsingContext(JS::Realm&); 37 explicit ParsingContext(DOM::Document const&); 38 explicit ParsingContext(DOM::Document const&, AK::URL); 39 explicit ParsingContext(DOM::ParentNode&); 40 41 bool in_quirks_mode() const; 42 DOM::Document const* document() const { return m_document; } 43 AK::URL complete_url(StringView) const; 44 45 PropertyID current_property_id() const { return m_current_property_id; } 46 void set_current_property_id(PropertyID property_id) { m_current_property_id = property_id; } 47 48 JS::Realm& realm() const { return m_realm; } 49 50private: 51 JS::Realm& m_realm; 52 DOM::Document const* m_document { nullptr }; 53 PropertyID m_current_property_id { PropertyID::Invalid }; 54 AK::URL m_url; 55}; 56 57class Parser { 58public: 59 static ErrorOr<Parser> create(ParsingContext const&, StringView input, StringView encoding = "utf-8"sv); 60 61 Parser(Parser&&); 62 63 CSSStyleSheet* parse_as_css_stylesheet(Optional<AK::URL> location); 64 ElementInlineCSSStyleDeclaration* parse_as_style_attribute(DOM::Element&); 65 CSSRule* parse_as_css_rule(); 66 Optional<StyleProperty> parse_as_supports_condition(); 67 68 enum class SelectorParsingMode { 69 Standard, 70 // `<forgiving-selector-list>` and `<forgiving-relative-selector-list>` 71 // are handled with this parameter, not as separate functions. 72 // https://drafts.csswg.org/selectors/#forgiving-selector 73 Forgiving 74 }; 75 // Contrary to the name, these parse a comma-separated list of selectors, according to the spec. 76 Optional<SelectorList> parse_as_selector(SelectorParsingMode = SelectorParsingMode::Standard); 77 Optional<SelectorList> parse_as_relative_selector(SelectorParsingMode = SelectorParsingMode::Standard); 78 79 Vector<NonnullRefPtr<MediaQuery>> parse_as_media_query_list(); 80 RefPtr<MediaQuery> parse_as_media_query(); 81 82 RefPtr<Supports> parse_as_supports(); 83 84 RefPtr<StyleValue> parse_as_css_value(PropertyID); 85 86 static RefPtr<StyleValue> parse_css_value(Badge<StyleComputer>, ParsingContext const&, PropertyID, Vector<ComponentValue> const&); 87 static RefPtr<CalculatedStyleValue> parse_calculated_value(Badge<StyleComputer>, ParsingContext const&, Vector<ComponentValue> const&); 88 89private: 90 Parser(ParsingContext const&, Vector<Token>); 91 92 enum class ParseError { 93 IncludesIgnoredVendorPrefix, 94 SyntaxError, 95 }; 96 template<typename T> 97 using ParseErrorOr = ErrorOr<T, ParseError>; 98 99 // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets. 100 struct ParsedStyleSheet { 101 Optional<AK::URL> location; 102 Vector<NonnullRefPtr<Rule>> rules; 103 }; 104 template<typename T> 105 ParsedStyleSheet parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location); 106 107 // "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 <CDO-token> and <CDC-token>. 108 template<typename T> 109 Vector<NonnullRefPtr<Rule>> parse_a_list_of_rules(TokenStream<T>&); 110 111 // "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. 112 template<typename T> 113 RefPtr<Rule> parse_a_rule(TokenStream<T>&); 114 115 // "Parse a declaration" is used in @supports conditions. [CSS3-CONDITIONAL] 116 template<typename T> 117 Optional<Declaration> parse_a_declaration(TokenStream<T>&); 118 119 template<typename T> 120 Vector<DeclarationOrAtRule> parse_a_style_blocks_contents(TokenStream<T>&); 121 122 // "Parse a list of declarations" is for the contents of a style attribute, which parses text into the contents of a single style rule. 123 template<typename T> 124 Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&); 125 126 // "Parse a component value" is for things that need to consume a single value, like the parsing rules for attr(). 127 template<typename T> 128 Optional<ComponentValue> parse_a_component_value(TokenStream<T>&); 129 130 // "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. 131 template<typename T> 132 Vector<ComponentValue> parse_a_list_of_component_values(TokenStream<T>&); 133 134 template<typename T> 135 Vector<Vector<ComponentValue>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&); 136 137 enum class SelectorType { 138 Standalone, 139 Relative 140 }; 141 template<typename T> 142 ParseErrorOr<SelectorList> parse_a_selector_list(TokenStream<T>&, SelectorType, SelectorParsingMode = SelectorParsingMode::Standard); 143 144 template<typename T> 145 Vector<NonnullRefPtr<MediaQuery>> parse_a_media_query_list(TokenStream<T>&); 146 template<typename T> 147 RefPtr<Supports> parse_a_supports(TokenStream<T>&); 148 149 Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<ComponentValue>&); 150 151 enum class TopLevel { 152 No, 153 Yes 154 }; 155 template<typename T> 156 [[nodiscard]] Vector<NonnullRefPtr<Rule>> consume_a_list_of_rules(TokenStream<T>&, TopLevel); 157 template<typename T> 158 [[nodiscard]] NonnullRefPtr<Rule> consume_an_at_rule(TokenStream<T>&); 159 template<typename T> 160 RefPtr<Rule> consume_a_qualified_rule(TokenStream<T>&); 161 template<typename T> 162 [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_style_blocks_contents(TokenStream<T>&); 163 template<typename T> 164 [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&); 165 template<typename T> 166 Optional<Declaration> consume_a_declaration(TokenStream<T>&); 167 template<typename T> 168 [[nodiscard]] ComponentValue consume_a_component_value(TokenStream<T>&); 169 template<typename T> 170 NonnullRefPtr<Block> consume_a_simple_block(TokenStream<T>&); 171 template<typename T> 172 NonnullRefPtr<Function> consume_a_function(TokenStream<T>&); 173 174 Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<ComponentValue>&); 175 176 CSSRule* parse_font_face_rule(TokenStream<ComponentValue>&); 177 Vector<FontFace::Source> parse_font_face_src(TokenStream<ComponentValue>&); 178 179 CSSRule* convert_to_rule(NonnullRefPtr<Rule>); 180 PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector<DeclarationOrAtRule> const& declarations); 181 Optional<StyleProperty> convert_to_style_property(Declaration const&); 182 183 class Dimension { 184 public: 185 Dimension(Angle&& value) 186 : m_value(move(value)) 187 { 188 } 189 190 Dimension(Frequency&& value) 191 : m_value(move(value)) 192 { 193 } 194 195 Dimension(Length&& value) 196 : m_value(move(value)) 197 { 198 } 199 Dimension(Percentage&& value) 200 : m_value(move(value)) 201 { 202 } 203 204 Dimension(Resolution&& value) 205 : m_value(move(value)) 206 { 207 } 208 209 Dimension(Time&& value) 210 : m_value(move(value)) 211 { 212 } 213 214 bool is_angle() const; 215 Angle angle() const; 216 217 bool is_angle_percentage() const; 218 AnglePercentage angle_percentage() const; 219 220 bool is_frequency() const; 221 Frequency frequency() const; 222 223 bool is_frequency_percentage() const; 224 FrequencyPercentage frequency_percentage() const; 225 226 bool is_length() const; 227 Length length() const; 228 229 bool is_length_percentage() const; 230 LengthPercentage length_percentage() const; 231 232 bool is_percentage() const; 233 Percentage percentage() const; 234 235 bool is_resolution() const; 236 Resolution resolution() const; 237 238 bool is_time() const; 239 Time time() const; 240 241 bool is_time_percentage() const; 242 TimePercentage time_percentage() const; 243 244 private: 245 Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value; 246 }; 247 Optional<Dimension> parse_dimension(ComponentValue const&); 248 Optional<Color> parse_rgb_or_hsl_color(StringView function_name, Vector<ComponentValue> const&); 249 Optional<Color> parse_color(ComponentValue const&); 250 Optional<Length> parse_length(ComponentValue const&); 251 Optional<Ratio> parse_ratio(TokenStream<ComponentValue>&); 252 Optional<UnicodeRange> parse_unicode_range(TokenStream<ComponentValue>&); 253 Optional<UnicodeRange> parse_unicode_range(StringView); 254 Optional<GridSize> parse_grid_size(ComponentValue const&); 255 Optional<GridMinMax> parse_min_max(Vector<ComponentValue> const&); 256 Optional<GridRepeat> parse_repeat(Vector<ComponentValue> const&); 257 Optional<ExplicitGridTrack> parse_track_sizing_function(ComponentValue const&); 258 Optional<PositionValue> parse_position(TokenStream<ComponentValue>&, PositionValue initial_value = PositionValue::center()); 259 260 enum class AllowedDataUrlType { 261 None, 262 Image, 263 Font, 264 }; 265 Optional<AK::URL> parse_url_function(ComponentValue const&, AllowedDataUrlType = AllowedDataUrlType::None); 266 267 Optional<Vector<LinearColorStopListElement>> parse_linear_color_stop_list(TokenStream<ComponentValue>&); 268 Optional<Vector<AngularColorStopListElement>> parse_angular_color_stop_list(TokenStream<ComponentValue>&); 269 270 RefPtr<StyleValue> parse_linear_gradient_function(ComponentValue const&); 271 RefPtr<StyleValue> parse_conic_gradient_function(ComponentValue const&); 272 RefPtr<StyleValue> parse_radial_gradient_function(ComponentValue const&); 273 274 ParseErrorOr<NonnullRefPtr<StyleValue>> parse_css_value(PropertyID, TokenStream<ComponentValue>&); 275 RefPtr<StyleValue> parse_css_value(ComponentValue const&); 276 RefPtr<StyleValue> parse_builtin_value(ComponentValue const&); 277 RefPtr<StyleValue> parse_dynamic_value(ComponentValue const&); 278 RefPtr<CalculatedStyleValue> parse_calculated_value(Vector<ComponentValue> const&); 279 RefPtr<StyleValue> parse_dimension_value(ComponentValue const&); 280 RefPtr<StyleValue> parse_numeric_value(ComponentValue const&); 281 RefPtr<StyleValue> parse_identifier_value(ComponentValue const&); 282 RefPtr<StyleValue> parse_color_value(ComponentValue const&); 283 RefPtr<StyleValue> parse_rect_value(ComponentValue const&); 284 RefPtr<StyleValue> parse_string_value(ComponentValue const&); 285 RefPtr<StyleValue> parse_image_value(ComponentValue const&); 286 template<typename ParseFunction> 287 RefPtr<StyleValue> parse_comma_separated_value_list(Vector<ComponentValue> const&, ParseFunction); 288 RefPtr<StyleValue> parse_simple_comma_separated_value_list(Vector<ComponentValue> const&); 289 290 RefPtr<StyleValue> parse_filter_value_list_value(Vector<ComponentValue> const&); 291 RefPtr<StyleValue> parse_background_value(Vector<ComponentValue> const&); 292 RefPtr<StyleValue> parse_single_background_position_value(TokenStream<ComponentValue>&); 293 RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<ComponentValue>&); 294 RefPtr<StyleValue> parse_single_background_size_value(TokenStream<ComponentValue>&); 295 RefPtr<StyleValue> parse_border_value(Vector<ComponentValue> const&); 296 RefPtr<StyleValue> parse_border_radius_value(Vector<ComponentValue> const&); 297 RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<ComponentValue> const&); 298 RefPtr<StyleValue> parse_content_value(Vector<ComponentValue> const&); 299 RefPtr<StyleValue> parse_flex_value(Vector<ComponentValue> const&); 300 RefPtr<StyleValue> parse_flex_flow_value(Vector<ComponentValue> const&); 301 RefPtr<StyleValue> parse_font_value(Vector<ComponentValue> const&); 302 RefPtr<StyleValue> parse_font_family_value(Vector<ComponentValue> const&, size_t start_index = 0); 303 RefPtr<StyleValue> parse_list_style_value(Vector<ComponentValue> const&); 304 RefPtr<StyleValue> parse_overflow_value(Vector<ComponentValue> const&); 305 enum class AllowInsetKeyword { 306 No, 307 Yes, 308 }; 309 RefPtr<StyleValue> parse_shadow_value(Vector<ComponentValue> const&, AllowInsetKeyword); 310 RefPtr<StyleValue> parse_single_shadow_value(TokenStream<ComponentValue>&, AllowInsetKeyword); 311 RefPtr<StyleValue> parse_text_decoration_value(Vector<ComponentValue> const&); 312 RefPtr<StyleValue> parse_text_decoration_line_value(TokenStream<ComponentValue>&); 313 RefPtr<StyleValue> parse_transform_value(Vector<ComponentValue> const&); 314 RefPtr<StyleValue> parse_transform_origin_value(Vector<ComponentValue> const&); 315 RefPtr<StyleValue> parse_grid_track_sizes(Vector<ComponentValue> const&); 316 RefPtr<StyleValue> parse_grid_track_placement(Vector<ComponentValue> const&); 317 RefPtr<StyleValue> parse_grid_track_placement_shorthand_value(Vector<ComponentValue> const&); 318 RefPtr<StyleValue> parse_grid_template_areas_value(Vector<ComponentValue> const&); 319 RefPtr<StyleValue> parse_grid_area_shorthand_value(Vector<ComponentValue> const&); 320 321 // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax 322 OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(TokenStream<ComponentValue>&); 323 OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(TokenStream<ComponentValue>&); 324 Optional<CalculatedStyleValue::CalcValue> parse_calc_value(TokenStream<ComponentValue>&); 325 OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(TokenStream<ComponentValue>&); 326 OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(TokenStream<ComponentValue>&); 327 Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(TokenStream<ComponentValue>&); 328 OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(TokenStream<ComponentValue>&); 329 OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(TokenStream<ComponentValue>&); 330 OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(TokenStream<ComponentValue>& tokens); 331 OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(TokenStream<ComponentValue>&); 332 OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(Vector<ComponentValue> const&); 333 334 ParseErrorOr<NonnullRefPtr<Selector>> parse_complex_selector(TokenStream<ComponentValue>&, SelectorType); 335 ParseErrorOr<Optional<Selector::CompoundSelector>> parse_compound_selector(TokenStream<ComponentValue>&); 336 Optional<Selector::Combinator> parse_selector_combinator(TokenStream<ComponentValue>&); 337 338 ParseErrorOr<Selector::SimpleSelector> parse_attribute_simple_selector(ComponentValue const&); 339 ParseErrorOr<Selector::SimpleSelector> parse_pseudo_simple_selector(TokenStream<ComponentValue>&); 340 ParseErrorOr<Optional<Selector::SimpleSelector>> parse_simple_selector(TokenStream<ComponentValue>&); 341 342 NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<ComponentValue>&); 343 OwnPtr<MediaCondition> parse_media_condition(TokenStream<ComponentValue>&, MediaCondition::AllowOr allow_or); 344 Optional<MediaFeature> parse_media_feature(TokenStream<ComponentValue>&); 345 Optional<MediaQuery::MediaType> parse_media_type(TokenStream<ComponentValue>&); 346 OwnPtr<MediaCondition> parse_media_in_parens(TokenStream<ComponentValue>&); 347 Optional<MediaFeatureValue> parse_media_feature_value(MediaFeatureID, TokenStream<ComponentValue>&); 348 349 OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<ComponentValue>&); 350 Optional<Supports::InParens> parse_supports_in_parens(TokenStream<ComponentValue>&); 351 Optional<Supports::Feature> parse_supports_feature(TokenStream<ComponentValue>&); 352 353 static bool has_ignored_vendor_prefix(StringView); 354 static bool is_builtin(StringView); 355 356 struct PropertiesAndCustomProperties { 357 Vector<StyleProperty> properties; 358 HashMap<DeprecatedString, StyleProperty> custom_properties; 359 }; 360 361 PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&); 362 363 ParsingContext m_context; 364 365 Vector<Token> m_tokens; 366 TokenStream<Token> m_token_stream; 367}; 368 369} 370 371namespace Web { 372 373CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const&, StringView, Optional<AK::URL> location = {}); 374CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&); 375RefPtr<CSS::StyleValue> parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid); 376Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingContext const&, StringView); 377CSS::CSSRule* parse_css_rule(CSS::Parser::ParsingContext const&, StringView); 378RefPtr<CSS::MediaQuery> parse_media_query(CSS::Parser::ParsingContext const&, StringView); 379Vector<NonnullRefPtr<CSS::MediaQuery>> parse_media_query_list(CSS::Parser::ParsingContext const&, StringView); 380RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingContext const&, StringView); 381Optional<CSS::StyleProperty> parse_css_supports_condition(CSS::Parser::ParsingContext const&, StringView); 382 383}