Parser.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/CSSStyleDeclaration.h>
  14. #include <LibWeb/CSS/GeneralEnclosed.h>
  15. #include <LibWeb/CSS/MediaQuery.h>
  16. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  17. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  18. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  19. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  20. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  21. #include <LibWeb/CSS/Parser/StyleRule.h>
  22. #include <LibWeb/CSS/Parser/Tokenizer.h>
  23. #include <LibWeb/CSS/Selector.h>
  24. #include <LibWeb/CSS/StyleValue.h>
  25. #include <LibWeb/CSS/Supports.h>
  26. namespace Web::CSS {
  27. class CSSStyleSheet;
  28. class CSSRule;
  29. class CSSStyleRule;
  30. struct StyleProperty;
  31. enum class PropertyID;
  32. class ParsingContext {
  33. public:
  34. ParsingContext() = default;
  35. explicit ParsingContext(DOM::Document const&);
  36. explicit ParsingContext(DOM::ParentNode&);
  37. bool in_quirks_mode() const;
  38. DOM::Document const* document() const { return m_document; }
  39. AK::URL complete_url(String const&) const;
  40. PropertyID current_property_id() const { return m_current_property_id; }
  41. void set_current_property_id(PropertyID property_id) { m_current_property_id = property_id; }
  42. private:
  43. DOM::Document const* m_document { nullptr };
  44. PropertyID m_current_property_id { PropertyID::Invalid };
  45. };
  46. template<typename T>
  47. class TokenStream {
  48. public:
  49. explicit TokenStream(Vector<T> const&);
  50. ~TokenStream();
  51. TokenStream(TokenStream<T> const&) = delete;
  52. bool has_next_token();
  53. T const& next_token();
  54. T const& peek_token(int offset = 0);
  55. T const& current_token();
  56. void reconsume_current_input_token();
  57. int position() const { return m_iterator_offset; }
  58. void rewind_to_position(int);
  59. void skip_whitespace();
  60. void dump_all_tokens();
  61. private:
  62. Vector<T> const& m_tokens;
  63. int m_iterator_offset { -1 };
  64. T make_eof();
  65. T m_eof;
  66. };
  67. class Parser {
  68. public:
  69. Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8");
  70. ~Parser();
  71. // The normal parser entry point, for parsing stylesheets.
  72. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  73. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  74. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  75. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  76. RefPtr<CSSRule> parse_as_rule();
  77. // Used in @supports conditions. [CSS3-CONDITIONAL]
  78. Optional<StyleProperty> parse_as_declaration();
  79. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  80. RefPtr<PropertyOwningCSSStyleDeclaration> parse_as_list_of_declarations();
  81. // For things that need to consume a single value, like the parsing rules for attr().
  82. Optional<StyleComponentValueRule> parse_as_component_value();
  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. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  86. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  87. Optional<SelectorList> parse_as_selector();
  88. Optional<SelectorList> parse_as_relative_selector();
  89. NonnullRefPtrVector<MediaQuery> parse_as_media_query_list();
  90. RefPtr<MediaQuery> parse_as_media_query();
  91. RefPtr<Supports> parse_as_supports();
  92. RefPtr<StyleValue> parse_as_css_value(PropertyID);
  93. static RefPtr<StyleValue> parse_css_value(Badge<StyleComputer>, ParsingContext const&, PropertyID, Vector<StyleComponentValueRule> const&);
  94. private:
  95. enum class ParsingResult {
  96. Done,
  97. IncludesIgnoredVendorPrefix,
  98. SyntaxError,
  99. };
  100. template<typename T>
  101. NonnullRefPtr<CSSStyleSheet> parse_a_stylesheet(TokenStream<T>&);
  102. template<typename T>
  103. NonnullRefPtrVector<CSSRule> parse_a_list_of_rules(TokenStream<T>&);
  104. template<typename T>
  105. RefPtr<CSSRule> parse_a_rule(TokenStream<T>&);
  106. template<typename T>
  107. Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
  108. template<typename T>
  109. RefPtr<PropertyOwningCSSStyleDeclaration> parse_a_list_of_declarations(TokenStream<T>&);
  110. template<typename T>
  111. Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
  112. template<typename T>
  113. Vector<StyleComponentValueRule> parse_a_list_of_component_values(TokenStream<T>&);
  114. template<typename T>
  115. Vector<Vector<StyleComponentValueRule>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  116. template<typename T>
  117. Result<SelectorList, ParsingResult> parse_a_selector(TokenStream<T>&);
  118. template<typename T>
  119. Result<SelectorList, ParsingResult> parse_a_relative_selector(TokenStream<T>&);
  120. template<typename T>
  121. Result<SelectorList, ParsingResult> parse_a_selector_list(TokenStream<T>&);
  122. template<typename T>
  123. Result<SelectorList, ParsingResult> parse_a_relative_selector_list(TokenStream<T>&);
  124. template<typename T>
  125. NonnullRefPtrVector<MediaQuery> parse_a_media_query_list(TokenStream<T>&);
  126. template<typename T>
  127. RefPtr<Supports> parse_a_supports(TokenStream<T>&);
  128. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<StyleComponentValueRule>&);
  129. template<typename T>
  130. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, bool top_level);
  131. template<typename T>
  132. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  133. template<typename T>
  134. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  135. template<typename T>
  136. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  137. template<typename T>
  138. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  139. template<typename T>
  140. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  141. template<typename T>
  142. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  143. template<typename T>
  144. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  145. [[nodiscard]] Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<StyleComponentValueRule>&);
  146. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  147. [[nodiscard]] RefPtr<PropertyOwningCSSStyleDeclaration> convert_to_declaration(NonnullRefPtr<StyleBlockRule>);
  148. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule const&);
  149. class Dimension {
  150. public:
  151. Dimension(Length&& value)
  152. : m_value(move(value))
  153. {
  154. }
  155. Dimension(Percentage&& value)
  156. : m_value(move(value))
  157. {
  158. }
  159. bool is_length() const;
  160. Length length() const;
  161. bool is_percentage() const;
  162. Percentage percentage() const;
  163. bool is_length_percentage() const;
  164. LengthPercentage length_percentage() const;
  165. private:
  166. Variant<Length, Percentage> m_value;
  167. };
  168. Optional<Dimension> parse_dimension(StyleComponentValueRule const&);
  169. Optional<Color> parse_color(StyleComponentValueRule const&);
  170. Optional<Length> parse_length(StyleComponentValueRule const&);
  171. enum class AllowedDataUrlType {
  172. None,
  173. Image,
  174. };
  175. Optional<AK::URL> parse_url_function(StyleComponentValueRule const&, AllowedDataUrlType = AllowedDataUrlType::None);
  176. Result<NonnullRefPtr<StyleValue>, ParsingResult> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  177. RefPtr<StyleValue> parse_css_value(StyleComponentValueRule const&);
  178. RefPtr<StyleValue> parse_builtin_value(StyleComponentValueRule const&);
  179. RefPtr<StyleValue> parse_dynamic_value(StyleComponentValueRule const&);
  180. RefPtr<StyleValue> parse_calculated_value(Vector<StyleComponentValueRule> const&);
  181. RefPtr<StyleValue> parse_dimension_value(StyleComponentValueRule const&);
  182. RefPtr<StyleValue> parse_numeric_value(StyleComponentValueRule const&);
  183. RefPtr<StyleValue> parse_identifier_value(StyleComponentValueRule const&);
  184. RefPtr<StyleValue> parse_color_value(StyleComponentValueRule const&);
  185. RefPtr<StyleValue> parse_string_value(StyleComponentValueRule const&);
  186. RefPtr<StyleValue> parse_image_value(StyleComponentValueRule const&);
  187. template<typename ParseFunction>
  188. RefPtr<StyleValue> parse_comma_separated_value_list(Vector<StyleComponentValueRule> const&, ParseFunction);
  189. RefPtr<StyleValue> parse_simple_comma_separated_value_list(Vector<StyleComponentValueRule> const&);
  190. RefPtr<StyleValue> parse_background_value(Vector<StyleComponentValueRule> const&);
  191. RefPtr<StyleValue> parse_single_background_position_value(TokenStream<StyleComponentValueRule>&);
  192. RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<StyleComponentValueRule>&);
  193. RefPtr<StyleValue> parse_single_background_size_value(TokenStream<StyleComponentValueRule>&);
  194. RefPtr<StyleValue> parse_border_value(Vector<StyleComponentValueRule> const&);
  195. RefPtr<StyleValue> parse_border_radius_value(Vector<StyleComponentValueRule> const&);
  196. RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<StyleComponentValueRule> const&);
  197. RefPtr<StyleValue> parse_box_shadow_value(Vector<StyleComponentValueRule> const&);
  198. RefPtr<StyleValue> parse_single_box_shadow_value(TokenStream<StyleComponentValueRule>&);
  199. RefPtr<StyleValue> parse_flex_value(Vector<StyleComponentValueRule> const&);
  200. RefPtr<StyleValue> parse_flex_flow_value(Vector<StyleComponentValueRule> const&);
  201. RefPtr<StyleValue> parse_font_value(Vector<StyleComponentValueRule> const&);
  202. RefPtr<StyleValue> parse_font_family_value(Vector<StyleComponentValueRule> const&, size_t start_index = 0);
  203. RefPtr<StyleValue> parse_list_style_value(Vector<StyleComponentValueRule> const&);
  204. RefPtr<StyleValue> parse_overflow_value(Vector<StyleComponentValueRule> const&);
  205. RefPtr<StyleValue> parse_text_decoration_value(Vector<StyleComponentValueRule> const&);
  206. RefPtr<StyleValue> parse_transform_value(Vector<StyleComponentValueRule> const&);
  207. // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
  208. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(TokenStream<StyleComponentValueRule>&);
  209. OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(TokenStream<StyleComponentValueRule>&);
  210. Optional<CalculatedStyleValue::CalcValue> parse_calc_value(TokenStream<StyleComponentValueRule>&);
  211. OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(TokenStream<StyleComponentValueRule>&);
  212. OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(TokenStream<StyleComponentValueRule>&);
  213. Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(TokenStream<StyleComponentValueRule>&);
  214. OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(TokenStream<StyleComponentValueRule>&);
  215. OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  216. OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(TokenStream<StyleComponentValueRule>& tokens);
  217. OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  218. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(Vector<StyleComponentValueRule> const&);
  219. Result<NonnullRefPtr<Selector>, ParsingResult> parse_complex_selector(TokenStream<StyleComponentValueRule>&, bool allow_starting_combinator);
  220. Result<Selector::CompoundSelector, ParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  221. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  222. Result<Selector::SimpleSelector, ParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  223. NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<StyleComponentValueRule>&);
  224. OwnPtr<MediaCondition> parse_media_condition(TokenStream<StyleComponentValueRule>&, MediaCondition::AllowOr allow_or);
  225. Optional<MediaFeature> parse_media_feature(TokenStream<StyleComponentValueRule>&);
  226. Optional<MediaQuery::MediaType> parse_media_type(TokenStream<StyleComponentValueRule>&);
  227. OwnPtr<MediaCondition> parse_media_in_parens(TokenStream<StyleComponentValueRule>&);
  228. Optional<MediaFeatureValue> parse_media_feature_value(TokenStream<StyleComponentValueRule>&);
  229. OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<StyleComponentValueRule>&);
  230. Optional<Supports::InParens> parse_supports_in_parens(TokenStream<StyleComponentValueRule>&);
  231. Optional<Supports::Feature> parse_supports_feature(TokenStream<StyleComponentValueRule>&);
  232. static bool has_ignored_vendor_prefix(StringView);
  233. ParsingContext m_context;
  234. Tokenizer m_tokenizer;
  235. Vector<Token> m_tokens;
  236. TokenStream<Token> m_token_stream;
  237. };
  238. }
  239. namespace Web {
  240. RefPtr<CSS::CSSStyleSheet> parse_css(CSS::ParsingContext const&, StringView);
  241. RefPtr<CSS::PropertyOwningCSSStyleDeclaration> parse_css_declaration(CSS::ParsingContext const&, StringView);
  242. RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  243. Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);
  244. RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
  245. RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const&, StringView);
  246. NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const&, StringView);
  247. RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const&, StringView);
  248. RefPtr<CSS::StyleValue> parse_html_length(DOM::Document const&, StringView);
  249. }