ElementDefinition.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "ElementDefinition.h"
  29. #include "../../Include/Rocket/Core/Decorator.h"
  30. #include "../../Include/Rocket/Core/Factory.h"
  31. #include "../../Include/Rocket/Core/FontDatabase.h"
  32. #include "../../Include/Rocket/Core/Log.h"
  33. namespace Rocket {
  34. namespace Core {
  35. ElementDefinition::ElementDefinition()
  36. {
  37. structurally_volatile = false;
  38. }
  39. ElementDefinition::~ElementDefinition()
  40. {
  41. for (DecoratorMap::iterator i = decorators.begin(); i != decorators.end(); ++i)
  42. (*i).second->RemoveReference();
  43. for (PseudoClassDecoratorMap::iterator i = pseudo_class_decorators.begin(); i != pseudo_class_decorators.end(); ++i)
  44. {
  45. for (DecoratorMap::iterator j = (*i).second.begin(); j != (*i).second.end(); ++j)
  46. {
  47. if ((*j).second != NULL)
  48. (*j).second->RemoveReference();
  49. }
  50. }
  51. for (size_t i = 0; i < font_effects.size(); ++i)
  52. font_effects[i]->RemoveReference();
  53. }
  54. // Initialises the element definition from a list of style sheet nodes.
  55. void ElementDefinition::Initialise(const std::vector< const StyleSheetNode* >& style_sheet_nodes, const PseudoClassList& volatile_pseudo_classes, bool _structurally_volatile)
  56. {
  57. // Set the volatile structure flag.
  58. structurally_volatile = _structurally_volatile;
  59. // Mark all the volatile pseudo-classes as structurally volatile.
  60. for (PseudoClassList::const_iterator i = volatile_pseudo_classes.begin(); i != volatile_pseudo_classes.end(); ++i)
  61. pseudo_class_volatility[*i] = STRUCTURE_VOLATILE;
  62. // Merge the default (non-pseudo-class) properties.
  63. for (size_t i = 0; i < style_sheet_nodes.size(); ++i)
  64. {
  65. const auto& merge_properties = style_sheet_nodes[i]->GetProperties();
  66. properties.insert(merge_properties.begin(), merge_properties.end());
  67. }
  68. // Merge the pseudo-class properties.
  69. PseudoClassPropertyMap merged_pseudo_class_properties;
  70. for (size_t i = 0; i < style_sheet_nodes.size(); ++i)
  71. {
  72. // Merge all the pseudo-classes.
  73. PseudoClassPropertyMap node_properties;
  74. style_sheet_nodes[i]->GetPseudoClassProperties(node_properties);
  75. for (PseudoClassPropertyMap::iterator j = node_properties.begin(); j != node_properties.end(); ++j)
  76. {
  77. // Merge the property maps into one uber-map; for the decorators.
  78. PseudoClassPropertyMap::iterator k = merged_pseudo_class_properties.find((*j).first);
  79. if (k == merged_pseudo_class_properties.end())
  80. merged_pseudo_class_properties[(*j).first] = (*j).second;
  81. else
  82. {
  83. const auto& merge_properties = (*j).second;
  84. k->second.insert(merge_properties.begin(), merge_properties.end());
  85. }
  86. // Search through all entries in this dictionary; we'll insert each one into our optimised list of
  87. // pseudo-class properties.
  88. for (PropertyMap::const_iterator k = (*j).second.begin(); k != (*j).second.end(); ++k)
  89. {
  90. PropertyId property_id = (*k).first;
  91. const Property& property = (*k).second;
  92. // Skip this property if its specificity is lower than the base property's, as in
  93. // this case it will never be used.
  94. const Property* default_property = GetIf(properties, property_id);
  95. if (default_property != NULL &&
  96. default_property->specificity >= property.specificity)
  97. continue;
  98. // TODO: Pseudo-class should also probably be a PropertyId, for now we do a conversion to String here
  99. PseudoClassPropertyDictionary::iterator l = pseudo_class_properties.find(property_id);
  100. if (l == pseudo_class_properties.end())
  101. pseudo_class_properties[property_id] = PseudoClassPropertyList(1, PseudoClassProperty((*j).first, property));
  102. else
  103. {
  104. // Find the location to insert this entry in the map, based on property priorities.
  105. int index = 0;
  106. while (index < (int) (*l).second.size() &&
  107. (*l).second[index].second.specificity > property.specificity)
  108. index++;
  109. (*l).second.insert((*l).second.begin() + index, PseudoClassProperty((*j).first, property));
  110. }
  111. }
  112. }
  113. }
  114. InstanceDecorators(merged_pseudo_class_properties);
  115. InstanceFontEffects(merged_pseudo_class_properties);
  116. }
  117. // Returns a specific property from the element definition's base properties.
  118. const Property* ElementDefinition::GetProperty(PropertyId property_id, const PseudoClassList& pseudo_classes) const
  119. {
  120. // Find a pseudo-class override for this property.
  121. PseudoClassPropertyDictionary::const_iterator property_iterator = pseudo_class_properties.find(property_id);
  122. if (property_iterator != pseudo_class_properties.end())
  123. {
  124. const PseudoClassPropertyList& property_list = (*property_iterator).second;
  125. for (size_t i = 0; i < property_list.size(); ++i)
  126. {
  127. if (!IsPseudoClassRuleApplicable(property_list[i].first, pseudo_classes))
  128. continue;
  129. return &property_list[i].second;
  130. }
  131. }
  132. auto it = properties.find(property_id);
  133. if (it != properties.end())
  134. return &it->second;
  135. return nullptr;
  136. }
  137. // Returns the list of properties this element definition defines for an element with the given set of pseudo-classes.
  138. void ElementDefinition::GetDefinedProperties(PropertyIdList& property_ids, const PseudoClassList& pseudo_classes) const
  139. {
  140. for (auto& p : properties)
  141. property_ids.insert(p.first);
  142. for (PseudoClassPropertyDictionary::const_iterator i = pseudo_class_properties.begin(); i != pseudo_class_properties.end(); ++i)
  143. {
  144. PropertyId id = i->first;
  145. // If this property is already in the default dictionary, don't bother checking for it here.
  146. if (property_ids.find(id) != property_ids.end())
  147. continue;
  148. const PseudoClassPropertyList& property_list = (*i).second;
  149. // Search through all the pseudo-class combinations that have a definition for this property; if the calling
  150. // element matches at least one of them, then add it to the list.
  151. bool property_defined = false;
  152. for (size_t j = 0; j < property_list.size(); ++j)
  153. {
  154. if (IsPseudoClassRuleApplicable(property_list[j].first, pseudo_classes))
  155. {
  156. property_defined = true;
  157. break;
  158. }
  159. }
  160. if (property_defined)
  161. property_ids.insert(id);
  162. }
  163. }
  164. // Returns the list of properties this element definition has explicit definitions for involving the given
  165. // pseudo-class.
  166. void ElementDefinition::GetDefinedProperties(PropertyIdList& property_ids, const PseudoClassList& pseudo_classes, const String& pseudo_class) const
  167. {
  168. for (PseudoClassPropertyDictionary::const_iterator i = pseudo_class_properties.begin(); i != pseudo_class_properties.end(); ++i)
  169. {
  170. PropertyId id = i->first;
  171. // If this property has already been found, don't bother checking for it again.
  172. if (property_ids.find(id) != property_ids.end())
  173. continue;
  174. const PseudoClassPropertyList& property_list = (*i).second;
  175. bool property_defined = false;
  176. for (size_t j = 0; j < property_list.size(); ++j)
  177. {
  178. bool rule_valid = true;
  179. bool found_toggled_pseudo_class = false;
  180. const StringList& rule_pseudo_classes = property_list[j].first;
  181. for (size_t j = 0; j < rule_pseudo_classes.size(); ++j)
  182. {
  183. if (rule_pseudo_classes[j] == pseudo_class)
  184. {
  185. found_toggled_pseudo_class = true;
  186. continue;
  187. }
  188. if (std::find(pseudo_classes.begin(), pseudo_classes.end(), rule_pseudo_classes[j]) == pseudo_classes.end())
  189. {
  190. rule_valid = false;
  191. break;
  192. }
  193. }
  194. if (rule_valid &&
  195. found_toggled_pseudo_class)
  196. {
  197. property_defined = true;
  198. break;
  199. }
  200. }
  201. if (property_defined)
  202. property_ids.insert(id);
  203. }
  204. }
  205. // Iterates over the properties in the definition.
  206. bool ElementDefinition::IterateProperties(int& index, const PseudoClassList& pseudo_classes, PseudoClassList& property_pseudo_classes, PropertyId& property_id, const Property*& property) const
  207. {
  208. if (index < (int)properties.size())
  209. {
  210. PropertyMap::const_iterator i = properties.begin();
  211. for (int count = 0; count < index; ++count)
  212. ++i;
  213. property_pseudo_classes.clear();
  214. property_id = (*i).first;
  215. property = &((*i).second);
  216. ++index;
  217. return true;
  218. }
  219. // Not in the base properties; check for pseudo-class overrides.
  220. int property_count = (int)properties.size();
  221. for (PseudoClassPropertyDictionary::const_iterator i = pseudo_class_properties.begin(); i != pseudo_class_properties.end(); ++i)
  222. {
  223. // Iterate over each pseudo-class set that has a definition for this property; if we find one that matches our
  224. // pseudo-class, increment our index counter and either return that property (if we hit the requested index) or
  225. // continue looking if we're still below it.
  226. for (size_t j = 0; j < (*i).second.size(); ++j)
  227. {
  228. if (IsPseudoClassRuleApplicable((*i).second[j].first, pseudo_classes))
  229. {
  230. property_count++;
  231. if (property_count > index)
  232. {
  233. // Copy the list of pseudo-classes.
  234. property_pseudo_classes = (*i).second[j].first;
  235. property_id = (*i).first;
  236. property = &((*i).second[j].second);
  237. ++index;
  238. return true;
  239. }
  240. else
  241. {
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. return false;
  248. }
  249. // Returns the list of the element definition's instanced decorators in the default state.
  250. const DecoratorMap& ElementDefinition::GetDecorators() const
  251. {
  252. return decorators;
  253. }
  254. // Returns the map of pseudo-class names to overriding instanced decorators.
  255. const PseudoClassDecoratorMap& ElementDefinition::GetPseudoClassDecorators() const
  256. {
  257. return pseudo_class_decorators;
  258. }
  259. // Appends this definition's font effects into a provided map of effects.
  260. void ElementDefinition::GetFontEffects(FontEffectMap& applicable_font_effects, const PseudoClassList& pseudo_classes) const
  261. {
  262. // Check each set of named effects, looking for applicable ones.
  263. for (FontEffectIndex::const_iterator i = font_effect_index.begin(); i != font_effect_index.end(); ++i)
  264. {
  265. // Search through this list, finding the first effect that is valid (depending on
  266. // pseudo-classes).
  267. const PseudoClassFontEffectIndex& index = i->second;
  268. for (size_t j = 0; j < index.size(); ++j)
  269. {
  270. if (IsPseudoClassRuleApplicable(index[j].first, pseudo_classes))
  271. {
  272. // This is the most specific valid font effect this element has under the name. If
  273. // the map of effects already has an effect with the same name, the effect with the
  274. // highest specificity will prevail.
  275. FontEffect* applicable_effect = font_effects[index[j].second];
  276. FontEffectMap::iterator map_iterator = applicable_font_effects.find(i->first);
  277. if (map_iterator == applicable_font_effects.end() ||
  278. map_iterator->second->GetSpecificity() < applicable_effect->GetSpecificity())
  279. applicable_font_effects[i->first] = applicable_effect;
  280. break;
  281. }
  282. }
  283. }
  284. }
  285. // Returns the volatility of a pseudo-class.
  286. ElementDefinition::PseudoClassVolatility ElementDefinition::GetPseudoClassVolatility(const String& pseudo_class) const
  287. {
  288. PseudoClassVolatilityMap::const_iterator i = pseudo_class_volatility.find(pseudo_class);
  289. if (i == pseudo_class_volatility.end())
  290. return STABLE;
  291. else
  292. return i->second;
  293. }
  294. // Returns true if this definition is built from nodes using structural selectors.
  295. bool ElementDefinition::IsStructurallyVolatile() const
  296. {
  297. return structurally_volatile;
  298. }
  299. // Destroys the definition.
  300. void ElementDefinition::OnReferenceDeactivate()
  301. {
  302. delete this;
  303. }
  304. // Finds all propery declarations for a group.
  305. void ElementDefinition::BuildPropertyGroup(PropertyGroupMap& groups, const String& group_type, const PropertyDictionary& element_properties, const PropertyGroupMap* default_properties)
  306. {
  307. String property_suffix = "-" + group_type;
  308. for (PropertyMap::const_iterator property_iterator = element_properties.begin(); property_iterator != element_properties.end(); ++property_iterator)
  309. {
  310. const String& property_name = GetName((*property_iterator).first);
  311. if (property_name.size() > property_suffix.size() &&
  312. strcasecmp(property_name.c_str() + (property_name.size() - property_suffix.size()), property_suffix.c_str()) == 0)
  313. {
  314. // We've found a group declaration!
  315. String group_name = property_name.substr(0, property_name.size() - (group_type.size() + 1));
  316. String group_class = (*property_iterator).second.value.Get< String >();
  317. PropertyDictionary* group_properties = NULL;
  318. // Check if we have an existing definition by this name; if so, we're only overriding the type.
  319. PropertyGroupMap::iterator existing_definition = groups.find(group_name);
  320. if (existing_definition != groups.end())
  321. {
  322. (*existing_definition).second.first = group_class;
  323. group_properties = &(*existing_definition).second.second;
  324. }
  325. else
  326. {
  327. // Check if we have any default decorator definitions, and if the new decorator has a default. If so,
  328. // we make a copy of the default properties for the new decorator.
  329. if (default_properties != NULL)
  330. {
  331. PropertyGroupMap::const_iterator default_definition = default_properties->find(group_name);
  332. if (default_definition != default_properties->end())
  333. group_properties = &(*groups.insert(PropertyGroupMap::value_type(group_name, PropertyGroup(group_class, (*default_definition).second.second))).first).second.second;
  334. }
  335. // If we still haven't got somewhere to put the properties for the new decorator, make a new
  336. // definition.
  337. if (group_properties == NULL)
  338. group_properties = &(*groups.insert(PropertyGroupMap::value_type(group_name, PropertyGroup(group_class, PropertyDictionary()))).first).second.second;
  339. }
  340. // Now find all of this decorator's properties.
  341. BuildPropertyGroupDictionary(*group_properties, group_type, group_name, element_properties);
  342. }
  343. }
  344. // Now go through all the default decorator definitions and see if the new property list redefines any properties
  345. // used by them.
  346. if (default_properties != NULL)
  347. {
  348. for (PropertyGroupMap::const_iterator default_definition_iterator = default_properties->begin(); default_definition_iterator != default_properties->end(); ++default_definition_iterator)
  349. {
  350. const String& default_definition_name = (*default_definition_iterator).first;
  351. // Check the list of new definitions hasn't defined this decorator already; if so, it overrode the
  352. // decorator type and so has inherited all the properties anyway.
  353. if (groups.find(default_definition_name) == groups.end())
  354. {
  355. // Nope! Make a copy of the decorator's properties and see if the new dictionary overrides any of the
  356. // properties.
  357. PropertyDictionary decorator_properties = (*default_definition_iterator).second.second;
  358. if (BuildPropertyGroupDictionary(decorator_properties, group_type, default_definition_name, element_properties) > 0)
  359. groups[default_definition_name] = PropertyGroup((*default_definition_iterator).second.first, decorator_properties);
  360. }
  361. }
  362. }
  363. }
  364. // Updates a property dictionary of all properties for a single group.
  365. int ElementDefinition::BuildPropertyGroupDictionary(PropertyDictionary& group_properties, const String& ROCKET_UNUSED_PARAMETER(group_type), const String& group_name, const PropertyDictionary& element_properties)
  366. {
  367. ROCKET_UNUSED(group_type);
  368. int num_properties = 0;
  369. for (PropertyMap::const_iterator property_iterator = element_properties.begin(); property_iterator != element_properties.end(); ++property_iterator)
  370. {
  371. PropertyId id = (*property_iterator).first;
  372. const String& full_property_name = GetName(id);
  373. if (full_property_name.size() > group_name.size() + 1 &&
  374. strncasecmp(full_property_name.c_str(), group_name.c_str(), group_name.size()) == 0 &&
  375. full_property_name[group_name.size()] == '-')
  376. {
  377. String property_name = full_property_name.substr(group_name.size() + 1);
  378. // if (property_name == group_type)
  379. // continue;
  380. group_properties[id] = (*property_iterator).second;
  381. num_properties++;
  382. }
  383. }
  384. return num_properties;
  385. }
  386. // Builds decorator definitions from the parsed properties and instances decorators as appropriate.
  387. void ElementDefinition::InstanceDecorators(const PseudoClassPropertyMap& merged_pseudo_class_properties)
  388. {
  389. // Now we have the complete property list, we can compile decorator properties and instance as appropriate.
  390. PropertyGroupMap decorator_definitions;
  391. BuildPropertyGroup(decorator_definitions, "decorator", properties);
  392. for (PropertyGroupMap::iterator i = decorator_definitions.begin(); i != decorator_definitions.end(); ++i)
  393. InstanceDecorator((*i).first, (*i).second.first, (*i).second.second);
  394. // Now go through all the pseudo-class properties ...
  395. for (PseudoClassPropertyMap::const_iterator pseudo_class_iterator = merged_pseudo_class_properties.begin(); pseudo_class_iterator != merged_pseudo_class_properties.end(); ++pseudo_class_iterator)
  396. {
  397. PropertyGroupMap pseudo_class_decorator_definitions;
  398. BuildPropertyGroup(pseudo_class_decorator_definitions, "decorator", (*pseudo_class_iterator).second, &decorator_definitions);
  399. for (PropertyGroupMap::iterator i = pseudo_class_decorator_definitions.begin(); i != pseudo_class_decorator_definitions.end(); ++i)
  400. InstanceDecorator((*i).first, (*i).second.first, (*i).second.second, (*pseudo_class_iterator).first);
  401. }
  402. }
  403. // Attempts to instance a decorator into a given list.
  404. bool ElementDefinition::InstanceDecorator(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_classes)
  405. {
  406. Decorator* decorator = Factory::InstanceDecorator(type, properties);
  407. if (decorator == NULL)
  408. {
  409. Log::Message(Log::LT_WARNING, "Failed to instance decorator '%s' of type '%s'.", name.c_str(), type.c_str());
  410. return false;
  411. }
  412. if (pseudo_classes.empty())
  413. {
  414. if (decorator != NULL)
  415. decorators[name] = decorator;
  416. }
  417. else
  418. {
  419. PseudoClassDecoratorMap::iterator i = pseudo_class_decorators.find(pseudo_classes);
  420. if (i == pseudo_class_decorators.end())
  421. {
  422. DecoratorMap decorators;
  423. decorators[name] = decorator;
  424. pseudo_class_decorators[pseudo_classes] = decorators;
  425. }
  426. else
  427. (*i).second[name] = decorator;
  428. }
  429. return true;
  430. }
  431. // Builds font effect definitions from the parsed properties and instances font effects as appropriate.
  432. void ElementDefinition::InstanceFontEffects(const PseudoClassPropertyMap& merged_pseudo_class_properties)
  433. {
  434. // Now we have the complete property list, we can compile font-effect properties and instance as appropriate.
  435. PropertyGroupMap font_effect_definitions;
  436. BuildPropertyGroup(font_effect_definitions, "font-effect", properties);
  437. for (PropertyGroupMap::iterator i = font_effect_definitions.begin(); i != font_effect_definitions.end(); ++i)
  438. InstanceFontEffect((*i).first, (*i).second.first, (*i).second.second);
  439. // Now go through all the pseudo-class properties ...
  440. for (PseudoClassPropertyMap::const_iterator pseudo_class_iterator = merged_pseudo_class_properties.begin(); pseudo_class_iterator != merged_pseudo_class_properties.end(); ++pseudo_class_iterator)
  441. {
  442. PropertyGroupMap pseudo_class_font_effect_definitions;
  443. BuildPropertyGroup(pseudo_class_font_effect_definitions, "font-effect", (*pseudo_class_iterator).second, &font_effect_definitions);
  444. for (PropertyGroupMap::iterator i = pseudo_class_font_effect_definitions.begin(); i != pseudo_class_font_effect_definitions.end(); ++i)
  445. InstanceFontEffect((*i).first, (*i).second.first, (*i).second.second, (*pseudo_class_iterator).first);
  446. }
  447. }
  448. // Attempts to instance a font effect.
  449. bool ElementDefinition::InstanceFontEffect(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_classes)
  450. {
  451. FontEffect* font_effect = FontDatabase::GetFontEffect(type, properties);
  452. if (font_effect == NULL)
  453. {
  454. Log::Message(Log::LT_WARNING, "Failed to instance font effect '%s' of type '%s'.", name.c_str(), type.c_str());
  455. return false;
  456. }
  457. // Push the instanced effect into the list of effects.
  458. int effect_index = (int) font_effects.size();
  459. font_effects.push_back(font_effect);
  460. // Get a reference to the index list we're adding this effect to.
  461. PseudoClassFontEffectIndex* index = NULL;
  462. FontEffectIndex::iterator index_iterator = font_effect_index.find(name);
  463. if (index_iterator == font_effect_index.end())
  464. {
  465. // No others; create a new index for this name.
  466. index = &(font_effect_index.insert(FontEffectIndex::value_type(name, PseudoClassFontEffectIndex())).first->second);
  467. }
  468. else
  469. {
  470. index = &(index_iterator->second);
  471. }
  472. // Add the new effect into the index.
  473. PseudoClassFontEffectIndex::iterator insert_iterator;
  474. for (insert_iterator = index->begin(); insert_iterator != index->end(); ++insert_iterator)
  475. {
  476. // Keep iterating until we find an effect whose specificity is below the new effect's. The
  477. // new effect will be inserted before it in the list.
  478. if (font_effects[insert_iterator->second]->GetSpecificity() < font_effect->GetSpecificity())
  479. break;
  480. }
  481. index->insert(insert_iterator, PseudoClassFontEffectIndex::value_type(pseudo_classes, effect_index));
  482. // Mark the effect's pseudo-classes as volatile.
  483. for (size_t i = 0; i < pseudo_classes.size(); ++i)
  484. {
  485. PseudoClassVolatilityMap::const_iterator j = pseudo_class_volatility.find(pseudo_classes[i]);
  486. if (j == pseudo_class_volatility.end())
  487. pseudo_class_volatility[pseudo_classes[i]] = FONT_VOLATILE;
  488. }
  489. return true;
  490. }
  491. // Returns true if the pseudo-class requirement of a rule is met by a list of an element's pseudo-classes.
  492. bool ElementDefinition::IsPseudoClassRuleApplicable(const StringList& rule_pseudo_classes, const PseudoClassList& element_pseudo_classes) const
  493. {
  494. for (StringList::size_type i = 0; i < rule_pseudo_classes.size(); ++i)
  495. {
  496. if (std::find(element_pseudo_classes.begin(), element_pseudo_classes.end(), rule_pseudo_classes[i]) == element_pseudo_classes.end())
  497. return false;
  498. }
  499. return true;
  500. }
  501. }
  502. }