Parser.h 17 KB

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