StyleSheetNode.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 RMLUICORESTYLESHEETNODE_H
  29. #define RMLUICORESTYLESHEETNODE_H
  30. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  31. #include "../../Include/RmlUi/Core/StyleSheet.h"
  32. #include "../../Include/RmlUi/Core/Types.h"
  33. namespace Rml {
  34. namespace Core {
  35. class StyleSheetNodeSelector;
  36. struct NodeSelector {
  37. NodeSelector(StyleSheetNodeSelector* selector, int a, int b) : selector(selector), a(a), b(b) {}
  38. StyleSheetNodeSelector* selector;
  39. int a;
  40. int b;
  41. };
  42. inline bool operator==(const NodeSelector& a, const NodeSelector& b) { return a.selector == b.selector && a.a == b.a && a.b == b.b; }
  43. inline bool operator<(const NodeSelector& a, const NodeSelector& b) { return std::tie(a.selector, a.a, a.b) < std::tie(b.selector, b.a, b.b); }
  44. using NodeSelectorList = std::vector< NodeSelector >;
  45. using StyleSheetNodeList = std::vector< UniquePtr<StyleSheetNode> >;
  46. /**
  47. A style sheet is composed of a tree of nodes.
  48. @author Pete / Lloyd
  49. */
  50. class StyleSheetNode
  51. {
  52. public:
  53. StyleSheetNode();
  54. StyleSheetNode(StyleSheetNode* parent, const String& tag, const String& id, const StringList& classes, const StringList& pseudo_classes, const NodeSelectorList& structural_pseudo_classes);
  55. StyleSheetNode(StyleSheetNode* parent, String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes, NodeSelectorList&& structural_pseudo_classes);
  56. StyleSheetNode* GetOrCreateChildNode(const StyleSheetNode& other);
  57. StyleSheetNode* GetOrCreateChildNode(String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes, NodeSelectorList&& structural_pseudo_classes);
  58. /// Merges an entire tree hierarchy into our hierarchy.
  59. bool MergeHierarchy(StyleSheetNode* node, int specificity_offset = 0);
  60. /// Recursively set structural volatility
  61. bool SetStructurallyVolatileRecursive(bool ancestor_is_structurally_volatile);
  62. /// Builds up a style sheet's index recursively and optimizes some properties for faster retrieval.
  63. void BuildIndexAndOptimizeProperties(StyleSheet::NodeIndex& styled_node_index, const StyleSheet& style_sheet);
  64. /// Returns the name of this node.
  65. const String& GetTag() const;
  66. /// Returns the specificity of this node.
  67. int GetSpecificity() const;
  68. /// Imports properties from a single rule definition into the node's properties and sets the
  69. /// appropriate specificity on them. Any existing attributes sharing a key with a new attribute
  70. /// will be overwritten if they are of a lower specificity.
  71. /// @param[in] properties The properties to import.
  72. /// @param[in] rule_specificity The specificity of the importing rule.
  73. void ImportProperties(const PropertyDictionary& properties, int rule_specificity);
  74. /// Returns the node's default properties.
  75. const PropertyDictionary& GetProperties() const;
  76. /// Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  77. bool IsApplicable(const Element* element) const;
  78. /// Returns true if this node employs a structural selector, and therefore generates element definitions that are
  79. /// sensitive to sibling changes.
  80. /// @warning Result is only valid if structural volatility is set since any changes to the node tree.
  81. /// @return True if this node uses a structural selector.
  82. bool IsStructurallyVolatile() const;
  83. private:
  84. static bool Match(const Element* element, const StyleSheetNode* node);
  85. bool IsEquivalent(const String& tag, const String& id, const StringList& classes, const StringList& pseudo_classes, const NodeSelectorList& structural_pseudo_classes) const;
  86. int CalculateSpecificity();
  87. // The parent of this node; is nullptr for the root node.
  88. StyleSheetNode* parent;
  89. //bool child_selector; // The '>' CSS selector: Parent node must be applicable to the element parent.
  90. // The name.
  91. String tag;
  92. String id;
  93. StringList class_names;
  94. StringList pseudo_class_names;
  95. NodeSelectorList structural_selectors;
  96. // True if any ancestor, descendent, or self is a structural pseudo class.
  97. bool is_structurally_volatile;
  98. // A measure of specificity of this node; the attribute in a node with a higher value will override those of a
  99. // node with a lower value.
  100. int specificity;
  101. // The generic properties for this node.
  102. PropertyDictionary properties;
  103. // This node's child nodes, whether standard tagged children, or further derivations of this tag by ID or class.
  104. StyleSheetNodeList children;
  105. };
  106. }
  107. }
  108. #endif