StyleSheetNode.cpp 10 KB

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