Parser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2020-2021, SerenityOS developers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullOwnPtrVector.h>
  28. #include <AK/Vector.h>
  29. #include <LibWeb/CSS/Parser/AtStyleRule.h>
  30. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  31. #include <LibWeb/CSS/Parser/QualifiedStyleRule.h>
  32. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  33. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  34. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  35. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  36. #include <LibWeb/CSS/Parser/Tokenizer.h>
  37. #include <LibWeb/CSS/Selector.h>
  38. namespace Web::CSS {
  39. class Parser {
  40. public:
  41. Parser(const StringView& input, const String& encoding = "utf-8");
  42. ~Parser();
  43. // The normal parser entry point, for parsing stylesheets.
  44. Vector<QualifiedStyleRule> parse_as_stylesheet();
  45. // For the content of at-rules such as @media. It differs from "Parse a stylesheet" in the handling of <CDO-token> and <CDC-token>.
  46. Vector<QualifiedStyleRule> parse_as_list_of_rules();
  47. // For use by the CSSStyleSheet#insertRule method, and similar functions which might exist, which parse text into a single rule.
  48. Optional<QualifiedStyleRule> parse_as_rule();
  49. // Used in @supports conditions. [CSS3-CONDITIONAL]
  50. Optional<StyleDeclarationRule> parse_as_declaration();
  51. // For the contents of a style attribute, which parses text into the contents of a single style rule.
  52. Vector<DeclarationOrAtRule> parse_as_list_of_declarations();
  53. // For things that need to consume a single value, like the parsing rules for attr().
  54. Optional<StyleComponentValueRule> parse_as_component_value();
  55. // 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.
  56. Vector<StyleComponentValueRule> parse_as_list_of_component_values();
  57. Vector<StyleComponentValueRule> parse_as_list_of_comma_separated_component_values();
  58. Vector<CSS::Selector::ComplexSelector> parse_selectors(Vector<String> parts);
  59. // FIXME: https://www.w3.org/TR/selectors-4/
  60. Optional<String> parse_a_selector() { return {}; }
  61. Optional<String> parse_a_relative_selector() { return {}; }
  62. bool match_a_selector_against_an_element() { return false; }
  63. bool match_a_selector_against_a_pseudo_element() { return false; }
  64. bool match_a_selector_against_a_tree() { return false; }
  65. // FIXME: https://drafts.csswg.org/css-backgrounds-3/
  66. static Optional<String> as_valid_background_repeat(String input) { return input; }
  67. static Optional<String> as_valid_background_attachment(String input) { return input; }
  68. static Optional<String> as_valid_background_position(String input) { return input; }
  69. static Optional<String> as_valid_background_clip(String input) { return input; }
  70. static Optional<String> as_valid_background_origin(String input) { return input; }
  71. static Optional<String> as_valid_background_size(String input) { return input; }
  72. static Optional<String> as_valid_border_style(String input) { return input; }
  73. static Optional<String> as_valid_border_image_repeat(String input) { return input; }
  74. void dump_all_tokens();
  75. private:
  76. Token next_token();
  77. Token peek_token();
  78. Token current_token();
  79. void reconsume_current_input_token();
  80. Vector<QualifiedStyleRule> consume_a_list_of_rules(bool top_level);
  81. AtStyleRule consume_an_at_rule();
  82. Optional<QualifiedStyleRule> consume_a_qualified_rule();
  83. Vector<DeclarationOrAtRule> consume_a_list_of_declarations();
  84. Optional<StyleDeclarationRule> consume_a_declaration(Vector<StyleComponentValueRule>);
  85. Optional<StyleDeclarationRule> consume_a_declaration();
  86. StyleComponentValueRule consume_a_component_value();
  87. StyleBlockRule consume_a_simple_block();
  88. StyleFunctionRule consume_a_function();
  89. Tokenizer m_tokenizer;
  90. Vector<Token> m_tokens;
  91. int m_iterator_offset { -1 };
  92. };
  93. }