Parser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/Ratio.h>
  24. #include <LibWeb/CSS/Selector.h>
  25. #include <LibWeb/CSS/StyleValue.h>
  26. #include <LibWeb/CSS/Supports.h>
  27. namespace Web::CSS {
  28. class CSSStyleSheet;
  29. class CSSRule;
  30. class CSSStyleRule;
  31. struct StyleProperty;
  32. enum class PropertyID;
  33. class ParsingContext {
  34. public:
  35. ParsingContext() = default;
  36. explicit ParsingContext(DOM::Document const&);
  37. explicit ParsingContext(DOM::Document const&, Optional<AK::URL> const);
  38. explicit ParsingContext(DOM::ParentNode&);
  39. bool in_quirks_mode() const;
  40. DOM::Document const* document() const { return m_document; }
  41. AK::URL complete_url(String const&) const;
  42. PropertyID current_property_id() const { return m_current_property_id; }
  43. void set_current_property_id(PropertyID property_id) { m_current_property_id = property_id; }
  44. private:
  45. DOM::Document const* m_document { nullptr };
  46. PropertyID m_current_property_id { PropertyID::Invalid };
  47. Optional<AK::URL> m_url;
  48. };
  49. template<typename T>
  50. class TokenStream {
  51. public:
  52. explicit TokenStream(Vector<T> const&);
  53. ~TokenStream() = default;
  54. TokenStream(TokenStream<T> const&) = delete;
  55. bool has_next_token();
  56. T const& next_token();
  57. T const& peek_token(int offset = 0);
  58. T const& current_token();
  59. void reconsume_current_input_token();
  60. int position() const { return m_iterator_offset; }
  61. void rewind_to_position(int);
  62. void skip_whitespace();
  63. void dump_all_tokens();
  64. private:
  65. Vector<T> const& m_tokens;
  66. int m_iterator_offset { -1 };
  67. T make_eof();
  68. T m_eof;
  69. };
  70. class Parser {
  71. public:
  72. Parser(ParsingContext const&, StringView input, String const& encoding = "utf-8");
  73. ~Parser() = default;
  74. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  75. NonnullRefPtr<CSSStyleSheet> parse_as_css_stylesheet(Optional<AK::URL> location);
  76. RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
  77. RefPtr<CSSRule> parse_as_css_rule();
  78. Optional<StyleProperty> parse_as_supports_condition();
  79. enum class SelectorParsingMode {
  80. Standard,
  81. // `<forgiving-selector-list>` and `<forgiving-relative-selector-list>`
  82. // are handled with this parameter, not as separate functions.
  83. // https://drafts.csswg.org/selectors/#forgiving-selector
  84. Forgiving
  85. };
  86. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  87. Optional<SelectorList> parse_as_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  88. Optional<SelectorList> parse_as_relative_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  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. // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets.
  101. struct ParsedStyleSheet {
  102. Optional<AK::URL> location;
  103. NonnullRefPtrVector<StyleRule> rules;
  104. };
  105. template<typename T>
  106. ParsedStyleSheet parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
  107. // "Parse a list of rules" is intended for the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  108. template<typename T>
  109. NonnullRefPtrVector<StyleRule> parse_a_list_of_rules(TokenStream<T>&);
  110. // "Parse a rule" is intended for use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  111. template<typename T>
  112. RefPtr<StyleRule> parse_a_rule(TokenStream<T>&);
  113. // "Parse a declaration" is used in @supports conditions. [CSS3-CONDITIONAL]
  114. template<typename T>
  115. Optional<StyleDeclarationRule> parse_a_declaration(TokenStream<T>&);
  116. // "Parse a list of declarations" is for the contents of a style attribute, which parses text into the contents of a single style rule.
  117. template<typename T>
  118. Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&);
  119. // "Parse a component value" is for things that need to consume a single value, like the parsing rules for attr().
  120. template<typename T>
  121. Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
  122. // "Parse a list of component values" is 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.
  123. template<typename T>
  124. Vector<StyleComponentValueRule> parse_a_list_of_component_values(TokenStream<T>&);
  125. template<typename T>
  126. Vector<Vector<StyleComponentValueRule>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  127. enum class SelectorType {
  128. Standalone,
  129. Relative
  130. };
  131. template<typename T>
  132. Result<SelectorList, ParsingResult> parse_a_selector_list(TokenStream<T>&, SelectorType, SelectorParsingMode = SelectorParsingMode::Standard);
  133. template<typename T>
  134. NonnullRefPtrVector<MediaQuery> parse_a_media_query_list(TokenStream<T>&);
  135. template<typename T>
  136. RefPtr<Supports> parse_a_supports(TokenStream<T>&);
  137. enum class AllowTrailingTokens {
  138. No,
  139. Yes
  140. };
  141. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<StyleComponentValueRule>&, AllowTrailingTokens = AllowTrailingTokens::No);
  142. enum class TopLevel {
  143. No,
  144. Yes
  145. };
  146. template<typename T>
  147. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, TopLevel);
  148. template<typename T>
  149. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  150. template<typename T>
  151. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  152. template<typename T>
  153. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  154. template<typename T>
  155. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  156. template<typename T>
  157. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  158. template<typename T>
  159. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  160. template<typename T>
  161. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  162. [[nodiscard]] Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<StyleComponentValueRule>&);
  163. RefPtr<CSSRule> parse_font_face_rule(TokenStream<StyleComponentValueRule>&);
  164. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  165. [[nodiscard]] RefPtr<PropertyOwningCSSStyleDeclaration> convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations);
  166. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule const&);
  167. class Dimension {
  168. public:
  169. Dimension(Angle&& value)
  170. : m_value(move(value))
  171. {
  172. }
  173. Dimension(Frequency&& value)
  174. : m_value(move(value))
  175. {
  176. }
  177. Dimension(Length&& value)
  178. : m_value(move(value))
  179. {
  180. }
  181. Dimension(Percentage&& value)
  182. : m_value(move(value))
  183. {
  184. }
  185. Dimension(Resolution&& value)
  186. : m_value(move(value))
  187. {
  188. }
  189. Dimension(Time&& value)
  190. : m_value(move(value))
  191. {
  192. }
  193. bool is_angle() const;
  194. Angle angle() const;
  195. bool is_angle_percentage() const;
  196. AnglePercentage angle_percentage() const;
  197. bool is_frequency() const;
  198. Frequency frequency() const;
  199. bool is_frequency_percentage() const;
  200. FrequencyPercentage frequency_percentage() const;
  201. bool is_length() const;
  202. Length length() const;
  203. bool is_length_percentage() const;
  204. LengthPercentage length_percentage() const;
  205. bool is_percentage() const;
  206. Percentage percentage() const;
  207. bool is_resolution() const;
  208. Resolution resolution() const;
  209. bool is_time() const;
  210. Time time() const;
  211. bool is_time_percentage() const;
  212. TimePercentage time_percentage() const;
  213. private:
  214. Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value;
  215. };
  216. Optional<Dimension> parse_dimension(StyleComponentValueRule const&);
  217. Optional<Color> parse_color(StyleComponentValueRule const&);
  218. Optional<Length> parse_length(StyleComponentValueRule const&);
  219. Optional<Ratio> parse_ratio(TokenStream<StyleComponentValueRule>&);
  220. enum class AllowedDataUrlType {
  221. None,
  222. Image,
  223. };
  224. Optional<AK::URL> parse_url_function(StyleComponentValueRule const&, AllowedDataUrlType = AllowedDataUrlType::None);
  225. Result<NonnullRefPtr<StyleValue>, ParsingResult> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  226. RefPtr<StyleValue> parse_css_value(StyleComponentValueRule const&);
  227. RefPtr<StyleValue> parse_builtin_value(StyleComponentValueRule const&);
  228. RefPtr<StyleValue> parse_dynamic_value(StyleComponentValueRule const&);
  229. RefPtr<StyleValue> parse_calculated_value(Vector<StyleComponentValueRule> const&);
  230. RefPtr<StyleValue> parse_dimension_value(StyleComponentValueRule const&);
  231. RefPtr<StyleValue> parse_numeric_value(StyleComponentValueRule const&);
  232. RefPtr<StyleValue> parse_identifier_value(StyleComponentValueRule const&);
  233. RefPtr<StyleValue> parse_color_value(StyleComponentValueRule const&);
  234. RefPtr<StyleValue> parse_string_value(StyleComponentValueRule const&);
  235. RefPtr<StyleValue> parse_image_value(StyleComponentValueRule const&);
  236. template<typename ParseFunction>
  237. RefPtr<StyleValue> parse_comma_separated_value_list(Vector<StyleComponentValueRule> const&, ParseFunction);
  238. RefPtr<StyleValue> parse_simple_comma_separated_value_list(Vector<StyleComponentValueRule> const&);
  239. RefPtr<StyleValue> parse_background_value(Vector<StyleComponentValueRule> const&);
  240. RefPtr<StyleValue> parse_single_background_position_value(TokenStream<StyleComponentValueRule>&);
  241. RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<StyleComponentValueRule>&);
  242. RefPtr<StyleValue> parse_single_background_size_value(TokenStream<StyleComponentValueRule>&);
  243. RefPtr<StyleValue> parse_border_value(Vector<StyleComponentValueRule> const&);
  244. RefPtr<StyleValue> parse_border_radius_value(Vector<StyleComponentValueRule> const&);
  245. RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<StyleComponentValueRule> const&);
  246. RefPtr<StyleValue> parse_content_value(Vector<StyleComponentValueRule> const&);
  247. RefPtr<StyleValue> parse_flex_value(Vector<StyleComponentValueRule> const&);
  248. RefPtr<StyleValue> parse_flex_flow_value(Vector<StyleComponentValueRule> const&);
  249. RefPtr<StyleValue> parse_font_value(Vector<StyleComponentValueRule> const&);
  250. RefPtr<StyleValue> parse_font_family_value(Vector<StyleComponentValueRule> const&, size_t start_index = 0);
  251. RefPtr<StyleValue> parse_list_style_value(Vector<StyleComponentValueRule> const&);
  252. RefPtr<StyleValue> parse_overflow_value(Vector<StyleComponentValueRule> const&);
  253. enum class AllowInsetKeyword {
  254. No,
  255. Yes,
  256. };
  257. RefPtr<StyleValue> parse_shadow_value(Vector<StyleComponentValueRule> const&, AllowInsetKeyword);
  258. RefPtr<StyleValue> parse_single_shadow_value(TokenStream<StyleComponentValueRule>&, AllowInsetKeyword);
  259. RefPtr<StyleValue> parse_text_decoration_value(Vector<StyleComponentValueRule> const&);
  260. RefPtr<StyleValue> parse_transform_value(Vector<StyleComponentValueRule> const&);
  261. RefPtr<StyleValue> parse_transform_origin_value(Vector<StyleComponentValueRule> const&);
  262. // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
  263. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(TokenStream<StyleComponentValueRule>&);
  264. OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(TokenStream<StyleComponentValueRule>&);
  265. Optional<CalculatedStyleValue::CalcValue> parse_calc_value(TokenStream<StyleComponentValueRule>&);
  266. OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(TokenStream<StyleComponentValueRule>&);
  267. OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(TokenStream<StyleComponentValueRule>&);
  268. Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(TokenStream<StyleComponentValueRule>&);
  269. OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(TokenStream<StyleComponentValueRule>&);
  270. OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  271. OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(TokenStream<StyleComponentValueRule>& tokens);
  272. OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  273. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(Vector<StyleComponentValueRule> const&);
  274. Result<NonnullRefPtr<Selector>, ParsingResult> parse_complex_selector(TokenStream<StyleComponentValueRule>&, SelectorType);
  275. Result<Selector::CompoundSelector, ParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  276. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  277. Result<Selector::SimpleSelector, ParsingResult> parse_attribute_simple_selector(StyleComponentValueRule const&);
  278. Result<Selector::SimpleSelector, ParsingResult> parse_pseudo_simple_selector(TokenStream<StyleComponentValueRule>&);
  279. Result<Selector::SimpleSelector, ParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  280. NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<StyleComponentValueRule>&);
  281. OwnPtr<MediaCondition> parse_media_condition(TokenStream<StyleComponentValueRule>&, MediaCondition::AllowOr allow_or);
  282. Optional<MediaFeature> parse_media_feature(TokenStream<StyleComponentValueRule>&);
  283. Optional<MediaQuery::MediaType> parse_media_type(TokenStream<StyleComponentValueRule>&);
  284. OwnPtr<MediaCondition> parse_media_in_parens(TokenStream<StyleComponentValueRule>&);
  285. Optional<MediaFeatureValue> parse_media_feature_value(MediaFeatureID, TokenStream<StyleComponentValueRule>&);
  286. OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<StyleComponentValueRule>&);
  287. Optional<Supports::InParens> parse_supports_in_parens(TokenStream<StyleComponentValueRule>&);
  288. Optional<Supports::Feature> parse_supports_feature(TokenStream<StyleComponentValueRule>&);
  289. static bool has_ignored_vendor_prefix(StringView);
  290. static bool is_builtin(StringView);
  291. struct PropertiesAndCustomProperties {
  292. Vector<StyleProperty> properties;
  293. HashMap<String, StyleProperty> custom_properties;
  294. };
  295. PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&);
  296. ParsingContext m_context;
  297. Tokenizer m_tokenizer;
  298. Vector<Token> m_tokens;
  299. TokenStream<Token> m_token_stream;
  300. };
  301. }
  302. namespace Web {
  303. RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const&, StringView, Optional<AK::URL> location = {});
  304. RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&);
  305. RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  306. Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);
  307. RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
  308. RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const&, StringView);
  309. NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const&, StringView);
  310. RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const&, StringView);
  311. }