StyleSheetSelector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_STYLESHEETSELECTOR_H
  29. #define RMLUI_CORE_STYLESHEETSELECTOR_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. #include <tuple>
  32. namespace Rml {
  33. class Element;
  34. class StyleSheetNode;
  35. struct SelectorTree;
  36. namespace SelectorSpecificity {
  37. enum {
  38. // Constants used to determine the specificity of a selector.
  39. Tag = 10'000,
  40. Class = 100'000,
  41. PseudoClass = Class,
  42. ID = 1'000'000,
  43. };
  44. }
  45. enum class StructuralSelectorType {
  46. Invalid,
  47. Nth_Child,
  48. Nth_Last_Child,
  49. Nth_Of_Type,
  50. Nth_Last_Of_Type,
  51. First_Child,
  52. Last_Child,
  53. First_Of_Type,
  54. Last_Of_Type,
  55. Only_Child,
  56. Only_Of_Type,
  57. Empty,
  58. Not
  59. };
  60. struct StructuralSelector {
  61. StructuralSelector(StructuralSelectorType type, int a, int b) : type(type), a(a), b(b) {}
  62. StructuralSelector(StructuralSelectorType type, SharedPtr<const SelectorTree> tree, int specificity) :
  63. type(type), specificity(specificity), selector_tree(std::move(tree))
  64. {}
  65. StructuralSelectorType type = StructuralSelectorType::Invalid;
  66. // For counting selectors, the following are the 'a' and 'b' variables of an + b.
  67. int a = 0;
  68. int b = 0;
  69. // Specificity is usually determined like a pseudo class, but some types override this value.
  70. int specificity = SelectorSpecificity::PseudoClass;
  71. // For selectors that contain internal selectors such as :not().
  72. SharedPtr<const SelectorTree> selector_tree;
  73. };
  74. inline bool operator==(const StructuralSelector& a, const StructuralSelector& b)
  75. {
  76. // Currently sub-selectors (selector_tree) are only superficially compared. This mainly has the consequence that selectors with a sub-selector
  77. // which are instantiated separately will never compare equal, even if they have the exact same sub-selector expression. This further results in
  78. // such selectors not being de-duplicated. This should not lead to any functional differences but leads to potentially missed memory/performance
  79. // optimizations. E.g. 'div a, div b' will combine the two div nodes, while ':not(div) a, :not(div) b' will not combine the two not-div nodes.
  80. return a.type == b.type && a.a == b.a && a.b == b.b && a.selector_tree == b.selector_tree;
  81. }
  82. inline bool operator<(const StructuralSelector& a, const StructuralSelector& b)
  83. {
  84. return std::tie(a.type, a.a, a.b, a.selector_tree) < std::tie(b.type, b.a, b.b, b.selector_tree);
  85. }
  86. // A tree of unstyled style sheet nodes.
  87. struct SelectorTree {
  88. UniquePtr<StyleSheetNode> root;
  89. Vector<StyleSheetNode*> leafs; // Owned by root.
  90. };
  91. enum class SelectorCombinator : byte {
  92. None,
  93. Child, // The 'E > F' combinator: Matches if F is a child of E.
  94. NextSibling, // The 'E + F' combinator: Matches if F is immediately preceded by E.
  95. SubsequentSibling, // The 'E ~ F' combinator: Matches if F is preceded by E.
  96. };
  97. /// Returns true if the the node the given selector is discriminating for is applicable to a given element.
  98. /// @param element[in] The element to determine node applicability for.
  99. /// @param selector[in] The selector to test against the element.
  100. bool IsSelectorApplicable(const Element* element, const StructuralSelector& selector);
  101. } // namespace Rml
  102. #endif