StyleSheet.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "../../Include/RmlUi/Core/StyleSheet.h"
  30. #include <algorithm>
  31. #include "ElementDefinition.h"
  32. #include "StyleSheetFactory.h"
  33. #include "StyleSheetNode.h"
  34. #include "StyleSheetParser.h"
  35. #include "../../Include/RmlUi/Core/Element.h"
  36. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  37. #include "../../Include/RmlUi/Core/StyleSheetSpecification.h"
  38. namespace Rml {
  39. namespace Core {
  40. // Sorts style nodes based on specificity.
  41. static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode* rhs)
  42. {
  43. return lhs->GetSpecificity() < rhs->GetSpecificity();
  44. }
  45. StyleSheet::StyleSheet()
  46. {
  47. root = new StyleSheetNode("", StyleSheetNode::ROOT);
  48. specificity_offset = 0;
  49. }
  50. StyleSheet::~StyleSheet()
  51. {
  52. delete root;
  53. // Release our reference count on the cached element definitions.
  54. for (ElementDefinitionCache::iterator cache_iterator = address_cache.begin(); cache_iterator != address_cache.end(); cache_iterator++)
  55. (*cache_iterator).second->RemoveReference();
  56. for (ElementDefinitionCache::iterator cache_iterator = node_cache.begin(); cache_iterator != node_cache.end(); cache_iterator++)
  57. (*cache_iterator).second->RemoveReference();
  58. }
  59. bool StyleSheet::LoadStyleSheet(Stream* stream)
  60. {
  61. StyleSheetParser parser;
  62. specificity_offset = parser.Parse(root, keyframes, stream);
  63. return specificity_offset >= 0;
  64. }
  65. /// Combines this style sheet with another one, producing a new sheet
  66. StyleSheet* StyleSheet::CombineStyleSheet(const StyleSheet* other_sheet) const
  67. {
  68. StyleSheet* new_sheet = new StyleSheet();
  69. if (!new_sheet->root->MergeHierarchy(root) ||
  70. !new_sheet->root->MergeHierarchy(other_sheet->root, specificity_offset))
  71. {
  72. delete new_sheet;
  73. return NULL;
  74. }
  75. // Any matching @keyframe names are overridden as per CSS rules
  76. for (auto& other_keyframes : other_sheet->keyframes)
  77. {
  78. new_sheet->keyframes[other_keyframes.first] = other_keyframes.second;
  79. }
  80. new_sheet->specificity_offset = specificity_offset + other_sheet->specificity_offset;
  81. return new_sheet;
  82. }
  83. // Builds the node index for a combined style sheet.
  84. void StyleSheet::BuildNodeIndex()
  85. {
  86. if (complete_node_index.empty())
  87. {
  88. styled_node_index.clear();
  89. complete_node_index.clear();
  90. root->BuildIndex(styled_node_index, complete_node_index);
  91. }
  92. }
  93. // Returns the Keyframes of the given name, or null if it does not exist.
  94. Keyframes * StyleSheet::GetKeyframes(const String & name) {
  95. auto it = keyframes.find(name);
  96. if (it != keyframes.end())
  97. return &(it->second);
  98. return nullptr;
  99. }
  100. // Returns the compiled element definition for a given element hierarchy.
  101. ElementDefinition* StyleSheet::GetElementDefinition(const Element* element) const
  102. {
  103. // Address cache is disabled for the time being; this doesn't work since the introduction of structural
  104. // pseudo-classes.
  105. ElementDefinitionCache::iterator cache_iterator;
  106. /* String element_address = element->GetAddress();
  107. // Look the address up in the definition, see if we've processed a similar element before.
  108. cache_iterator = address_cache.find(element_address);
  109. if (cache_iterator != address_cache.end())
  110. {
  111. ElementDefinition* definition = (*cache_iterator).second;
  112. definition->AddReference();
  113. return definition;
  114. }*/
  115. // See if there are any styles defined for this element.
  116. std::vector< const StyleSheetNode* > applicable_nodes;
  117. String tags[] = {element->GetTagName(), ""};
  118. for (int i = 0; i < 2; i++)
  119. {
  120. NodeIndex::const_iterator iterator = styled_node_index.find(tags[i]);
  121. if (iterator != styled_node_index.end())
  122. {
  123. const NodeList& nodes = (*iterator).second;
  124. // There are! Now see if we satisfy all of their parenting requirements. What this involves is traversing the style
  125. // nodes backwards, trying to match nodes in the element's hierarchy to nodes in the style hierarchy.
  126. for (NodeList::const_iterator iterator = nodes.begin(); iterator != nodes.end(); iterator++)
  127. {
  128. if ((*iterator)->IsApplicable(element))
  129. {
  130. // Get the node to add any of its non-tag children that we match into our list.
  131. (*iterator)->GetApplicableDescendants(applicable_nodes, element);
  132. }
  133. }
  134. }
  135. }
  136. std::sort(applicable_nodes.begin(), applicable_nodes.end(), StyleSheetNodeSort);
  137. // Compile the list of volatile pseudo-classes for this element definition.
  138. PseudoClassList volatile_pseudo_classes;
  139. bool structurally_volatile = false;
  140. for (int i = 0; i < 2; ++i)
  141. {
  142. NodeIndex::const_iterator iterator = complete_node_index.find(tags[i]);
  143. if (iterator != complete_node_index.end())
  144. {
  145. const NodeList& nodes = (*iterator).second;
  146. // See if we satisfy all of the parenting requirements for each of these nodes (as in the previous loop).
  147. for (NodeList::const_iterator iterator = nodes.begin(); iterator != nodes.end(); iterator++)
  148. {
  149. structurally_volatile |= (*iterator)->IsStructurallyVolatile();
  150. if ((*iterator)->IsApplicable(element))
  151. {
  152. std::vector< const StyleSheetNode* > volatile_nodes;
  153. (*iterator)->GetApplicableDescendants(volatile_nodes, element);
  154. for (size_t i = 0; i < volatile_nodes.size(); ++i)
  155. volatile_nodes[i]->GetVolatilePseudoClasses(volatile_pseudo_classes);
  156. }
  157. }
  158. }
  159. }
  160. // If this element definition won't actually store any information, don't bother with it.
  161. if (applicable_nodes.empty() &&
  162. volatile_pseudo_classes.empty() &&
  163. !structurally_volatile)
  164. return NULL;
  165. // Check if this puppy has already been cached in the node index; it may be that it has already been created by an
  166. // element with a different address but an identical output definition.
  167. String node_ids;
  168. for (size_t i = 0; i < applicable_nodes.size(); i++)
  169. node_ids += String(10, "%x ", applicable_nodes[i]);
  170. for (PseudoClassList::iterator i = volatile_pseudo_classes.begin(); i != volatile_pseudo_classes.end(); ++i)
  171. node_ids += String(32, ":%s", (*i).CString());
  172. cache_iterator = node_cache.find(node_ids);
  173. if (cache_iterator != node_cache.end())
  174. {
  175. ElementDefinition* definition = (*cache_iterator).second;
  176. definition->AddReference();
  177. return definition;
  178. }
  179. // Create the new definition and add it to our cache. One reference count is added, bringing the total to two; one
  180. // for the element that requested it, and one for the cache.
  181. ElementDefinition* new_definition = new ElementDefinition();
  182. new_definition->Initialise(applicable_nodes, volatile_pseudo_classes, structurally_volatile);
  183. // Add to the address cache.
  184. // address_cache[element_address] = new_definition;
  185. // new_definition->AddReference();
  186. // Add to the node cache.
  187. node_cache[node_ids] = new_definition;
  188. new_definition->AddReference();
  189. return new_definition;
  190. }
  191. // Destroys the style sheet.
  192. void StyleSheet::OnReferenceDeactivate()
  193. {
  194. delete this;
  195. }
  196. }
  197. }