Parser.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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();
  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. template<typename T>
  64. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet(TokenStream<T>&);
  65. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  66. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  67. template<typename T>
  68. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules(TokenStream<T>&);
  69. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  70. RefPtr<CSSRule> parse_as_rule();
  71. template<typename T>
  72. RefPtr<CSSRule> parse_as_rule(TokenStream<T>&);
  73. // Used in @supports conditions. [CSS3-CONDITIONAL]
  74. Optional<StyleProperty> parse_as_declaration();
  75. template<typename T>
  76. Optional<StyleProperty> parse_as_declaration(TokenStream<T>&);
  77. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  78. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations();
  79. template<typename T>
  80. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations(TokenStream<T>&);
  81. // For things that need to consume a single value, like the parsing rules for attr().
  82. Optional<StyleComponentValueRule> parse_as_component_value();
  83. template<typename T>
  84. Optional<StyleComponentValueRule> parse_as_component_value(TokenStream<T>&);
  85. // 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.
  86. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  87. template<typename T>
  88. Vector<StyleComponentValueRule> parse_as_list_of_component_values(TokenStream<T>&);
  89. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  90. template<typename T>
  91. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values(TokenStream<T>&);
  92. Optional<Selector::SimpleSelector::NthChildPattern> parse_nth_child_pattern(TokenStream<StyleComponentValueRule>&);
  93. // FIXME: https://www.w3.org/TR/selectors-4/
  94. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  95. Optional<SelectorList> parse_a_selector();
  96. template<typename T>
  97. Optional<SelectorList> parse_a_selector(TokenStream<T>&);
  98. Optional<SelectorList> parse_a_relative_selector();
  99. template<typename T>
  100. Optional<SelectorList> parse_a_relative_selector(TokenStream<T>&);
  101. RefPtr<StyleValue> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  102. static RefPtr<StyleValue> parse_css_value(ParsingContext const&, PropertyID, StyleComponentValueRule const&);
  103. private:
  104. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(bool top_level);
  105. template<typename T>
  106. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, bool top_level);
  107. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule();
  108. template<typename T>
  109. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  110. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule();
  111. template<typename T>
  112. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  113. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  114. template<typename T>
  115. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  116. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration();
  117. template<typename T>
  118. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  119. [[nodiscard]] StyleComponentValueRule consume_a_component_value();
  120. template<typename T>
  121. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  122. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
  123. template<typename T>
  124. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  125. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function();
  126. template<typename T>
  127. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  128. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  129. [[nodiscard]] RefPtr<CSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
  130. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&);
  131. static Optional<float> try_parse_float(StringView string);
  132. static Optional<URL> parse_url_function(ParsingContext const&, StyleComponentValueRule const&);
  133. static RefPtr<StyleValue> parse_keyword_or_custom_value(ParsingContext const&, StyleComponentValueRule const&);
  134. static RefPtr<StyleValue> parse_length_value(ParsingContext const&, StyleComponentValueRule const&);
  135. static RefPtr<StyleValue> parse_numeric_value(ParsingContext const&, StyleComponentValueRule const&);
  136. static RefPtr<StyleValue> parse_identifier_value(ParsingContext const&, StyleComponentValueRule const&);
  137. static RefPtr<StyleValue> parse_color_value(ParsingContext const&, StyleComponentValueRule const&);
  138. static RefPtr<StyleValue> parse_string_value(ParsingContext const&, StyleComponentValueRule const&);
  139. static RefPtr<StyleValue> parse_image_value(ParsingContext const&, StyleComponentValueRule const&);
  140. template<typename T>
  141. Optional<SelectorList> parse_a_selector_list(TokenStream<T>&);
  142. template<typename T>
  143. Optional<SelectorList> parse_a_relative_selector_list(TokenStream<T>&);
  144. enum class SelectorParsingResult {
  145. Done,
  146. SyntaxError,
  147. };
  148. RefPtr<Selector> parse_complex_selector(TokenStream<StyleComponentValueRule>&, bool allow_starting_combinator);
  149. Result<Selector::CompoundSelector, SelectorParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  150. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  151. Result<Selector::SimpleSelector, SelectorParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  152. ParsingContext m_context;
  153. Tokenizer m_tokenizer;
  154. Vector<Token> m_tokens;
  155. TokenStream<Token> m_token_stream;
  156. };
  157. }