StyleSheetNode.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_STYLESHEETNODE_H
  29. #define RMLUI_CORE_STYLESHEETNODE_H
  30. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  31. #include "../../Include/RmlUi/Core/Types.h"
  32. #include "StyleSheetSelector.h"
  33. namespace Rml {
  34. struct StyleSheetIndex;
  35. class StyleSheetNode;
  36. using StyleSheetNodeList = Vector<UniquePtr<StyleSheetNode>>;
  37. /**
  38. A style sheet is composed of a tree of nodes.
  39. @author Pete / Lloyd
  40. */
  41. class StyleSheetNode {
  42. public:
  43. StyleSheetNode();
  44. StyleSheetNode(StyleSheetNode* parent, const CompoundSelector& selector);
  45. StyleSheetNode(StyleSheetNode* parent, CompoundSelector&& selector);
  46. /// Retrieves or creates a child node with requirements equivalent to the 'other' node.
  47. StyleSheetNode* GetOrCreateChildNode(const CompoundSelector& other);
  48. /// Retrieves a child node with the given requirements if they match an existing node, or else creates a new one.
  49. StyleSheetNode* GetOrCreateChildNode(CompoundSelector&& other);
  50. /// Merges an entire tree hierarchy into our hierarchy.
  51. void MergeHierarchy(StyleSheetNode* node, int specificity_offset = 0);
  52. /// Copy this node including all descendent nodes.
  53. UniquePtr<StyleSheetNode> DeepCopy(StyleSheetNode* parent = nullptr) const;
  54. /// Builds up a style sheet's index recursively.
  55. void BuildIndex(StyleSheetIndex& styled_node_index) const;
  56. /// Imports properties from a single rule definition into the node's properties and sets the appropriate specificity on them. Any existing
  57. /// attributes sharing a key with a new attribute will be overwritten if they are of a lower specificity.
  58. /// @param[in] properties The properties to import.
  59. /// @param[in] rule_specificity The specificity of the importing rule.
  60. void ImportProperties(const PropertyDictionary& properties, int rule_specificity);
  61. /// Returns the node's default properties.
  62. const PropertyDictionary& GetProperties() const;
  63. /// Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  64. /// @note For performance reasons this call does not check whether 'element' is a text element. The caller must manually check this condition and
  65. /// consider any text element not applicable.
  66. bool IsApplicable(const Element* element, const Element* scope) const;
  67. /// Returns the specificity of this node.
  68. int GetSpecificity() const;
  69. private:
  70. void CalculateAndSetSpecificity();
  71. // Match an element to the local node requirements.
  72. inline bool Match(const Element* element, const Element* scope) const;
  73. inline bool MatchStructuralSelector(const Element* element, const Element* scope) const;
  74. inline bool MatchAttributes(const Element* element) const;
  75. // Recursively traverse the nodes up towards the root to match the element and its hierarchy.
  76. bool TraverseMatch(const Element* element, const Element* scope) const;
  77. // The parent of this node; is nullptr for the root node.
  78. StyleSheetNode* parent = nullptr;
  79. // Node requirements
  80. CompoundSelector selector;
  81. // A measure of specificity of this node; the attribute in a node with a higher value will override those of a node with a lower value.
  82. int specificity = 0;
  83. PropertyDictionary properties;
  84. StyleSheetNodeList children;
  85. };
  86. } // namespace Rml
  87. #endif