StyleSheetNode.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. typedef std::map< StringList, PropertyDictionary > PseudoClassPropertyMap;
  37. /**
  38. A style sheet is composed of a tree of nodes.
  39. @author Pete / Lloyd
  40. */
  41. class StyleSheetNode
  42. {
  43. public:
  44. enum NodeType
  45. {
  46. TAG = 0,
  47. CLASS,
  48. ID,
  49. PSEUDO_CLASS,
  50. STRUCTURAL_PSEUDO_CLASS,
  51. NUM_NODE_TYPES, // only counts the listed node types
  52. ROOT // special node type we don't keep in a list
  53. };
  54. /// Constructs a generic style-sheet node.
  55. StyleSheetNode(const String& name, NodeType type, StyleSheetNode* parent = NULL);
  56. /// Constructs a structural style-sheet node.
  57. StyleSheetNode(const String& name, StyleSheetNode* parent, StyleSheetNodeSelector* selector, int a, int b);
  58. ~StyleSheetNode();
  59. /// Writes the style sheet node (and all ancestors) into the stream.
  60. void Write(Stream* stream);
  61. /// Merges an entire tree hierarchy into our hierarchy.
  62. bool MergeHierarchy(StyleSheetNode* node, int specificity_offset = 0);
  63. /// Builds up a style sheet's index recursively.
  64. void BuildIndex(StyleSheet::NodeIndex& styled_index, StyleSheet::NodeIndex& complete_index);
  65. /// Returns the name of this node.
  66. const String& GetName() const;
  67. /// Returns the specificity of this node.
  68. int GetSpecificity() const;
  69. /// Imports properties from a single rule definition into the node's properties and sets the
  70. /// appropriate specificity on them. Any existing attributes sharing a key with a new attribute
  71. /// will be overwritten if they are of a lower specificity.
  72. /// @param[in] properties The properties to import.
  73. /// @param[in] rule_specificity The specificity of the importing rule.
  74. void ImportProperties(const PropertyDictionary& properties, int rule_specificity);
  75. /// Merges properties from another node (ie, with potentially differing specificities) into the
  76. /// node's properties. Any existing properties sharing a key with a new attribute will be
  77. /// overwritten if they are of a lower specificity.
  78. /// @param[in] properties The properties to merge with this node's.
  79. /// @param[in] rule_specificity_offset The offset of the importing properties' specificities.
  80. void MergeProperties(const PropertyDictionary& properties, int rule_specificity_offset);
  81. /// Returns the node's default properties.
  82. const PropertyDictionary& GetProperties() const;
  83. /// Merges the properties of all of the pseudo-classes of this style sheet node into a single map.
  84. /// @param pseudo_class_properties[out] The dictionary to fill with the pseudo-class properties.
  85. void GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties) const;
  86. /// Adds to a list the names of this node's pseudo-classes which are deemed volatile; that is, which will
  87. /// potentially affect child node's element definition if set or unset.
  88. /// @param volatile_pseudo_classes[out] The list of volatile pseudo-classes.
  89. bool GetVolatilePseudoClasses(PseudoClassList& volatile_pseudo_classes) const;
  90. /// Returns a direct child node of this node of the requested type.
  91. /// @param name The name of the child node to fetch.
  92. /// @param type The type of node to fetch; this must be one of either TAG, CLASS, ID, PSEUDO_CLASS or PSEUDO_CLASS_STRUCTURAL.
  93. /// @param create If set to true, the node will be created if it doesn't exist.
  94. StyleSheetNode* GetChildNode(const String& name, NodeType type, bool create = true);
  95. /// Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  96. bool IsApplicable(const Element* element) const;
  97. /// Appends all applicable non-tag descendants of this node into the given element list.
  98. void GetApplicableDescendants(std::vector< const StyleSheetNode* >& applicable_nodes, const Element* element) const;
  99. /// Returns true if this node employs a structural selector, and therefore generates element definitions that are
  100. /// sensitive to sibling changes.
  101. /// @return True if this node uses a structural selector.
  102. bool IsStructurallyVolatile(bool check_ancestors = true) const;
  103. private:
  104. // Constructs a structural pseudo-class child node.
  105. StyleSheetNode* CreateStructuralChild(const String& child_name);
  106. // Recursively builds up a list of all pseudo-classes branching from a single node.
  107. void GetPseudoClassProperties(PseudoClassPropertyMap& pseudo_class_properties, const StringList& ancestor_pseudo_classes);
  108. int CalculateSpecificity();
  109. // The parent of this node; is NULL for the root node.
  110. StyleSheetNode* parent;
  111. // The name and type.
  112. String name;
  113. NodeType type;
  114. // The complex selector for this node; only used for structural nodes.
  115. StyleSheetNodeSelector* selector;
  116. int a;
  117. int b;
  118. // A measure of specificity of this node; the attribute in a node with a higher value will override those of a
  119. // node with a lower value.
  120. int specificity;
  121. // The generic properties for this node.
  122. PropertyDictionary properties;
  123. // This node's child nodes, whether standard tagged children, or further derivations of this tag by ID or class.
  124. typedef std::unordered_map< String, StyleSheetNode* > NodeMap;
  125. NodeMap children[NUM_NODE_TYPES];
  126. };
  127. }
  128. }
  129. #endif