Parser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #pragma once
  8. #include <AK/Error.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/Vector.h>
  11. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  12. #include <LibWeb/CSS/FontFace.h>
  13. #include <LibWeb/CSS/GeneralEnclosed.h>
  14. #include <LibWeb/CSS/MediaQuery.h>
  15. #include <LibWeb/CSS/Parser/Block.h>
  16. #include <LibWeb/CSS/Parser/ComponentValue.h>
  17. #include <LibWeb/CSS/Parser/Declaration.h>
  18. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  19. #include <LibWeb/CSS/Parser/Dimension.h>
  20. #include <LibWeb/CSS/Parser/Function.h>
  21. #include <LibWeb/CSS/Parser/ParsingContext.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/PropertyID.h>
  26. #include <LibWeb/CSS/Ratio.h>
  27. #include <LibWeb/CSS/Selector.h>
  28. #include <LibWeb/CSS/StyleValue.h>
  29. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  30. #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
  31. #include <LibWeb/CSS/Supports.h>
  32. #include <LibWeb/CSS/UnicodeRange.h>
  33. #include <LibWeb/Forward.h>
  34. namespace Web::CSS::Parser {
  35. class PropertyDependencyNode;
  36. class Parser {
  37. public:
  38. static ErrorOr<Parser> create(ParsingContext const&, StringView input, StringView encoding = "utf-8"sv);
  39. Parser(Parser&&);
  40. CSSStyleSheet* parse_as_css_stylesheet(Optional<AK::URL> location);
  41. ElementInlineCSSStyleDeclaration* parse_as_style_attribute(DOM::Element&);
  42. CSSRule* parse_as_css_rule();
  43. Optional<StyleProperty> parse_as_supports_condition();
  44. enum class SelectorParsingMode {
  45. Standard,
  46. // `<forgiving-selector-list>` and `<forgiving-relative-selector-list>`
  47. // are handled with this parameter, not as separate functions.
  48. // https://drafts.csswg.org/selectors/#forgiving-selector
  49. Forgiving
  50. };
  51. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  52. Optional<SelectorList> parse_as_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  53. Optional<SelectorList> parse_as_relative_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  54. Vector<NonnullRefPtr<MediaQuery>> parse_as_media_query_list();
  55. RefPtr<MediaQuery> parse_as_media_query();
  56. RefPtr<Supports> parse_as_supports();
  57. RefPtr<StyleValue> parse_as_css_value(PropertyID);
  58. Optional<ComponentValue> parse_as_component_value();
  59. static NonnullRefPtr<StyleValue> resolve_unresolved_style_value(Badge<StyleComputer>, ParsingContext const&, DOM::Element&, Optional<CSS::Selector::PseudoElement>, PropertyID, UnresolvedStyleValue const&);
  60. [[nodiscard]] LengthOrCalculated parse_as_sizes_attribute();
  61. private:
  62. Parser(ParsingContext const&, Vector<Token>);
  63. enum class ParseError {
  64. IncludesIgnoredVendorPrefix,
  65. InternalError,
  66. SyntaxError,
  67. };
  68. template<typename T>
  69. using ParseErrorOr = ErrorOr<T, ParseError>;
  70. // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets.
  71. struct ParsedStyleSheet {
  72. Optional<AK::URL> location;
  73. Vector<NonnullRefPtr<Rule>> rules;
  74. };
  75. template<typename T>
  76. ParsedStyleSheet parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
  77. // "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>.
  78. template<typename T>
  79. Vector<NonnullRefPtr<Rule>> parse_a_list_of_rules(TokenStream<T>&);
  80. // "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.
  81. template<typename T>
  82. RefPtr<Rule> parse_a_rule(TokenStream<T>&);
  83. // "Parse a declaration" is used in @supports conditions. [CSS3-CONDITIONAL]
  84. template<typename T>
  85. Optional<Declaration> parse_a_declaration(TokenStream<T>&);
  86. template<typename T>
  87. Vector<DeclarationOrAtRule> parse_a_style_blocks_contents(TokenStream<T>&);
  88. // "Parse a list of declarations" is for the contents of a style attribute, which parses text into the contents of a single style rule.
  89. template<typename T>
  90. Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&);
  91. // "Parse a component value" is for things that need to consume a single value, like the parsing rules for attr().
  92. template<typename T>
  93. Optional<ComponentValue> parse_a_component_value(TokenStream<T>&);
  94. // "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.
  95. template<typename T>
  96. Vector<ComponentValue> parse_a_list_of_component_values(TokenStream<T>&);
  97. template<typename T>
  98. Vector<Vector<ComponentValue>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  99. enum class SelectorType {
  100. Standalone,
  101. Relative
  102. };
  103. template<typename T>
  104. ParseErrorOr<SelectorList> parse_a_selector_list(TokenStream<T>&, SelectorType, SelectorParsingMode = SelectorParsingMode::Standard);
  105. template<typename T>
  106. Vector<NonnullRefPtr<MediaQuery>> parse_a_media_query_list(TokenStream<T>&);
  107. template<typename T>
  108. RefPtr<Supports> parse_a_supports(TokenStream<T>&);
  109. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<ComponentValue>&);
  110. enum class TopLevel {
  111. No,
  112. Yes
  113. };
  114. template<typename T>
  115. [[nodiscard]] Vector<NonnullRefPtr<Rule>> consume_a_list_of_rules(TokenStream<T>&, TopLevel);
  116. template<typename T>
  117. [[nodiscard]] NonnullRefPtr<Rule> consume_an_at_rule(TokenStream<T>&);
  118. template<typename T>
  119. RefPtr<Rule> consume_a_qualified_rule(TokenStream<T>&);
  120. template<typename T>
  121. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_style_blocks_contents(TokenStream<T>&);
  122. template<typename T>
  123. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  124. template<typename T>
  125. Optional<Declaration> consume_a_declaration(TokenStream<T>&);
  126. template<typename T>
  127. [[nodiscard]] ComponentValue consume_a_component_value(TokenStream<T>&);
  128. template<typename T>
  129. NonnullRefPtr<Block> consume_a_simple_block(TokenStream<T>&);
  130. template<typename T>
  131. NonnullRefPtr<Function> consume_a_function(TokenStream<T>&);
  132. Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<ComponentValue>&);
  133. CSSRule* parse_font_face_rule(TokenStream<ComponentValue>&);
  134. Vector<FontFace::Source> parse_font_face_src(TokenStream<ComponentValue>&);
  135. CSSRule* convert_to_rule(NonnullRefPtr<Rule>);
  136. CSSMediaRule* convert_to_media_rule(NonnullRefPtr<Rule>);
  137. PropertyOwningCSSStyleDeclaration* convert_to_style_declaration(Vector<DeclarationOrAtRule> const& declarations);
  138. Optional<StyleProperty> convert_to_style_property(Declaration const&);
  139. Optional<Dimension> parse_dimension(ComponentValue const&);
  140. Optional<Color> parse_rgb_or_hsl_color(StringView function_name, Vector<ComponentValue> const&);
  141. Optional<Color> parse_color(ComponentValue const&);
  142. Optional<Length> parse_length(ComponentValue const&);
  143. [[nodiscard]] Optional<LengthOrCalculated> parse_source_size_value(ComponentValue const&);
  144. Optional<Ratio> parse_ratio(TokenStream<ComponentValue>&);
  145. Optional<UnicodeRange> parse_unicode_range(TokenStream<ComponentValue>&);
  146. Optional<UnicodeRange> parse_unicode_range(StringView);
  147. Optional<GridSize> parse_grid_size(ComponentValue const&);
  148. Optional<GridMinMax> parse_min_max(Vector<ComponentValue> const&);
  149. Optional<GridRepeat> parse_repeat(Vector<ComponentValue> const&);
  150. Optional<ExplicitGridTrack> parse_track_sizing_function(ComponentValue const&);
  151. Optional<AK::URL> parse_url_function(ComponentValue const&);
  152. RefPtr<StyleValue> parse_url_value(ComponentValue const&);
  153. Optional<Vector<LinearColorStopListElement>> parse_linear_color_stop_list(TokenStream<ComponentValue>&);
  154. Optional<Vector<AngularColorStopListElement>> parse_angular_color_stop_list(TokenStream<ComponentValue>&);
  155. RefPtr<StyleValue> parse_linear_gradient_function(ComponentValue const&);
  156. RefPtr<StyleValue> parse_conic_gradient_function(ComponentValue const&);
  157. RefPtr<StyleValue> parse_radial_gradient_function(ComponentValue const&);
  158. ParseErrorOr<NonnullRefPtr<StyleValue>> parse_css_value(PropertyID, TokenStream<ComponentValue>&);
  159. RefPtr<StyleValue> parse_css_value_for_property(PropertyID, TokenStream<ComponentValue>&);
  160. struct PropertyAndValue {
  161. PropertyID property;
  162. RefPtr<StyleValue> style_value;
  163. };
  164. Optional<PropertyAndValue> parse_css_value_for_properties(ReadonlySpan<PropertyID>, TokenStream<ComponentValue>&);
  165. RefPtr<StyleValue> parse_builtin_value(ComponentValue const&);
  166. RefPtr<CalculatedStyleValue> parse_calculated_value(ComponentValue const&);
  167. // NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp)
  168. OwnPtr<CalculationNode> parse_math_function(PropertyID, Function const&);
  169. OwnPtr<CalculationNode> parse_a_calc_function_node(Function const&);
  170. RefPtr<StyleValue> parse_dimension_value(ComponentValue const&);
  171. RefPtr<StyleValue> parse_integer_value(TokenStream<ComponentValue>&);
  172. RefPtr<StyleValue> parse_number_value(TokenStream<ComponentValue>&);
  173. RefPtr<StyleValue> parse_identifier_value(ComponentValue const&);
  174. RefPtr<StyleValue> parse_color_value(ComponentValue const&);
  175. RefPtr<StyleValue> parse_rect_value(ComponentValue const&);
  176. RefPtr<StyleValue> parse_ratio_value(TokenStream<ComponentValue>&);
  177. RefPtr<StyleValue> parse_string_value(ComponentValue const&);
  178. RefPtr<StyleValue> parse_image_value(ComponentValue const&);
  179. RefPtr<StyleValue> parse_paint_value(TokenStream<ComponentValue>&);
  180. RefPtr<PositionStyleValue> parse_position_value(TokenStream<ComponentValue>&);
  181. template<typename ParseFunction>
  182. RefPtr<StyleValue> parse_comma_separated_value_list(Vector<ComponentValue> const&, ParseFunction);
  183. RefPtr<StyleValue> parse_simple_comma_separated_value_list(PropertyID, Vector<ComponentValue> const&);
  184. RefPtr<StyleValue> parse_filter_value_list_value(Vector<ComponentValue> const&);
  185. RefPtr<StyleValue> parse_aspect_ratio_value(Vector<ComponentValue> const&);
  186. RefPtr<StyleValue> parse_background_value(Vector<ComponentValue> const&);
  187. RefPtr<StyleValue> parse_single_background_position_value(TokenStream<ComponentValue>&);
  188. RefPtr<StyleValue> parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>&, PropertyID);
  189. RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<ComponentValue>&);
  190. RefPtr<StyleValue> parse_single_background_size_value(TokenStream<ComponentValue>&);
  191. RefPtr<StyleValue> parse_border_value(PropertyID, Vector<ComponentValue> const&);
  192. RefPtr<StyleValue> parse_border_radius_value(Vector<ComponentValue> const&);
  193. RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<ComponentValue> const&);
  194. RefPtr<StyleValue> parse_content_value(Vector<ComponentValue> const&);
  195. RefPtr<StyleValue> parse_display_value(Vector<ComponentValue> const&);
  196. RefPtr<StyleValue> parse_flex_value(Vector<ComponentValue> const&);
  197. RefPtr<StyleValue> parse_flex_flow_value(Vector<ComponentValue> const&);
  198. RefPtr<StyleValue> parse_font_value(Vector<ComponentValue> const&);
  199. RefPtr<StyleValue> parse_font_family_value(TokenStream<ComponentValue>&);
  200. RefPtr<StyleValue> parse_list_style_value(Vector<ComponentValue> const&);
  201. RefPtr<StyleValue> parse_math_depth_value(Vector<ComponentValue> const&);
  202. RefPtr<StyleValue> parse_overflow_value(Vector<ComponentValue> const&);
  203. RefPtr<StyleValue> parse_place_content_value(Vector<ComponentValue> const&);
  204. RefPtr<StyleValue> parse_place_items_value(Vector<ComponentValue> const&);
  205. RefPtr<StyleValue> parse_place_self_value(Vector<ComponentValue> const&);
  206. RefPtr<StyleValue> parse_quotes_value(Vector<ComponentValue> const&);
  207. enum class AllowInsetKeyword {
  208. No,
  209. Yes,
  210. };
  211. RefPtr<StyleValue> parse_shadow_value(Vector<ComponentValue> const&, AllowInsetKeyword);
  212. RefPtr<StyleValue> parse_single_shadow_value(TokenStream<ComponentValue>&, AllowInsetKeyword);
  213. RefPtr<StyleValue> parse_text_decoration_value(Vector<ComponentValue> const&);
  214. RefPtr<StyleValue> parse_text_decoration_line_value(TokenStream<ComponentValue>&);
  215. RefPtr<StyleValue> parse_easing_value(TokenStream<ComponentValue>&);
  216. RefPtr<StyleValue> parse_transform_value(Vector<ComponentValue> const&);
  217. RefPtr<StyleValue> parse_transform_origin_value(Vector<ComponentValue> const&);
  218. RefPtr<StyleValue> parse_grid_track_size_list(Vector<ComponentValue> const&, bool allow_separate_line_name_blocks = false);
  219. RefPtr<StyleValue> parse_grid_auto_track_sizes(Vector<ComponentValue> const&);
  220. [[nodiscard]] RefPtr<GridAutoFlowStyleValue> parse_grid_auto_flow_value(Vector<ComponentValue> const&);
  221. RefPtr<StyleValue> parse_grid_track_size_list_shorthand_value(PropertyID, Vector<ComponentValue> const&);
  222. RefPtr<StyleValue> parse_grid_track_placement(Vector<ComponentValue> const&);
  223. RefPtr<StyleValue> parse_grid_track_placement_shorthand_value(PropertyID, Vector<ComponentValue> const&);
  224. RefPtr<StyleValue> parse_grid_template_areas_value(Vector<ComponentValue> const&);
  225. RefPtr<StyleValue> parse_grid_area_shorthand_value(Vector<ComponentValue> const&);
  226. RefPtr<StyleValue> parse_grid_shorthand_value(Vector<ComponentValue> const&);
  227. OwnPtr<CalculationNode> parse_a_calculation(Vector<ComponentValue> const&);
  228. ParseErrorOr<NonnullRefPtr<Selector>> parse_complex_selector(TokenStream<ComponentValue>&, SelectorType);
  229. ParseErrorOr<Optional<Selector::CompoundSelector>> parse_compound_selector(TokenStream<ComponentValue>&);
  230. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<ComponentValue>&);
  231. enum class AllowWildcardName {
  232. No,
  233. Yes,
  234. };
  235. Optional<Selector::SimpleSelector::QualifiedName> parse_selector_qualified_name(TokenStream<ComponentValue>&, AllowWildcardName);
  236. ParseErrorOr<Selector::SimpleSelector> parse_attribute_simple_selector(ComponentValue const&);
  237. ParseErrorOr<Selector::SimpleSelector> parse_pseudo_simple_selector(TokenStream<ComponentValue>&);
  238. ParseErrorOr<Optional<Selector::SimpleSelector>> parse_simple_selector(TokenStream<ComponentValue>&);
  239. NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<ComponentValue>&);
  240. OwnPtr<MediaCondition> parse_media_condition(TokenStream<ComponentValue>&, MediaCondition::AllowOr allow_or);
  241. Optional<MediaFeature> parse_media_feature(TokenStream<ComponentValue>&);
  242. Optional<MediaQuery::MediaType> parse_media_type(TokenStream<ComponentValue>&);
  243. OwnPtr<MediaCondition> parse_media_in_parens(TokenStream<ComponentValue>&);
  244. Optional<MediaFeatureValue> parse_media_feature_value(MediaFeatureID, TokenStream<ComponentValue>&);
  245. OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<ComponentValue>&);
  246. Optional<Supports::InParens> parse_supports_in_parens(TokenStream<ComponentValue>&);
  247. Optional<Supports::Feature> parse_supports_feature(TokenStream<ComponentValue>&);
  248. NonnullRefPtr<StyleValue> resolve_unresolved_style_value(DOM::Element&, Optional<Selector::PseudoElement>, PropertyID, UnresolvedStyleValue const&);
  249. bool expand_variables(DOM::Element&, Optional<Selector::PseudoElement>, StringView property_name, HashMap<FlyString, NonnullRefPtr<PropertyDependencyNode>>& dependencies, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
  250. bool expand_unresolved_values(DOM::Element&, StringView property_name, TokenStream<ComponentValue>& source, Vector<ComponentValue>& dest);
  251. bool substitute_attr_function(DOM::Element& element, StringView property_name, Function const& attr_function, Vector<ComponentValue>& dest);
  252. static bool has_ignored_vendor_prefix(StringView);
  253. struct PropertiesAndCustomProperties {
  254. Vector<StyleProperty> properties;
  255. HashMap<FlyString, StyleProperty> custom_properties;
  256. };
  257. PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&);
  258. ParsingContext m_context;
  259. Vector<Token> m_tokens;
  260. TokenStream<Token> m_token_stream;
  261. };
  262. }
  263. namespace Web {
  264. CSS::CSSStyleSheet* parse_css_stylesheet(CSS::Parser::ParsingContext const&, StringView, Optional<AK::URL> location = {});
  265. CSS::ElementInlineCSSStyleDeclaration* parse_css_style_attribute(CSS::Parser::ParsingContext const&, StringView, DOM::Element&);
  266. RefPtr<CSS::StyleValue> parse_css_value(CSS::Parser::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  267. Optional<CSS::SelectorList> parse_selector(CSS::Parser::ParsingContext const&, StringView);
  268. CSS::CSSRule* parse_css_rule(CSS::Parser::ParsingContext const&, StringView);
  269. RefPtr<CSS::MediaQuery> parse_media_query(CSS::Parser::ParsingContext const&, StringView);
  270. Vector<NonnullRefPtr<CSS::MediaQuery>> parse_media_query_list(CSS::Parser::ParsingContext const&, StringView);
  271. RefPtr<CSS::Supports> parse_css_supports(CSS::Parser::ParsingContext const&, StringView);
  272. Optional<CSS::StyleProperty> parse_css_supports_condition(CSS::Parser::ParsingContext const&, StringView);
  273. }