Parser.h 13 KB

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