Parser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  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/Parser/DeclarationOrAtRule.h>
  14. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  15. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  16. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  17. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  18. #include <LibWeb/CSS/Parser/StyleRule.h>
  19. #include <LibWeb/CSS/Parser/Tokenizer.h>
  20. #include <LibWeb/CSS/Selector.h>
  21. #include <LibWeb/CSS/StyleValue.h>
  22. namespace Web::CSS {
  23. class CSSStyleSheet;
  24. class CSSRule;
  25. class CSSStyleRule;
  26. struct StyleProperty;
  27. enum class PropertyID;
  28. class ParsingContext {
  29. public:
  30. ParsingContext();
  31. explicit ParsingContext(DOM::Document&);
  32. explicit ParsingContext(DOM::ParentNode&);
  33. bool in_quirks_mode() const;
  34. DOM::Document* document() const { return m_document; }
  35. URL complete_url(String const&) const;
  36. private:
  37. DOM::Document* m_document { nullptr };
  38. };
  39. template<typename T>
  40. class TokenStream {
  41. public:
  42. explicit TokenStream(Vector<T> const&);
  43. ~TokenStream();
  44. bool has_next_token();
  45. T const& next_token();
  46. T const& peek_token(int offset = 0);
  47. T const& current_token();
  48. void reconsume_current_input_token();
  49. void skip_whitespace();
  50. void dump_all_tokens();
  51. private:
  52. Vector<T> const& m_tokens;
  53. int m_iterator_offset { -1 };
  54. T make_eof();
  55. T m_eof;
  56. };
  57. class Parser {
  58. public:
  59. Parser(ParsingContext const&, StringView const& input, String const& encoding = "utf-8");
  60. ~Parser();
  61. // The normal parser entry point, for parsing stylesheets.
  62. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  63. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  64. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  65. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  66. RefPtr<CSSRule> parse_as_rule();
  67. // Used in @supports conditions. [CSS3-CONDITIONAL]
  68. Optional<StyleProperty> parse_as_declaration();
  69. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  70. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations();
  71. // For things that need to consume a single value, like the parsing rules for attr().
  72. Optional<StyleComponentValueRule> parse_as_component_value();
  73. // 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.
  74. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  75. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  76. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  77. Optional<SelectorList> parse_as_selector();
  78. Optional<SelectorList> parse_as_relative_selector();
  79. RefPtr<StyleValue> parse_as_css_value(PropertyID);
  80. // FIXME: These want to be private, but StyleResolver still uses them for now.
  81. RefPtr<StyleValue> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  82. static RefPtr<StyleValue> parse_css_value(ParsingContext const&, PropertyID, StyleComponentValueRule const&);
  83. private:
  84. template<typename T>
  85. NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&);
  86. template<typename T>
  87. NonnullRefPtrVector<CSSRule> parse_a_list_of_rules(TokenStream<T>&);
  88. template<typename T>
  89. RefPtr<CSSRule> parse_a_rule(TokenStream<T>&);
  90. template<typename T>
  91. Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
  92. template<typename T>
  93. RefPtr<CSSStyleDeclaration> parse_a_list_of_declarations(TokenStream<T>&);
  94. template<typename T>
  95. Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
  96. template<typename T>
  97. Vector<StyleComponentValueRule> parse_a_list_of_component_values(TokenStream<T>&);
  98. template<typename T>
  99. Vector<Vector<StyleComponentValueRule>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  100. template<typename T>
  101. Optional<SelectorList> parse_a_selector(TokenStream<T>&);
  102. template<typename T>
  103. Optional<SelectorList> parse_a_relative_selector(TokenStream<T>&);
  104. template<typename T>
  105. Optional<SelectorList> parse_a_selector_list(TokenStream<T>&);
  106. template<typename T>
  107. Optional<SelectorList> parse_a_relative_selector_list(TokenStream<T>&);
  108. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<StyleComponentValueRule>&);
  109. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(bool top_level);
  110. template<typename T>
  111. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, bool top_level);
  112. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule();
  113. template<typename T>
  114. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  115. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule();
  116. template<typename T>
  117. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  118. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  119. template<typename T>
  120. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  121. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration();
  122. template<typename T>
  123. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  124. [[nodiscard]] StyleComponentValueRule consume_a_component_value();
  125. template<typename T>
  126. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  127. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
  128. template<typename T>
  129. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  130. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function();
  131. template<typename T>
  132. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  133. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  134. [[nodiscard]] RefPtr<CSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
  135. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&);
  136. static Optional<float> try_parse_float(StringView string);
  137. static Optional<Color> parse_color(ParsingContext const&, StyleComponentValueRule const&);
  138. static Optional<Length> parse_length(ParsingContext const&, StyleComponentValueRule const&);
  139. static Optional<URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&);
  140. static RefPtr<StyleValue> parse_builtin_or_dynamic_value(ParsingContext const&, StyleComponentValueRule const&);
  141. static RefPtr<StyleValue> parse_length_value(ParsingContext const&, StyleComponentValueRule const&);
  142. static RefPtr<StyleValue> parse_numeric_value(ParsingContext const&, StyleComponentValueRule const&);
  143. static RefPtr<StyleValue> parse_identifier_value(ParsingContext const&, StyleComponentValueRule const&);
  144. static RefPtr<StyleValue> parse_color_value(ParsingContext const&, StyleComponentValueRule const&);
  145. static RefPtr<StyleValue> parse_string_value(ParsingContext const&, StyleComponentValueRule const&);
  146. static RefPtr<StyleValue> parse_image_value(ParsingContext const&, StyleComponentValueRule const&);
  147. static RefPtr<StyleValue> parse_box_shadow_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  148. // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
  149. static OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  150. static OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  151. static Optional<CalculatedStyleValue::CalcValue> parse_calc_value(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  152. static OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  153. static OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  154. static Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  155. static OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  156. static OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  157. static OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>& tokens);
  158. static OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(ParsingContext const&, TokenStream<StyleComponentValueRule>&);
  159. static OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(ParsingContext const&, Vector<StyleComponentValueRule> const&);
  160. enum class SelectorParsingResult {
  161. Done,
  162. SyntaxError,
  163. };
  164. RefPtr<Selector> parse_complex_selector(TokenStream<StyleComponentValueRule>&, bool allow_starting_combinator);
  165. Result<Selector::CompoundSelector, SelectorParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  166. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  167. Result<Selector::SimpleSelector, SelectorParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  168. ParsingContext m_context;
  169. Tokenizer m_tokenizer;
  170. Vector<Token> m_tokens;
  171. TokenStream<Token> m_token_stream;
  172. };
  173. }
  174. namespace Web {
  175. RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView const&);
  176. RefPtr<CSS::CSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const&, StringView const&);
  177. RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView const&, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  178. Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView const&);
  179. RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const&, StringView const&);
  180. }