Parser.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/Vector.h>
  10. #include <LibWeb/CSS/Parser/AtStyleRule.h>
  11. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  12. #include <LibWeb/CSS/Parser/QualifiedStyleRule.h>
  13. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  14. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  15. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  16. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  17. #include <LibWeb/CSS/Parser/Tokenizer.h>
  18. #include <LibWeb/CSS/Selector.h>
  19. namespace Web::CSS {
  20. class CSSStyleSheet;
  21. class CSSRule;
  22. class CSSStyleRule;
  23. struct StyleProperty;
  24. class Parser {
  25. public:
  26. Parser(const StringView& input, const String& encoding = "utf-8");
  27. ~Parser();
  28. // The normal parser entry point, for parsing stylesheets.
  29. NonnullRefPtr<CSSStyleSheet> parse_as_stylesheet();
  30. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  31. NonnullRefPtrVector<CSSRule> parse_as_list_of_rules();
  32. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  33. RefPtr<CSSRule> parse_as_rule();
  34. // Used in @supports conditions. [CSS3-CONDITIONAL]
  35. Optional<StyleProperty> parse_as_declaration();
  36. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  37. Vector<StyleProperty> parse_as_list_of_declarations();
  38. // For things that need to consume a single value, like the parsing rules for attr().
  39. Optional<StyleComponentValueRule> parse_as_component_value();
  40. // 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.
  41. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  42. Vector<StyleComponentValueRule> parse_as_list_of_comma_separated_component_values();
  43. Vector<CSS::Selector::ComplexSelector> parse_selectors(Vector<StyleComponentValueRule> parts);
  44. // FIXME: https://www.w3.org/TR/selectors-4/
  45. Optional<String> parse_a_selector() { return {}; }
  46. Optional<String> parse_a_relative_selector() { return {}; }
  47. bool match_a_selector_against_an_element() { return false; }
  48. bool match_a_selector_against_a_pseudo_element() { return false; }
  49. bool match_a_selector_against_a_tree() { return false; }
  50. // FIXME: https://drafts.csswg.org/css-backgrounds-3/
  51. static Optional<String> as_valid_background_repeat(String input) { return input; }
  52. static Optional<String> as_valid_background_attachment(String input) { return input; }
  53. static Optional<String> as_valid_background_position(String input) { return input; }
  54. static Optional<String> as_valid_background_clip(String input) { return input; }
  55. static Optional<String> as_valid_background_origin(String input) { return input; }
  56. static Optional<String> as_valid_background_size(String input) { return input; }
  57. static Optional<String> as_valid_border_style(String input) { return input; }
  58. static Optional<String> as_valid_border_image_repeat(String input) { return input; }
  59. void dump_all_tokens();
  60. private:
  61. Token next_token();
  62. Token peek_token();
  63. Token current_token();
  64. void reconsume_current_input_token();
  65. NonnullRefPtrVector<QualifiedStyleRule> consume_a_list_of_rules(bool top_level);
  66. NonnullRefPtr<AtStyleRule> consume_an_at_rule();
  67. RefPtr<QualifiedStyleRule> consume_a_qualified_rule();
  68. Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  69. Optional<StyleDeclarationRule> consume_a_declaration(Vector<StyleComponentValueRule>);
  70. Optional<StyleDeclarationRule> consume_a_declaration();
  71. StyleComponentValueRule consume_a_component_value();
  72. NonnullRefPtr<StyleBlockRule> consume_a_simple_block();
  73. NonnullRefPtr<StyleFunctionRule> consume_a_function();
  74. RefPtr<CSSRule> convert_rule(NonnullRefPtr<QualifiedStyleRule>);
  75. Tokenizer m_tokenizer;
  76. Vector<Token> m_tokens;
  77. int m_iterator_offset { -1 };
  78. };
  79. }