Parser.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/Vector.h>
  10. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  11. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  12. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  13. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  14. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  15. #include <LibWeb/CSS/Parser/StyleRule.h>
  16. #include <LibWeb/CSS/Parser/Tokenizer.h>
  17. #include <LibWeb/CSS/Selector.h>
  18. namespace Web::CSS {
  19. class CSSStyleSheet;
  20. class CSSRule;
  21. class CSSStyleRule;
  22. struct StyleProperty;
  23. class StyleValue;
  24. enum class PropertyID;
  25. class ParsingContext {
  26. public:
  27. ParsingContext();
  28. explicit ParsingContext(DOM::Document const&);
  29. explicit ParsingContext(DOM::ParentNode const&);
  30. bool in_quirks_mode() const;
  31. URL complete_url(String const&) const;
  32. private:
  33. const DOM::Document* m_document { nullptr };
  34. };
  35. template<typename T>
  36. class TokenStream {
  37. public:
  38. explicit TokenStream(Vector<T> const&);
  39. ~TokenStream();
  40. bool has_next_token();
  41. T const& next_token();
  42. T const& peek_token();
  43. T const& current_token();
  44. void reconsume_current_input_token();
  45. void skip_whitespace();
  46. void dump_all_tokens();
  47. private:
  48. Vector<T> const& m_tokens;
  49. int m_iterator_offset { -1 };
  50. T make_eof();
  51. T m_eof;
  52. };
  53. class Parser {
  54. public:
  55. Parser(ParsingContext const&, StringView const& input, String const& encoding = "utf-8");
  56. ~Parser();
  57. // The normal parser entry point, for parsing stylesheets.
  58. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  59. template<typename T>
  60. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet(TokenStream<T>&);
  61. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  62. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  63. template<typename T>
  64. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules(TokenStream<T>&);
  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. template<typename T>
  68. RefPtr<CSSRule> parse_as_rule(TokenStream<T>&);
  69. // Used in @supports conditions. [CSS3-CONDITIONAL]
  70. Optional<StyleProperty> parse_as_declaration();
  71. template<typename T>
  72. Optional<StyleProperty> parse_as_declaration(TokenStream<T>&);
  73. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  74. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations();
  75. template<typename T>
  76. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations(TokenStream<T>&);
  77. // For things that need to consume a single value, like the parsing rules for attr().
  78. Optional<StyleComponentValueRule> parse_as_component_value();
  79. template<typename T>
  80. Optional<StyleComponentValueRule> parse_as_component_value(TokenStream<T>&);
  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. template<typename T>
  84. Vector<StyleComponentValueRule> parse_as_list_of_component_values(TokenStream<T>&);
  85. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  86. template<typename T>
  87. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values(TokenStream<T>&);
  88. template<typename T>
  89. Optional<Selector> parse_single_selector(TokenStream<T>&, bool is_relative = false);
  90. // FIXME: https://www.w3.org/TR/selectors-4/
  91. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  92. Vector<Selector> parse_a_selector();
  93. template<typename T>
  94. Vector<Selector> parse_a_selector(TokenStream<T>&);
  95. Vector<Selector> parse_a_relative_selector();
  96. template<typename T>
  97. Vector<Selector> parse_a_relative_selector(TokenStream<T>&);
  98. RefPtr<StyleValue> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  99. // FIXME: https://drafts.csswg.org/css-backgrounds-3/
  100. static Optional<String> as_valid_background_repeat(String input) { return input; }
  101. static Optional<String> as_valid_background_attachment(String input) { return input; }
  102. static Optional<String> as_valid_background_position(String input) { return input; }
  103. static Optional<String> as_valid_background_clip(String input) { return input; }
  104. static Optional<String> as_valid_background_origin(String input) { return input; }
  105. static Optional<String> as_valid_background_size(String input) { return input; }
  106. static Optional<String> as_valid_border_style(String input) { return input; }
  107. static Optional<String> as_valid_border_image_repeat(String input) { return input; }
  108. private:
  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. static Optional<float> try_parse_float(StringView string);
  136. ParsingContext m_context;
  137. Tokenizer m_tokenizer;
  138. Vector<Token> m_tokens;
  139. TokenStream<Token> m_token_stream;
  140. };
  141. }