StyleSheetNode.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 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 <tuple>
  33. namespace Rml {
  34. struct StyleSheetIndex;
  35. class StyleSheetNode;
  36. class StyleSheetNodeSelector;
  37. struct StructuralSelector {
  38. StructuralSelector(StyleSheetNodeSelector* selector, int a, int b) : selector(selector), a(a), b(b) {}
  39. StyleSheetNodeSelector* selector;
  40. int a;
  41. int b;
  42. };
  43. inline bool operator==(const StructuralSelector& a, const StructuralSelector& b)
  44. {
  45. return a.selector == b.selector && a.a == b.a && a.b == b.b;
  46. }
  47. inline bool operator<(const StructuralSelector& a, const StructuralSelector& b)
  48. {
  49. return std::tie(a.selector, a.a, a.b) < std::tie(b.selector, b.a, b.b);
  50. }
  51. enum class SelectorCombinator : byte {
  52. None,
  53. Child, // The 'E > F' combinator: Matches if F is a child of E.
  54. NextSibling, // The 'E + F' combinator: Matches if F is immediately preceded by E.
  55. SubsequentSibling, // The 'E ~ F' combinator: Matches if F is preceded by E.
  56. };
  57. using StructuralSelectorList = Vector<StructuralSelector>;
  58. using StyleSheetNodeList = Vector<UniquePtr<StyleSheetNode>>;
  59. /**
  60. A style sheet is composed of a tree of nodes.
  61. @author Pete / Lloyd
  62. */
  63. class StyleSheetNode {
  64. public:
  65. StyleSheetNode();
  66. StyleSheetNode(StyleSheetNode* parent, const String& tag, const String& id, const StringList& classes, const StringList& pseudo_classes,
  67. const StructuralSelectorList& structural_selectors, SelectorCombinator combinator);
  68. StyleSheetNode(StyleSheetNode* parent, String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes,
  69. StructuralSelectorList&& structural_selectors, SelectorCombinator combinator);
  70. /// Retrieves a child node with the given requirements if they match an existing node, or else creates a new one.
  71. StyleSheetNode* GetOrCreateChildNode(String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes,
  72. StructuralSelectorList&& structural_selectors, SelectorCombinator combinator);
  73. /// Retrieves or creates a child node with requirements equivalent to the 'other' node.
  74. StyleSheetNode* GetOrCreateChildNode(const StyleSheetNode& other);
  75. /// Merges an entire tree hierarchy into our hierarchy.
  76. void MergeHierarchy(StyleSheetNode* node, int specificity_offset = 0);
  77. /// Copy this node including all descendent nodes.
  78. UniquePtr<StyleSheetNode> DeepCopy(StyleSheetNode* parent = nullptr) const;
  79. /// Recursively set structural volatility.
  80. bool SetStructurallyVolatileRecursive(bool ancestor_is_structurally_volatile);
  81. /// Builds up a style sheet's index recursively.
  82. void BuildIndex(StyleSheetIndex& styled_node_index) const;
  83. /// Imports properties from a single rule definition into the node's properties and sets the appropriate specificity on them. Any existing
  84. /// attributes sharing a key with a new attribute will be overwritten if they are of a lower specificity.
  85. /// @param[in] properties The properties to import.
  86. /// @param[in] rule_specificity The specificity of the importing rule.
  87. void ImportProperties(const PropertyDictionary& properties, int rule_specificity);
  88. /// Returns the node's default properties.
  89. const PropertyDictionary& GetProperties() const;
  90. /// Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  91. bool IsApplicable(const Element* element) const;
  92. /// Returns the specificity of this node.
  93. int GetSpecificity() const;
  94. /// Returns true if this node employs a structural selector, and therefore generates element definitions that are sensitive to sibling changes.
  95. /// @warning Result is only valid if structural volatility is set since any changes to the node tree.
  96. bool IsStructurallyVolatile() const;
  97. private:
  98. // Returns true if the requirements of this node equals the given arguments.
  99. bool EqualRequirements(const String& tag, const String& id, const StringList& classes, const StringList& pseudo_classes,
  100. const StructuralSelectorList& structural_pseudo_classes, SelectorCombinator combinator) const;
  101. void CalculateAndSetSpecificity();
  102. // Match an element to the local node requirements.
  103. inline bool Match(const Element* element) const;
  104. inline bool MatchClassPseudoClass(const Element* element) const;
  105. inline bool MatchStructuralSelector(const Element* element) const;
  106. // The parent of this node; is nullptr for the root node.
  107. StyleSheetNode* parent = nullptr;
  108. // Node requirements
  109. String tag;
  110. String id;
  111. StringList class_names;
  112. StringList pseudo_class_names;
  113. StructuralSelectorList structural_selectors; // Represents structural pseudo classes
  114. SelectorCombinator combinator = SelectorCombinator::None;
  115. // True if any ancestor, descendent, or self is a structural pseudo class.
  116. bool is_structurally_volatile = true;
  117. // 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.
  118. int specificity = 0;
  119. PropertyDictionary properties;
  120. StyleSheetNodeList children;
  121. };
  122. } // namespace Rml
  123. #endif