Parser.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // Used in @supports conditions. [CSS3-CONDITIONAL]
  75. Optional<StyleProperty> parse_as_declaration();
  76. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  77. Vector<DeclarationOrAtRule> parse_as_list_of_declarations();
  78. // For things that need to consume a single value, like the parsing rules for attr().
  79. Optional<StyleComponentValueRule> parse_as_component_value();
  80. // 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.
  81. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  82. Vector<Vector<StyleComponentValueRule>> parse_as_comma_separated_list_of_component_values();
  83. NonnullRefPtr<CSSStyleSheet> parse_as_css_stylesheet(Optional<AK::URL> location);
  84. RefPtr<ElementInlineCSSStyleDeclaration> parse_as_style_attribute(DOM::Element&);
  85. RefPtr<CSSRule> parse_as_css_rule();
  86. enum class SelectorParsingMode {
  87. Standard,
  88. // `<forgiving-selector-list>` and `<forgiving-relative-selector-list>`
  89. // are handled with this parameter, not as separate functions.
  90. // https://drafts.csswg.org/selectors/#forgiving-selector
  91. Forgiving
  92. };
  93. // Contrary to the name, these parse a comma-separated list of selectors, according to the spec.
  94. Optional<SelectorList> parse_as_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  95. Optional<SelectorList> parse_as_relative_selector(SelectorParsingMode = SelectorParsingMode::Standard);
  96. NonnullRefPtrVector<MediaQuery> parse_as_media_query_list();
  97. RefPtr<MediaQuery> parse_as_media_query();
  98. RefPtr<Supports> parse_as_supports();
  99. RefPtr<StyleValue> parse_as_css_value(PropertyID);
  100. static RefPtr<StyleValue> parse_css_value(Badge<StyleComputer>, ParsingContext const&, PropertyID, Vector<StyleComponentValueRule> const&);
  101. private:
  102. enum class ParsingResult {
  103. Done,
  104. IncludesIgnoredVendorPrefix,
  105. SyntaxError,
  106. };
  107. // "Parse a stylesheet" is intended to be the normal parser entry point, for parsing stylesheets.
  108. struct ParsedStyleSheet {
  109. Optional<AK::URL> location;
  110. NonnullRefPtrVector<StyleRule> rules;
  111. };
  112. template<typename T>
  113. ParsedStyleSheet parse_a_stylesheet(TokenStream<T>&, Optional<AK::URL> location);
  114. // "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>.
  115. template<typename T>
  116. NonnullRefPtrVector<StyleRule> parse_a_list_of_rules(TokenStream<T>&);
  117. // "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.
  118. template<typename T>
  119. RefPtr<StyleRule> parse_a_rule(TokenStream<T>&);
  120. template<typename T>
  121. Optional<StyleProperty> parse_a_declaration(TokenStream<T>&);
  122. template<typename T>
  123. Vector<DeclarationOrAtRule> parse_a_list_of_declarations(TokenStream<T>&);
  124. template<typename T>
  125. Optional<StyleComponentValueRule> parse_a_component_value(TokenStream<T>&);
  126. template<typename T>
  127. Vector<StyleComponentValueRule> parse_a_list_of_component_values(TokenStream<T>&);
  128. template<typename T>
  129. Vector<Vector<StyleComponentValueRule>> parse_a_comma_separated_list_of_component_values(TokenStream<T>&);
  130. enum class SelectorType {
  131. Standalone,
  132. Relative
  133. };
  134. template<typename T>
  135. Result<SelectorList, ParsingResult> parse_a_selector_list(TokenStream<T>&, SelectorType, SelectorParsingMode = SelectorParsingMode::Standard);
  136. template<typename T>
  137. NonnullRefPtrVector<MediaQuery> parse_a_media_query_list(TokenStream<T>&);
  138. template<typename T>
  139. RefPtr<Supports> parse_a_supports(TokenStream<T>&);
  140. enum class AllowTrailingTokens {
  141. No,
  142. Yes
  143. };
  144. Optional<Selector::SimpleSelector::ANPlusBPattern> parse_a_n_plus_b_pattern(TokenStream<StyleComponentValueRule>&, AllowTrailingTokens = AllowTrailingTokens::No);
  145. enum class TopLevel {
  146. No,
  147. Yes
  148. };
  149. template<typename T>
  150. [[nodiscard]] NonnullRefPtrVector<StyleRule> consume_a_list_of_rules(TokenStream<T>&, TopLevel);
  151. template<typename T>
  152. [[nodiscard]] NonnullRefPtr<StyleRule> consume_an_at_rule(TokenStream<T>&);
  153. template<typename T>
  154. [[nodiscard]] RefPtr<StyleRule> consume_a_qualified_rule(TokenStream<T>&);
  155. template<typename T>
  156. [[nodiscard]] Vector<DeclarationOrAtRule> consume_a_list_of_declarations(TokenStream<T>&);
  157. template<typename T>
  158. [[nodiscard]] Optional<StyleDeclarationRule> consume_a_declaration(TokenStream<T>&);
  159. template<typename T>
  160. [[nodiscard]] StyleComponentValueRule consume_a_component_value(TokenStream<T>&);
  161. template<typename T>
  162. [[nodiscard]] NonnullRefPtr<StyleBlockRule> consume_a_simple_block(TokenStream<T>&);
  163. template<typename T>
  164. [[nodiscard]] NonnullRefPtr<StyleFunctionRule> consume_a_function(TokenStream<T>&);
  165. [[nodiscard]] Optional<GeneralEnclosed> parse_general_enclosed(TokenStream<StyleComponentValueRule>&);
  166. RefPtr<CSSRule> parse_font_face_rule(TokenStream<StyleComponentValueRule>&);
  167. [[nodiscard]] RefPtr<CSSRule> convert_to_rule(NonnullRefPtr<StyleRule>);
  168. [[nodiscard]] RefPtr<PropertyOwningCSSStyleDeclaration> convert_to_style_declaration(Vector<DeclarationOrAtRule> declarations);
  169. [[nodiscard]] Optional<StyleProperty> convert_to_style_property(StyleDeclarationRule const&);
  170. class Dimension {
  171. public:
  172. Dimension(Angle&& value)
  173. : m_value(move(value))
  174. {
  175. }
  176. Dimension(Frequency&& value)
  177. : m_value(move(value))
  178. {
  179. }
  180. Dimension(Length&& value)
  181. : m_value(move(value))
  182. {
  183. }
  184. Dimension(Percentage&& value)
  185. : m_value(move(value))
  186. {
  187. }
  188. Dimension(Resolution&& value)
  189. : m_value(move(value))
  190. {
  191. }
  192. Dimension(Time&& value)
  193. : m_value(move(value))
  194. {
  195. }
  196. bool is_angle() const;
  197. Angle angle() const;
  198. bool is_angle_percentage() const;
  199. AnglePercentage angle_percentage() const;
  200. bool is_frequency() const;
  201. Frequency frequency() const;
  202. bool is_frequency_percentage() const;
  203. FrequencyPercentage frequency_percentage() const;
  204. bool is_length() const;
  205. Length length() const;
  206. bool is_length_percentage() const;
  207. LengthPercentage length_percentage() const;
  208. bool is_percentage() const;
  209. Percentage percentage() const;
  210. bool is_resolution() const;
  211. Resolution resolution() const;
  212. bool is_time() const;
  213. Time time() const;
  214. bool is_time_percentage() const;
  215. TimePercentage time_percentage() const;
  216. private:
  217. Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value;
  218. };
  219. Optional<Dimension> parse_dimension(StyleComponentValueRule const&);
  220. Optional<Color> parse_color(StyleComponentValueRule const&);
  221. Optional<Length> parse_length(StyleComponentValueRule const&);
  222. Optional<Ratio> parse_ratio(TokenStream<StyleComponentValueRule>&);
  223. enum class AllowedDataUrlType {
  224. None,
  225. Image,
  226. };
  227. Optional<AK::URL> parse_url_function(StyleComponentValueRule const&, AllowedDataUrlType = AllowedDataUrlType::None);
  228. Result<NonnullRefPtr<StyleValue>, ParsingResult> parse_css_value(PropertyID, TokenStream<StyleComponentValueRule>&);
  229. RefPtr<StyleValue> parse_css_value(StyleComponentValueRule const&);
  230. RefPtr<StyleValue> parse_builtin_value(StyleComponentValueRule const&);
  231. RefPtr<StyleValue> parse_dynamic_value(StyleComponentValueRule const&);
  232. RefPtr<StyleValue> parse_calculated_value(Vector<StyleComponentValueRule> const&);
  233. RefPtr<StyleValue> parse_dimension_value(StyleComponentValueRule const&);
  234. RefPtr<StyleValue> parse_numeric_value(StyleComponentValueRule const&);
  235. RefPtr<StyleValue> parse_identifier_value(StyleComponentValueRule const&);
  236. RefPtr<StyleValue> parse_color_value(StyleComponentValueRule const&);
  237. RefPtr<StyleValue> parse_string_value(StyleComponentValueRule const&);
  238. RefPtr<StyleValue> parse_image_value(StyleComponentValueRule const&);
  239. template<typename ParseFunction>
  240. RefPtr<StyleValue> parse_comma_separated_value_list(Vector<StyleComponentValueRule> const&, ParseFunction);
  241. RefPtr<StyleValue> parse_simple_comma_separated_value_list(Vector<StyleComponentValueRule> const&);
  242. RefPtr<StyleValue> parse_background_value(Vector<StyleComponentValueRule> const&);
  243. RefPtr<StyleValue> parse_single_background_position_value(TokenStream<StyleComponentValueRule>&);
  244. RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<StyleComponentValueRule>&);
  245. RefPtr<StyleValue> parse_single_background_size_value(TokenStream<StyleComponentValueRule>&);
  246. RefPtr<StyleValue> parse_border_value(Vector<StyleComponentValueRule> const&);
  247. RefPtr<StyleValue> parse_border_radius_value(Vector<StyleComponentValueRule> const&);
  248. RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<StyleComponentValueRule> const&);
  249. RefPtr<StyleValue> parse_content_value(Vector<StyleComponentValueRule> const&);
  250. RefPtr<StyleValue> parse_flex_value(Vector<StyleComponentValueRule> const&);
  251. RefPtr<StyleValue> parse_flex_flow_value(Vector<StyleComponentValueRule> const&);
  252. RefPtr<StyleValue> parse_font_value(Vector<StyleComponentValueRule> const&);
  253. RefPtr<StyleValue> parse_font_family_value(Vector<StyleComponentValueRule> const&, size_t start_index = 0);
  254. RefPtr<StyleValue> parse_list_style_value(Vector<StyleComponentValueRule> const&);
  255. RefPtr<StyleValue> parse_overflow_value(Vector<StyleComponentValueRule> const&);
  256. enum class AllowInsetKeyword {
  257. No,
  258. Yes,
  259. };
  260. RefPtr<StyleValue> parse_shadow_value(Vector<StyleComponentValueRule> const&, AllowInsetKeyword);
  261. RefPtr<StyleValue> parse_single_shadow_value(TokenStream<StyleComponentValueRule>&, AllowInsetKeyword);
  262. RefPtr<StyleValue> parse_text_decoration_value(Vector<StyleComponentValueRule> const&);
  263. RefPtr<StyleValue> parse_transform_value(Vector<StyleComponentValueRule> const&);
  264. RefPtr<StyleValue> parse_transform_origin_value(Vector<StyleComponentValueRule> const&);
  265. // calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
  266. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(TokenStream<StyleComponentValueRule>&);
  267. OwnPtr<CalculatedStyleValue::CalcProduct> parse_calc_product(TokenStream<StyleComponentValueRule>&);
  268. Optional<CalculatedStyleValue::CalcValue> parse_calc_value(TokenStream<StyleComponentValueRule>&);
  269. OwnPtr<CalculatedStyleValue::CalcNumberSum> parse_calc_number_sum(TokenStream<StyleComponentValueRule>&);
  270. OwnPtr<CalculatedStyleValue::CalcNumberProduct> parse_calc_number_product(TokenStream<StyleComponentValueRule>&);
  271. Optional<CalculatedStyleValue::CalcNumberValue> parse_calc_number_value(TokenStream<StyleComponentValueRule>&);
  272. OwnPtr<CalculatedStyleValue::CalcProductPartWithOperator> parse_calc_product_part_with_operator(TokenStream<StyleComponentValueRule>&);
  273. OwnPtr<CalculatedStyleValue::CalcSumPartWithOperator> parse_calc_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  274. OwnPtr<CalculatedStyleValue::CalcNumberProductPartWithOperator> parse_calc_number_product_part_with_operator(TokenStream<StyleComponentValueRule>& tokens);
  275. OwnPtr<CalculatedStyleValue::CalcNumberSumPartWithOperator> parse_calc_number_sum_part_with_operator(TokenStream<StyleComponentValueRule>&);
  276. OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_expression(Vector<StyleComponentValueRule> const&);
  277. Result<NonnullRefPtr<Selector>, ParsingResult> parse_complex_selector(TokenStream<StyleComponentValueRule>&, SelectorType);
  278. Result<Selector::CompoundSelector, ParsingResult> parse_compound_selector(TokenStream<StyleComponentValueRule>&);
  279. Optional<Selector::Combinator> parse_selector_combinator(TokenStream<StyleComponentValueRule>&);
  280. Result<Selector::SimpleSelector, ParsingResult> parse_attribute_simple_selector(StyleComponentValueRule const&);
  281. Result<Selector::SimpleSelector, ParsingResult> parse_pseudo_simple_selector(TokenStream<StyleComponentValueRule>&);
  282. Result<Selector::SimpleSelector, ParsingResult> parse_simple_selector(TokenStream<StyleComponentValueRule>&);
  283. NonnullRefPtr<MediaQuery> parse_media_query(TokenStream<StyleComponentValueRule>&);
  284. OwnPtr<MediaCondition> parse_media_condition(TokenStream<StyleComponentValueRule>&, MediaCondition::AllowOr allow_or);
  285. Optional<MediaFeature> parse_media_feature(TokenStream<StyleComponentValueRule>&);
  286. Optional<MediaQuery::MediaType> parse_media_type(TokenStream<StyleComponentValueRule>&);
  287. OwnPtr<MediaCondition> parse_media_in_parens(TokenStream<StyleComponentValueRule>&);
  288. Optional<MediaFeatureValue> parse_media_feature_value(MediaFeatureID, TokenStream<StyleComponentValueRule>&);
  289. OwnPtr<Supports::Condition> parse_supports_condition(TokenStream<StyleComponentValueRule>&);
  290. Optional<Supports::InParens> parse_supports_in_parens(TokenStream<StyleComponentValueRule>&);
  291. Optional<Supports::Feature> parse_supports_feature(TokenStream<StyleComponentValueRule>&);
  292. static bool has_ignored_vendor_prefix(StringView);
  293. static bool is_builtin(StringView);
  294. struct PropertiesAndCustomProperties {
  295. Vector<StyleProperty> properties;
  296. HashMap<String, StyleProperty> custom_properties;
  297. };
  298. PropertiesAndCustomProperties extract_properties(Vector<DeclarationOrAtRule> const&);
  299. ParsingContext m_context;
  300. Tokenizer m_tokenizer;
  301. Vector<Token> m_tokens;
  302. TokenStream<Token> m_token_stream;
  303. };
  304. }
  305. namespace Web {
  306. RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const&, StringView, Optional<AK::URL> location = {});
  307. RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const&, StringView, DOM::Element&);
  308. RefPtr<CSS::StyleValue> parse_css_value(CSS::ParsingContext const&, StringView, CSS::PropertyID property_id = CSS::PropertyID::Invalid);
  309. Optional<CSS::SelectorList> parse_selector(CSS::ParsingContext const&, StringView);
  310. RefPtr<CSS::CSSRule> parse_css_rule(CSS::ParsingContext const&, StringView);
  311. RefPtr<CSS::MediaQuery> parse_media_query(CSS::ParsingContext const&, StringView);
  312. NonnullRefPtrVector<CSS::MediaQuery> parse_media_query_list(CSS::ParsingContext const&, StringView);
  313. RefPtr<CSS::Supports> parse_css_supports(CSS::ParsingContext const&, StringView);
  314. }