Parser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/NonnullRefPtrVector.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/Result.h>
  12. #include <AK/Vector.h>
  13. #include <LibWeb/CSS/CSSStyleDeclaration.h>
  14. #include <LibWeb/CSS/MediaQuery.h>
  15. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  16. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  17. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  18. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  19. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  20. #include <LibWeb/CSS/Parser/StyleRule.h>
  21. #include <LibWeb/CSS/Parser/Tokenizer.h>
  22. #include <LibWeb/CSS/Selector.h>
  23. #include <LibWeb/CSS/StyleValue.h>
  24. #include <LibWeb/CSS/Supports.h>
  25. namespace Web::CSS {
  26. class CSSStyleSheet;
  27. class CSSRule;
  28. class CSSStyleRule;
  29. struct StyleProperty;
  30. enum class PropertyID;
  31. class ParsingContext {
  32. public:
  33. ParsingContext() = default;
  34. explicit ParsingContext(DOM::Document const&);
  35. explicit ParsingContext(DOM::ParentNode&);
  36. bool in_quirks_mode() const;
  37. DOM::Document const* document() const { return m_document; }
  38. AK::URL complete_url(String const&) const;
  39. PropertyID current_property_id() const { return m_current_property_id; }
  40. void set_current_property_id(PropertyID property_id) { m_current_property_id = property_id; }
  41. private:
  42. DOM::Document const* m_document { nullptr };
  43. PropertyID m_current_property_id { PropertyID::Invalid };
  44. };
  45. template<typename T>
  46. class TokenStream {
  47. public:
  48. explicit TokenStream(Vector<T> const&);
  49. ~TokenStream();
  50. bool has_next_token();
  51. T const& next_token();
  52. T const& peek_token(int offset = 0);
  53. T const& current_token();
  54. void reconsume_current_input_token();
  55. int position() const { return m_iterator_offset; }
  56. void rewind_to_position(int);
  57. void skip_whitespace();
  58. void dump_all_tokens();
  59. private:
  60. Vector<T> const& m_tokens;
  61. int m_iterator_offset { -1 };
  62. T make_eof();
  63. T m_eof;
  64. };
  65. class Parser {
  66. public:
  67. Parser(ParsingContext const&, StringView const& input, String const& encoding = "utf-8");
  68. ~Parser();
  69. // The normal parser entry point, for parsing stylesheets.
  70. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  71. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  72. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  73. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  74. RefPtr<CSSRule> parse_as_rule();
  75. // Used in @supports conditions. [CSS3-CONDITIONAL]
  76. Optional<StyleProperty> parse_as_declaration();
  77. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  78. RefPtr<PropertyOwningCSSStyleDeclaration> parse_as_list_of_declarations();
  79. // For things that need to consume a single value, like the parsing rules for attr().
  80. Optional<StyleComponentValueRule> parse_as_component_value();
  81. // 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.
  82. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  83. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  84. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  85. Optional<SelectorList> parse_as_selector();
  86. Optional<SelectorList> parse_as_relative_selector();
  87. NonnullRefPtrVector<MediaQuery> parse_as_media_query_list();
  88. RefPtr<MediaQuery> parse_as_media_query();
  89. RefPtr<Supports> parse_as_supports();
  90. RefPtr<StyleValue> parse_as_css_value(PropertyID);
  91. // FIXME: This is a hack, while CSS::Supports is using a StyleDeclarationRule
  92. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule const&);
  93. private:
  94. enum class ParsingResult {
  95. Done,
  96. IncludesIgnoredVendorPrefix,
  97. SyntaxError,
  98. };
  99. template<typename T>
  100. NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&);
  101. template<typename T>
  102. NonnullRefPtrVector<CSSRule> parse_a_list_of_rules(TokenStream<T>&);
  103. template<typename T>
  104. RefPtr<CSSRule> parse_a_rule(TokenStream<T>&);
  105. template<typename T>
  106. Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
  107. template<typename T>
  108. RefPtr<PropertyOwningCSSStyleDeclaration> parse_a_list_of_declarations(TokenStream<T>&);
  109. template<typename T>
  110. Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
  111. template<typename T>
  112. Vector<StyleComponentValueRule> parse_a_list_of_component_values(TokenStream<T>&);
  113. template<typename T>
  114. Vector<Vector<StyleComponentValueRule>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  115. template<typename T>
  116. Result<SelectorList, ParsingResult> parse_a_selector(TokenStream<T>&);
  117. template<typename T>
  118. Result<SelectorList, ParsingResult> parse_a_relative_selector(TokenStream<T>&);
  119. template<typename T>
  120. Result<SelectorList, ParsingResult> parse_a_selector_list(TokenStream<T>&);
  121. template<typename T>
  122. Result<SelectorList, ParsingResult> parse_a_relative_selector_list(TokenStream<T>&);
  123. template<typename T>
  124. NonnullRefPtrVector<MediaQuery> parse_a_media_query_list(TokenStream<T>&);
  125. template<typename T>
  126. RefPtr<Supports> parse_a_supports(TokenStream<T>&);
  127. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<StyleComponentValueRule>&);
  128. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(bool top_level);
  129. template<typename T>
  130. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, bool top_level);
  131. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule();
  132. template<typename T>
  133. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  134. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule();
  135. template<typename T>
  136. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  137. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  138. template<typename T>
  139. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  140. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration();
  141. template<typename T>
  142. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  143. [[nodiscard]] StyleComponentValueRule consume_a_component_value();
  144. template<typename T>
  145. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  146. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
  147. template<typename T>
  148. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  149. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function();
  150. template<typename T>
  151. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  152. struct GeneralEnclosed {
  153. };
  154. [[nodiscard]] Optional<GeneralEnclosed> parse_general_enclosed();
  155. template<typename T>
  156. [[nodiscard]] Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<T>&);
  157. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  158. [[nodiscard]] RefPtr<PropertyOwningCSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
  159. static Optional<float> try_parse_float(StringView string);
  160. static Optional<Color> parse_color(ParsingContext const&, StyleComponentValueRule const&);
  161. static Optional<Length> parse_length(ParsingContext const&, StyleComponentValueRule const&);
  162. enum class AllowedDataUrlType {
  163. None,
  164. Image,
  165. };
  166. static Optional<AK::URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&, AllowedDataUrlType = AllowedDataUrlType::None);
  167. Result<NonnullRefPtr<StyleValue>, ParsingResult> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  168. static RefPtr<StyleValue> parse_css_value(ParsingContext const&, StyleComponentValueRule const&);
  169. static RefPtr<StyleValue> parse_builtin_value(ParsingContext const&, StyleComponentValueRule const&);
  170. static RefPtr<StyleValue> parse_dynamic_value(ParsingContext const&, StyleComponentValueRule const&);
  171. static RefPtr<StyleValue> parse_length_value(ParsingContext const&, StyleComponentValueRule const&);
  172. static RefPtr<StyleValue> parse_numeric_value(ParsingContext const&, StyleComponentValueRule const&);
  173. static RefPtr<StyleValue> parse_identifier_value(ParsingContext const&, StyleComponentValueRule const&);
  174. static RefPtr<StyleValue> parse_color_value(ParsingContext const&, StyleComponentValueRule const&);
  175. static RefPtr<StyleValue> parse_string_value(ParsingContext const&, StyleComponentValueRule const&);
  176. static RefPtr<StyleValue> parse_image_value(ParsingContext const&, StyleComponentValueRule const&);
  177. static RefPtr<StyleValue> parse_background_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  178. static RefPtr<StyleValue> parse_background_image_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  179. static RefPtr<StyleValue> parse_single_background_position_value(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  180. static RefPtr<StyleValue> parse_background_position_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  181. static RefPtr<StyleValue> parse_background_repeat_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  182. static RefPtr<StyleValue> parse_border_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  183. static RefPtr<StyleValue> parse_border_radius_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  184. static RefPtr<StyleValue> parse_border_radius_shorthand_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  185. static RefPtr<StyleValue> parse_box_shadow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  186. static RefPtr<StyleValue> parse_flex_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  187. static RefPtr<StyleValue> parse_flex_flow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  188. static RefPtr<StyleValue> parse_font_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  189. static RefPtr<StyleValue> parse_font_family_value(ParsingContext const&, Vector<StyleComponentValueRule> const&, size_t start_index = 0);
  190. static RefPtr<StyleValue> parse_list_style_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  191. static RefPtr<StyleValue> parse_overflow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  192. static RefPtr<StyleValue> parse_text_decoration_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  193. static RefPtr<StyleValue> parse_transform_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  194. // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
  195. static OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  196. static OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  197. static Optional<CalculatedStyleValue::CalcValue> parse_calc_value(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  198. static OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  199. static OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  200. static Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  201. static OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  202. static OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  203. static OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>& tokens);
  204. static OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  205. static OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  206. Result<NonnullRefPtr<Selector>, ParsingResult> parse_complex_selector(TokenStream<StyleComponentValueRule>&, bool allow_starting_combinator);
  207. Result<Selector::CompoundSelector, ParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  208. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  209. Result<Selector::SimpleSelector, ParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  210. NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<StyleComponentValueRule>&);
  211. OwnPtr<MediaQuery::MediaCondition> consume_media_condition(TokenStream<StyleComponentValueRule>&);
  212. Optional<MediaQuery::MediaFeature> consume_media_feature(TokenStream<StyleComponentValueRule>&);
  213. Optional<MediaQuery::MediaType> consume_media_type(TokenStream<StyleComponentValueRule>&);
  214. OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<StyleComponentValueRule>&);
  215. Optional<Supports::InParens> parse_supports_in_parens(TokenStream<StyleComponentValueRule>&);
  216. Optional<Supports::Feature> parse_supports_feature(TokenStream<StyleComponentValueRule>&);
  217. static bool has_ignored_vendor_prefix(StringView const&);
  218. ParsingContext m_context;
  219. Tokenizer m_tokenizer;
  220. Vector<Token> m_tokens;
  221. TokenStream<Token> m_token_stream;
  222. };
  223. }
  224. namespace Web {
  225. RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView const&);
  226. RefPtr<CSS::PropertyOwningCSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const&, StringView const&);
  227. RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView const&, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  228. Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView const&);
  229. RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
  230. RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const&, StringView const&);
  231. NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const&, StringView const&);
  232. RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const&, StringView const&);
  233. RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const&, StringView const&);
  234. }