StyleSheet.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../Include/Rocket/Core/StyleSheet.h"
  29. #include <algorithm>
  30. #include "ElementDefinition.h"
  31. #include "StyleSheetFactory.h"
  32. #include "StyleSheetNode.h"
  33. #include "StyleSheetParser.h"
  34. #include "../../Include/Rocket/Core/Element.h"
  35. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  36. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  37. namespace Rocket {
  38. namespace Core {
  39. // Sorts style nodes based on specificity.
  40. static bool StyleSheetNodeSort(const StyleSheetNode* lhs, const StyleSheetNode* rhs)
  41. {
  42. return lhs->GetSpecificity() < rhs->GetSpecificity();
  43. }
  44. StyleSheet::StyleSheet()
  45. {
  46. root = new StyleSheetNode("", StyleSheetNode::ROOT);
  47. specificity_offset = 0;
  48. }
  49. StyleSheet::~StyleSheet()
  50. {
  51. delete root;
  52. // Release our reference count on the cached element definitions.
  53. for (ElementDefinitionCache::iterator cache_iterator = address_cache.begin(); cache_iterator != address_cache.end(); cache_iterator++)
  54. (*cache_iterator).second->RemoveReference();
  55. for (ElementDefinitionCache::iterator cache_iterator = node_cache.begin(); cache_iterator != node_cache.end(); cache_iterator++)
  56. (*cache_iterator).second->RemoveReference();
  57. }
  58. bool StyleSheet::LoadStyleSheet(Stream* stream)
  59. {
  60. StyleSheetParser parser;
  61. specificity_offset = parser.Parse(root, stream);
  62. return specificity_offset >= 0;
  63. }
  64. /// Combines this style sheet with another one, producing a new sheet
  65. StyleSheet* StyleSheet::CombineStyleSheet(const StyleSheet* other_sheet) const
  66. {
  67. StyleSheet* new_sheet = new StyleSheet();
  68. if (!new_sheet->root->MergeHierarchy(root) ||
  69. !new_sheet->root->MergeHierarchy(other_sheet->root, specificity_offset))
  70. {
  71. delete new_sheet;
  72. return NULL;
  73. }
  74. new_sheet->specificity_offset = specificity_offset + other_sheet->specificity_offset;
  75. return new_sheet;
  76. }
  77. // Builds the node index for a combined style sheet.
  78. void StyleSheet::BuildNodeIndex()
  79. {
  80. if (complete_node_index.empty())
  81. {
  82. styled_node_index.clear();
  83. complete_node_index.clear();
  84. root->BuildIndex(styled_node_index, complete_node_index);
  85. }
  86. }
  87. // Returns the compiled element definition for a given element hierarchy.
  88. ElementDefinition* StyleSheet::GetElementDefinition(const Element* element) const
  89. {
  90. // Address cache is disabled for the time being; this doesn't work since the introduction of structural
  91. // pseudo-classes.
  92. ElementDefinitionCache::iterator cache_iterator;
  93. /* String element_address = element->GetAddress();
  94. // Look the address up in the definition, see if we've processed a similar element before.
  95. cache_iterator = address_cache.find(element_address);
  96. if (cache_iterator != address_cache.end())
  97. {
  98. ElementDefinition* definition = (*cache_iterator).second;
  99. definition->AddReference();
  100. return definition;
  101. }*/
  102. // See if there are any styles defined for this element.
  103. std::vector< const StyleSheetNode* > applicable_nodes;
  104. String tags[] = {element->GetTagName(), ""};
  105. for (int i = 0; i < 2; i++)
  106. {
  107. NodeIndex::const_iterator iterator = styled_node_index.find(tags[i]);
  108. if (iterator != styled_node_index.end())
  109. {
  110. const NodeList& nodes = (*iterator).second;
  111. // There are! Now see if we satisfy all of their parenting requirements. What this involves is traversing the style
  112. // nodes backwards, trying to match nodes in the element's hierarchy to nodes in the style hierarchy.
  113. for (NodeList::const_iterator iterator = nodes.begin(); iterator != nodes.end(); iterator++)
  114. {
  115. if ((*iterator)->IsApplicable(element))
  116. {
  117. // Get the node to add any of its non-tag children that we match into our list.
  118. (*iterator)->GetApplicableDescendants(applicable_nodes, element);
  119. }
  120. }
  121. }
  122. }
  123. std::sort(applicable_nodes.begin(), applicable_nodes.end(), StyleSheetNodeSort);
  124. // Compile the list of volatile pseudo-classes for this element definition.
  125. PseudoClassList volatile_pseudo_classes;
  126. bool structurally_volatile = false;
  127. for (int i = 0; i < 2; ++i)
  128. {
  129. NodeIndex::const_iterator iterator = complete_node_index.find(tags[i]);
  130. if (iterator != complete_node_index.end())
  131. {
  132. const NodeList& nodes = (*iterator).second;
  133. // See if we satisfy all of the parenting requirements for each of these nodes (as in the previous loop).
  134. for (NodeList::const_iterator iterator = nodes.begin(); iterator != nodes.end(); iterator++)
  135. {
  136. structurally_volatile |= (*iterator)->IsStructurallyVolatile();
  137. if ((*iterator)->IsApplicable(element))
  138. {
  139. std::vector< const StyleSheetNode* > volatile_nodes;
  140. (*iterator)->GetApplicableDescendants(volatile_nodes, element);
  141. for (size_t i = 0; i < volatile_nodes.size(); ++i)
  142. volatile_nodes[i]->GetVolatilePseudoClasses(volatile_pseudo_classes);
  143. }
  144. }
  145. }
  146. }
  147. // If this element definition won't actually store any information, don't bother with it.
  148. if (applicable_nodes.empty() &&
  149. volatile_pseudo_classes.empty() &&
  150. !structurally_volatile)
  151. return NULL;
  152. // Check if this puppy has already been cached in the node index; it may be that it has already been created by an
  153. // element with a different address but an identical output definition.
  154. String node_ids;
  155. for (size_t i = 0; i < applicable_nodes.size(); i++)
  156. node_ids += String(10, "%x ", applicable_nodes[i]);
  157. for (PseudoClassList::iterator i = volatile_pseudo_classes.begin(); i != volatile_pseudo_classes.end(); ++i)
  158. node_ids += String(32, ":%s", (*i).CString());
  159. cache_iterator = node_cache.find(node_ids);
  160. if (cache_iterator != node_cache.end())
  161. {
  162. ElementDefinition* definition = (*cache_iterator).second;
  163. definition->AddReference();
  164. return definition;
  165. }
  166. // Create the new definition and add it to our cache. One reference count is added, bringing the total to two; one
  167. // for the element that requested it, and one for the cache.
  168. ElementDefinition* new_definition = new ElementDefinition();
  169. new_definition->Initialise(applicable_nodes, volatile_pseudo_classes, structurally_volatile);
  170. // Add to the address cache.
  171. // address_cache[element_address] = new_definition;
  172. // new_definition->AddReference();
  173. // Add to the node cache.
  174. node_cache[node_ids] = new_definition;
  175. new_definition->AddReference();
  176. return new_definition;
  177. }
  178. // Destroys the style sheet.
  179. void StyleSheet::OnReferenceDeactivate()
  180. {
  181. delete this;
  182. }
  183. }
  184. }