Parser.h 18 KB

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