Parser.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/Vector.h>
  12. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  13. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  14. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  15. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  16. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  17. #include <LibWeb/CSS/Parser/StyleRule.h>
  18. #include <LibWeb/CSS/Parser/Tokenizer.h>
  19. #include <LibWeb/CSS/Selector.h>
  20. namespace Web::CSS {
  21. class CSSStyleSheet;
  22. class CSSRule;
  23. class CSSStyleRule;
  24. struct StyleProperty;
  25. class StyleValue;
  26. enum class PropertyID;
  27. class ParsingContext {
  28. public:
  29. ParsingContext();
  30. explicit ParsingContext(DOM::Document const&);
  31. explicit ParsingContext(DOM::ParentNode const&);
  32. bool in_quirks_mode() const;
  33. URL complete_url(String const&) const;
  34. private:
  35. const DOM::Document* m_document { nullptr };
  36. };
  37. template<typename T>
  38. class TokenStream {
  39. public:
  40. explicit TokenStream(Vector<T> const&);
  41. ~TokenStream();
  42. bool has_next_token();
  43. T const& next_token();
  44. T const& peek_token();
  45. T const& current_token();
  46. void reconsume_current_input_token();
  47. void skip_whitespace();
  48. void dump_all_tokens();
  49. private:
  50. Vector<T> const& m_tokens;
  51. int m_iterator_offset { -1 };
  52. T make_eof();
  53. T m_eof;
  54. };
  55. class Parser {
  56. public:
  57. Parser(ParsingContext const&, StringView const& input, String const& encoding = "utf-8");
  58. ~Parser();
  59. // The normal parser entry point, for parsing stylesheets.
  60. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  61. template<typename T>
  62. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet(TokenStream<T>&);
  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. template<typename T>
  66. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules(TokenStream<T>&);
  67. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  68. RefPtr<CSSRule> parse_as_rule();
  69. template<typename T>
  70. RefPtr<CSSRule> parse_as_rule(TokenStream<T>&);
  71. // Used in @supports conditions. [CSS3-CONDITIONAL]
  72. Optional<StyleProperty> parse_as_declaration();
  73. template<typename T>
  74. Optional<StyleProperty> parse_as_declaration(TokenStream<T>&);
  75. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  76. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations();
  77. template<typename T>
  78. RefPtr<CSSStyleDeclaration> parse_as_list_of_declarations(TokenStream<T>&);
  79. // For things that need to consume a single value, like the parsing rules for attr().
  80. Optional<StyleComponentValueRule> parse_as_component_value();
  81. template<typename T>
  82. Optional<StyleComponentValueRule> parse_as_component_value(TokenStream<T>&);
  83. // 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.
  84. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  85. template<typename T>
  86. Vector<StyleComponentValueRule> parse_as_list_of_component_values(TokenStream<T>&);
  87. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  88. template<typename T>
  89. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values(TokenStream<T>&);
  90. template<typename T>
  91. RefPtr<Selector> parse_single_selector(TokenStream<T>&, bool is_relative = false);
  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. NonnullRefPtrVector<Selector> parse_a_selector();
  96. template<typename T>
  97. NonnullRefPtrVector<Selector> parse_a_selector(TokenStream<T>&);
  98. NonnullRefPtrVector<Selector> parse_a_relative_selector();
  99. template<typename T>
  100. NonnullRefPtrVector<Selector> parse_a_relative_selector(TokenStream<T>&);
  101. RefPtr<StyleValue> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  102. // FIXME: https://drafts.csswg.org/css-backgrounds-3/
  103. static Optional<String> as_valid_background_repeat(String input) { return input; }
  104. static Optional<String> as_valid_background_attachment(String input) { return input; }
  105. static Optional<String> as_valid_background_position(String input) { return input; }
  106. static Optional<String> as_valid_background_clip(String input) { return input; }
  107. static Optional<String> as_valid_background_origin(String input) { return input; }
  108. static Optional<String> as_valid_background_size(String input) { return input; }
  109. static Optional<String> as_valid_border_style(String input) { return input; }
  110. static Optional<String> as_valid_border_image_repeat(String input) { return input; }
  111. private:
  112. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(bool top_level);
  113. template<typename T>
  114. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, bool top_level);
  115. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule();
  116. template<typename T>
  117. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  118. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule();
  119. template<typename T>
  120. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  121. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  122. template<typename T>
  123. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  124. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration();
  125. template<typename T>
  126. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  127. [[nodiscard]] StyleComponentValueRule consume_a_component_value();
  128. template<typename T>
  129. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  130. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
  131. template<typename T>
  132. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  133. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function();
  134. template<typename T>
  135. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  136. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  137. [[nodiscard]] RefPtr<CSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
  138. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule&);
  139. static Optional<float> try_parse_float(StringView string);
  140. ParsingContext m_context;
  141. Tokenizer m_tokenizer;
  142. Vector<Token> m_tokens;
  143. TokenStream<Token> m_token_stream;
  144. };
  145. }