StyleSheetSelector.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_STYLESHEETSELECTOR_H
  29. #define RMLUI_CORE_STYLESHEETSELECTOR_H
  30. #include "../../Include/RmlUi/Core/Types.h"
  31. namespace Rml {
  32. class Element;
  33. class StyleSheetNode;
  34. /**
  35. Constants used to determine the specificity of a selector.
  36. */
  37. namespace SelectorSpecificity {
  38. enum {
  39. Tag = 10'000,
  40. Class = 100'000,
  41. Attribute = Class,
  42. PseudoClass = Class,
  43. ID = 1'000'000,
  44. };
  45. }
  46. /**
  47. Combinator determines how two connected compound selectors are matched against the element hierarchy.
  48. */
  49. enum class SelectorCombinator {
  50. Descendant, // The 'E F' (whitespace) combinator: Matches if F is a descendant of E.
  51. Child, // The 'E > F' combinator: Matches if F is a child of E.
  52. NextSibling, // The 'E + F' combinator: Matches if F is immediately preceded by E.
  53. SubsequentSibling, // The 'E ~ F' combinator: Matches if F is preceded by E.
  54. };
  55. /**
  56. Attribute selector.
  57. Such as [unit], [unit=meter], [href^=http]
  58. */
  59. enum class AttributeSelectorType {
  60. Always,
  61. Equal = '=',
  62. InList = '~',
  63. BeginsWithThenHyphen = '|',
  64. BeginsWith = '^',
  65. EndsWith = '$',
  66. Contains = '*',
  67. };
  68. struct AttributeSelector {
  69. AttributeSelectorType type = AttributeSelectorType::Always;
  70. String name;
  71. String value;
  72. };
  73. bool operator==(const AttributeSelector& a, const AttributeSelector& b);
  74. bool operator<(const AttributeSelector& a, const AttributeSelector& b);
  75. using AttributeSelectorList = Vector<AttributeSelector>;
  76. /**
  77. A tree of unstyled style sheet nodes.
  78. */
  79. struct SelectorTree {
  80. UniquePtr<StyleSheetNode> root;
  81. Vector<StyleSheetNode*> leafs; // Owned by root.
  82. };
  83. /**
  84. Tree-structural selector.
  85. Such as :nth-child(2n+1), :empty(), :not(div)
  86. */
  87. enum class StructuralSelectorType {
  88. Invalid,
  89. Nth_Child,
  90. Nth_Last_Child,
  91. Nth_Of_Type,
  92. Nth_Last_Of_Type,
  93. First_Child,
  94. Last_Child,
  95. First_Of_Type,
  96. Last_Of_Type,
  97. Only_Child,
  98. Only_Of_Type,
  99. Empty,
  100. Not,
  101. Scope,
  102. };
  103. struct StructuralSelector {
  104. StructuralSelector(StructuralSelectorType type, int a, int b) : type(type), a(a), b(b) {}
  105. StructuralSelector(StructuralSelectorType type, SharedPtr<const SelectorTree> tree, int specificity) :
  106. type(type), specificity(specificity), selector_tree(std::move(tree))
  107. {}
  108. StructuralSelectorType type = StructuralSelectorType::Invalid;
  109. // For counting selectors, the following are the 'a' and 'b' variables of an + b.
  110. int a = 0;
  111. int b = 0;
  112. // Specificity is usually determined like a pseudo class, but some types override this value.
  113. int specificity = SelectorSpecificity::PseudoClass;
  114. // For selectors that contain internal selectors such as :not().
  115. SharedPtr<const SelectorTree> selector_tree;
  116. };
  117. bool operator==(const StructuralSelector& a, const StructuralSelector& b);
  118. bool operator<(const StructuralSelector& a, const StructuralSelector& b);
  119. using StructuralSelectorList = Vector<StructuralSelector>;
  120. /**
  121. Compound selector contains all the basic selectors for a single node.
  122. Such as div#foo.bar:nth-child(2)
  123. */
  124. struct CompoundSelector {
  125. String tag;
  126. String id;
  127. StringList class_names;
  128. StringList pseudo_class_names;
  129. AttributeSelectorList attributes;
  130. StructuralSelectorList structural_selectors;
  131. SelectorCombinator combinator = SelectorCombinator::Descendant; // Determines how to match with our parent node.
  132. };
  133. bool operator==(const CompoundSelector& a, const CompoundSelector& b);
  134. /// Returns true if the node the given selector is discriminating for is applicable to a given element.
  135. /// @param element[in] The element to determine node applicability for.
  136. /// @param selector[in] The selector to test against the element.
  137. /// @param scope[in] The element considered as the reference point/scope (for :scope).
  138. bool IsSelectorApplicable(const Element* element, const StructuralSelector& selector, const Element* scope);
  139. } // namespace Rml
  140. #endif