Parser.h 17 KB

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