2
0

StyleSheetParser.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #ifndef RMLUI_CORE_STYLESHEETPARSER_H
  29. #define RMLUI_CORE_STYLESHEETPARSER_H
  30. #include "../../Include/RmlUi/Core/StyleSheetTypes.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. namespace Rml {
  33. class PropertyDictionary;
  34. class Stream;
  35. class StyleSheetNode;
  36. class AbstractPropertyParser;
  37. struct PropertySource;
  38. using StyleSheetNodeListRaw = Vector<StyleSheetNode*>;
  39. /**
  40. Helper class for parsing a style sheet into its memory representation.
  41. @author Lloyd Weehuizen
  42. */
  43. class StyleSheetParser {
  44. public:
  45. StyleSheetParser();
  46. ~StyleSheetParser();
  47. /// Parses the given stream into the style sheet
  48. /// @param[out] style_sheets The collection of style sheets to write into, organized into media blocks
  49. /// @param[in] stream The stream to read
  50. /// @param[in] begin_line_number The used line number for the first line in the stream, for reporting errors.
  51. /// @return True on success, false on failure.
  52. bool Parse(MediaBlockList& style_sheets, Stream* stream, int begin_line_number);
  53. /// Parses the given string into the property dictionary
  54. /// @param[out] parsed_properties The properties dictionary the properties will be read into
  55. /// @param[in] properties The properties to parse
  56. /// @return True if the parse was successful, or false if an error occurred.
  57. void ParseProperties(PropertyDictionary& parsed_properties, const String& properties);
  58. /// Converts a selector query to a tree of nodes.
  59. /// @param[out] root_node Node to construct into.
  60. /// @param[in] selectors The selector rules as a string value.
  61. /// @return The list of leaf nodes in the constructed tree, which are all owned by the root node.
  62. static StyleSheetNodeListRaw ConstructNodes(StyleSheetNode& root_node, const String& selectors);
  63. /// Initialises property parsers. Call after initialisation of StylesheetSpecification.
  64. static void Initialise();
  65. /// Reset property parsers.
  66. static void Shutdown();
  67. private:
  68. /// Parses properties from the parse buffer.
  69. /// @param[in-out] property_parser An abstract parser which specifies how the properties are parsed and stored.
  70. void ReadProperties(AbstractPropertyParser& property_parser);
  71. /// Import properties into the stylesheet node
  72. /// @param[out] node Node to import into
  73. /// @param[in] rule The rule name to parse
  74. /// @param[in] properties The dictionary of properties
  75. /// @param[in] rule_specificity The specific of the rule
  76. /// @return The leaf node of the rule, or nullptr on parse failure.
  77. static StyleSheetNode* ImportProperties(StyleSheetNode* node, const String& rule, const PropertyDictionary& properties, int rule_specificity);
  78. /// Attempts to parse a @keyframes block
  79. bool ParseKeyframeBlock(KeyframesMap& keyframes_map, const String& identifier, const String& rules, const PropertyDictionary& properties);
  80. /// Attempts to parse a @decorator block
  81. bool ParseDecoratorBlock(const String& at_name, NamedDecoratorMap& named_decorator_map, const SharedPtr<const PropertySource>& source);
  82. /// Attempts to parse the properties of a @media query.
  83. /// @param[in] rules The rules to parse.
  84. /// @param[out] properties Parsed properties representing all values to be matched.
  85. /// @param[out] modifier Media query modifier.
  86. bool ParseMediaFeatureMap(const String& rules, PropertyDictionary& properties, MediaQueryModifier& modifier);
  87. /// Attempts to find one of the given character tokens in the active stream.
  88. /// If it's found, buffer is filled with all content up until the token, cursor advances past the token.
  89. /// @param[out] buffer The buffer that receives the content.
  90. /// @param[in] tokens The character tokens to find.
  91. /// @return The found token character, or '\0' if none was found.
  92. char FindAnyToken(String& buffer, const char* tokens);
  93. /// Attempts to find the next character in the active stream.
  94. /// If it's found, buffer is filled with the character
  95. /// @param[out] buffer The buffer that receives the character, if read.
  96. /// @return True if a character was read, false on end of stream.
  97. bool ReadCharacter(char& buffer);
  98. /// Fill the internal parse buffer
  99. bool FillBuffer();
  100. // Stream we're parsing from.
  101. Stream* stream;
  102. // Parser memory buffer.
  103. String parse_buffer;
  104. // How far we've read through the buffer.
  105. size_t parse_buffer_pos;
  106. // The name of the file we're parsing.
  107. String stream_file_name;
  108. // Current line number we're parsing.
  109. int line_number;
  110. };
  111. } // namespace Rml
  112. #endif