StyleSheetNode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "StyleSheetNode.h"
  29. #include "../../Include/RmlUi/Core/Element.h"
  30. #include "../../Include/RmlUi/Core/Profiling.h"
  31. #include "StyleSheetFactory.h"
  32. #include "StyleSheetNodeSelector.h"
  33. #include <algorithm>
  34. namespace Rml {
  35. StyleSheetNode::StyleSheetNode()
  36. {
  37. CalculateAndSetSpecificity();
  38. }
  39. StyleSheetNode::StyleSheetNode(StyleSheetNode* parent, const String& tag, const String& id, const StringList& classes, const StringList& pseudo_classes, const StructuralSelectorList& structural_selectors, bool child_combinator)
  40. : parent(parent), tag(tag), id(id), class_names(classes), pseudo_class_names(pseudo_classes), structural_selectors(structural_selectors), child_combinator(child_combinator)
  41. {
  42. CalculateAndSetSpecificity();
  43. }
  44. StyleSheetNode::StyleSheetNode(StyleSheetNode* parent, String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes, StructuralSelectorList&& structural_selectors, bool child_combinator)
  45. : parent(parent), tag(std::move(tag)), id(std::move(id)), class_names(std::move(classes)), pseudo_class_names(std::move(pseudo_classes)), structural_selectors(std::move(structural_selectors)), child_combinator(child_combinator)
  46. {
  47. CalculateAndSetSpecificity();
  48. }
  49. StyleSheetNode* StyleSheetNode::GetOrCreateChildNode(const StyleSheetNode& other)
  50. {
  51. // See if we match the target child
  52. for (const auto& child : children)
  53. {
  54. if (child->EqualRequirements(other.tag, other.id, other.class_names, other.pseudo_class_names, other.structural_selectors, other.child_combinator))
  55. return child.get();
  56. }
  57. // We don't, so create a new child
  58. auto child = std::make_unique<StyleSheetNode>(this, other.tag, other.id, other.class_names, other.pseudo_class_names, other.structural_selectors, other.child_combinator);
  59. StyleSheetNode* result = child.get();
  60. children.push_back(std::move(child));
  61. return result;
  62. }
  63. StyleSheetNode* StyleSheetNode::GetOrCreateChildNode(String&& tag, String&& id, StringList&& classes, StringList&& pseudo_classes, StructuralSelectorList&& structural_pseudo_classes, bool child_combinator)
  64. {
  65. // See if we match an existing child
  66. for (const auto& child : children)
  67. {
  68. if (child->EqualRequirements(tag, id, classes, pseudo_classes, structural_pseudo_classes, child_combinator))
  69. return child.get();
  70. }
  71. // We don't, so create a new child
  72. auto child = std::make_unique<StyleSheetNode>(this, std::move(tag), std::move(id), std::move(classes), std::move(pseudo_classes), std::move(structural_pseudo_classes), child_combinator);
  73. StyleSheetNode* result = child.get();
  74. children.push_back(std::move(child));
  75. return result;
  76. }
  77. // Merges an entire tree hierarchy into our hierarchy.
  78. bool StyleSheetNode::MergeHierarchy(StyleSheetNode* node, int specificity_offset)
  79. {
  80. // Merge the other node's properties into ours.
  81. properties.Merge(node->properties, specificity_offset);
  82. for (const auto& other_child : node->children)
  83. {
  84. StyleSheetNode* local_node = GetOrCreateChildNode(*other_child);
  85. local_node->MergeHierarchy(other_child.get(), specificity_offset);
  86. }
  87. return true;
  88. }
  89. // Builds up a style sheet's index recursively.
  90. void StyleSheetNode::BuildIndexAndOptimizeProperties(StyleSheet::NodeIndex& styled_node_index, const StyleSheet& style_sheet)
  91. {
  92. RMLUI_ZoneScoped;
  93. // If this has properties defined, then we insert it into the styled node index.
  94. if(properties.GetNumProperties() > 0)
  95. {
  96. // The keys of the node index is a hashed combination of tag and id. These are used for fast lookup of applicable nodes.
  97. size_t node_hash = StyleSheet::NodeHash(tag, id);
  98. StyleSheet::NodeList& nodes = styled_node_index[node_hash];
  99. auto it = std::find(nodes.begin(), nodes.end(), this);
  100. if(it == nodes.end())
  101. nodes.push_back(this);
  102. }
  103. // Turn any decorator and font-effect properties from String to DecoratorList / FontEffectList.
  104. // This is essentially an optimization, it will work fine to skip this step and let ElementStyle::ComputeValues() do all the work.
  105. // However, when we do it here, we only need to do it once.
  106. // Note, since the user may set a new decorator through its style, we still do the conversion as necessary again in ComputeValues.
  107. if (properties.GetNumProperties() > 0)
  108. {
  109. // Decorators
  110. if (const Property* property = properties.GetProperty(PropertyId::Decorator))
  111. {
  112. if (property->unit == Property::STRING)
  113. {
  114. const String string_value = property->Get<String>();
  115. if(DecoratorsPtr decorators = style_sheet.InstanceDecoratorsFromString(string_value, property->source))
  116. {
  117. Property new_property = *property;
  118. new_property.value = std::move(decorators);
  119. new_property.unit = Property::DECORATOR;
  120. properties.SetProperty(PropertyId::Decorator, new_property);
  121. }
  122. }
  123. }
  124. // Font-effects
  125. if (const Property * property = properties.GetProperty(PropertyId::FontEffect))
  126. {
  127. if (property->unit == Property::STRING)
  128. {
  129. const String string_value = property->Get<String>();
  130. FontEffectsPtr font_effects = style_sheet.InstanceFontEffectsFromString(string_value, property->source);
  131. Property new_property = *property;
  132. new_property.value = std::move(font_effects);
  133. new_property.unit = Property::FONTEFFECT;
  134. properties.SetProperty(PropertyId::FontEffect, new_property);
  135. }
  136. }
  137. }
  138. for (auto& child : children)
  139. {
  140. child->BuildIndexAndOptimizeProperties(styled_node_index, style_sheet);
  141. }
  142. }
  143. bool StyleSheetNode::SetStructurallyVolatileRecursive(bool ancestor_is_structural_pseudo_class)
  144. {
  145. // If any ancestor or descendant is a structural pseudo class, then we are structurally volatile.
  146. bool self_is_structural_pseudo_class = (!structural_selectors.empty());
  147. // Check our children for structural pseudo-classes.
  148. bool descendant_is_structural_pseudo_class = false;
  149. for (auto& child : children)
  150. {
  151. if (child->SetStructurallyVolatileRecursive(self_is_structural_pseudo_class || ancestor_is_structural_pseudo_class))
  152. descendant_is_structural_pseudo_class = true;
  153. }
  154. is_structurally_volatile = (self_is_structural_pseudo_class || ancestor_is_structural_pseudo_class || descendant_is_structural_pseudo_class);
  155. return (self_is_structural_pseudo_class || descendant_is_structural_pseudo_class);
  156. }
  157. bool StyleSheetNode::EqualRequirements(const String& _tag, const String& _id, const StringList& _class_names, const StringList& _pseudo_class_names, const StructuralSelectorList& _structural_selectors, bool _child_combinator) const
  158. {
  159. if (tag != _tag)
  160. return false;
  161. if (id != _id)
  162. return false;
  163. if (class_names != _class_names)
  164. return false;
  165. if (pseudo_class_names != _pseudo_class_names)
  166. return false;
  167. if (structural_selectors != _structural_selectors)
  168. return false;
  169. if (child_combinator != _child_combinator)
  170. return false;
  171. return true;
  172. }
  173. // Returns the specificity of this node.
  174. int StyleSheetNode::GetSpecificity() const
  175. {
  176. return specificity;
  177. }
  178. // Imports properties from a single rule definition (ie, with a shared specificity) into the node's
  179. // properties.
  180. void StyleSheetNode::ImportProperties(const PropertyDictionary& _properties, int rule_specificity)
  181. {
  182. properties.Import(_properties, specificity + rule_specificity);
  183. }
  184. // Returns the node's default properties.
  185. const PropertyDictionary& StyleSheetNode::GetProperties() const
  186. {
  187. return properties;
  188. }
  189. inline bool StyleSheetNode::Match(const Element* element) const
  190. {
  191. if (!tag.empty() && tag != element->GetTagName())
  192. return false;
  193. if (!id.empty() && id != element->GetId())
  194. return false;
  195. if (!MatchClassPseudoClass(element))
  196. return false;
  197. if (!MatchStructuralSelector(element))
  198. return false;
  199. return true;
  200. }
  201. inline bool StyleSheetNode::MatchClassPseudoClass(const Element* element) const
  202. {
  203. for (auto& name : class_names)
  204. {
  205. if (!element->IsClassSet(name))
  206. return false;
  207. }
  208. for (auto& name : pseudo_class_names)
  209. {
  210. if (!element->IsPseudoClassSet(name))
  211. return false;
  212. }
  213. return true;
  214. }
  215. inline bool StyleSheetNode::MatchStructuralSelector(const Element* element) const
  216. {
  217. for (auto& node_selector : structural_selectors)
  218. {
  219. if (!node_selector.selector->IsApplicable(element, node_selector.a, node_selector.b))
  220. return false;
  221. }
  222. return true;
  223. }
  224. // Returns true if this node is applicable to the given element, given its IDs, classes and heritage.
  225. bool StyleSheetNode::IsApplicable(const Element* const in_element, bool skip_id_tag) const
  226. {
  227. // Determine whether the element matches the current node and its entire lineage. The entire hierarchy of
  228. // the element's document will be considered during the match as necessary.
  229. if (skip_id_tag)
  230. {
  231. // Id and tag have already been checked, only check class and pseudo class.
  232. if (!MatchClassPseudoClass(in_element))
  233. return false;
  234. }
  235. else
  236. {
  237. // Id and tag have not already been matched, match everything.
  238. if (!Match(in_element))
  239. return false;
  240. }
  241. const Element* element = in_element;
  242. // Walk up through all our parent nodes, each one of them must be matched by some ancestor element.
  243. for(const StyleSheetNode* node = parent; node && node->parent; node = node->parent)
  244. {
  245. // Try a match on every element ancestor. If it succeeds, we continue on to the next node.
  246. for(element = element->GetParentNode(); element; element = element->GetParentNode())
  247. {
  248. if (node->Match(element))
  249. break;
  250. // If we have a child combinator on the node, we must match this first ancestor.
  251. else if (node->child_combinator)
  252. return false;
  253. }
  254. // We have run out of element ancestors before we matched every node. Bail out.
  255. if (!element)
  256. return false;
  257. }
  258. // Finally, check the structural selector requirements last as they can be quite slow.
  259. if (!MatchStructuralSelector(in_element))
  260. return false;
  261. return true;
  262. }
  263. bool StyleSheetNode::IsStructurallyVolatile() const
  264. {
  265. return is_structurally_volatile;
  266. }
  267. void StyleSheetNode::CalculateAndSetSpecificity()
  268. {
  269. // Calculate the specificity of just this node; tags are worth 10,000, IDs 1,000,000 and other specifiers (classes
  270. // and pseudo-classes) 100,000.
  271. specificity = 0;
  272. if (!tag.empty())
  273. specificity += 10'000;
  274. if (!id.empty())
  275. specificity += 1'000'000;
  276. specificity += 100'000*(int)class_names.size();
  277. specificity += 100'000*(int)pseudo_class_names.size();
  278. specificity += 100'000*(int)structural_selectors.size();
  279. // Add our parent's specificity onto ours.
  280. if (parent)
  281. specificity += parent->specificity;
  282. }
  283. } // namespace Rml