ElementDefinition.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef ROCKETCOREELEMENTDEFINITION_H
  28. #define ROCKETCOREELEMENTDEFINITION_H
  29. #include "../../Include/Rocket/Core/Dictionary.h"
  30. #include "../../Include/Rocket/Core/ReferenceCountable.h"
  31. #include <map>
  32. #include "../../Include/Rocket/Core/FontEffect.h"
  33. #include "StyleSheetNode.h"
  34. namespace Rocket {
  35. namespace Core {
  36. class Decorator;
  37. class FontEffect;
  38. class ElementDefinitionIterator;
  39. typedef SmallUnorderedMap< String, Decorator* > DecoratorMap;
  40. typedef std::map< StringList, DecoratorMap > PseudoClassDecoratorMap;
  41. /**
  42. @author Peter Curry
  43. */
  44. class ElementDefinition : public ReferenceCountable
  45. {
  46. public:
  47. enum PseudoClassVolatility
  48. {
  49. STABLE, // pseudo-class has no volatility
  50. FONT_VOLATILE, // pseudo-class may impact on font effects
  51. STRUCTURE_VOLATILE // pseudo-class may impact on definitions of child elements
  52. };
  53. ElementDefinition();
  54. virtual ~ElementDefinition();
  55. /// Initialises the element definition from a list of style sheet nodes.
  56. void Initialise(const std::vector< const StyleSheetNode* >& style_sheet_nodes, const PseudoClassList& volatile_pseudo_classes, bool structurally_volatile);
  57. /// Returns a specific property from the element definition's base properties.
  58. /// @param[in] name The name of the property to return.
  59. /// @param[in] pseudo_classes The pseudo-classes currently active on the calling element.
  60. /// @return The property defined against the give name, or NULL if no such property was found.
  61. const Property* GetProperty(const String& name, const PseudoClassList& pseudo_classes) const;
  62. /// Returns the list of properties this element definition defines for an element with the given set of
  63. /// pseudo-classes.
  64. /// @param[out] property_names The list to store the defined properties in.
  65. /// @param[in] pseudo_classes The pseudo-classes defined on the querying element.
  66. void GetDefinedProperties(PropertyNameList& property_names, const PseudoClassList& pseudo_classes) const;
  67. /// Returns the list of properties this element definition has explicit definitions for involving the given
  68. /// pseudo-class.
  69. /// @param[out] property_names The list of store the newly defined / undefined properties in.
  70. /// @param[in] pseudo_classes The list of pseudo-classes currently set on the element (post-change).
  71. /// @param[in] pseudo_class The pseudo-class that was just activated or deactivated.
  72. void GetDefinedProperties(PropertyNameList& property_names, const PseudoClassList& pseudo_classes, const String& pseudo_class) const;
  73. /// Returns the list of the element definition's instanced decorators in the default state.
  74. /// @return The list of instanced decorators.
  75. const DecoratorMap& GetDecorators() const;
  76. /// Returns the map of pseudo-class names to overriding instanced decorators.
  77. /// @return The map of the overriding decorators for each pseudo-class.
  78. const PseudoClassDecoratorMap& GetPseudoClassDecorators() const;
  79. /// Appends this definition's font effects (appropriately for the given pseudo classes) into a
  80. /// provided map of effects.
  81. /// @param[out] font_effects The outgoing map of font effects.
  82. /// @param[in] pseudo_classes Pseudo-classes active on the querying element.
  83. void GetFontEffects(FontEffectMap& font_effects, const PseudoClassList& pseudo_classes) const;
  84. /// Returns the volatility of a pseudo-class.
  85. /// @param[in] pseudo_class The name of the pseudo-class to check for volatility.
  86. /// @return The volatility of the pseudo-class.
  87. PseudoClassVolatility GetPseudoClassVolatility(const String& pseudo_class) const;
  88. /// Returns true if this definition is built from nodes using structural selectors, and therefore is reliant on
  89. /// siblings remaining stable.
  90. /// @return True if this definition is structurally volatile.
  91. bool IsStructurallyVolatile() const;
  92. /// Returns an iterator to the first property matching the active set of pseudo_classes.
  93. ElementDefinitionIterator begin(const StringList& pseudo_classes) const;
  94. /// Returns an iterator to the property following the last property matching the active set of pseudo_classes.
  95. ElementDefinitionIterator end(const StringList& pseudo_classes) const;
  96. /// Returns true if the pseudo-class requirement of a rule is met by a list of an element's pseudo-classes.
  97. static bool IsPseudoClassRuleApplicable(const StringList& rule_pseudo_classes, const PseudoClassList& element_pseudo_classes);
  98. protected:
  99. /// Destroys the definition.
  100. void OnReferenceDeactivate();
  101. private:
  102. typedef std::pair< String, PropertyDictionary > PropertyGroup;
  103. typedef SmallUnorderedMap< String, PropertyGroup > PropertyGroupMap;
  104. typedef std::vector< std::pair< StringList, int > > PseudoClassFontEffectIndex;
  105. typedef SmallUnorderedMap< String, PseudoClassFontEffectIndex > FontEffectIndex;
  106. typedef SmallUnorderedMap< String, PseudoClassVolatility > PseudoClassVolatilityMap;
  107. // Finds all propery declarations for a group.
  108. void BuildPropertyGroup(PropertyGroupMap& groups, const String& group_type, const PropertyDictionary& element_properties, const PropertyGroupMap* default_properties = NULL);
  109. // Updates a property dictionary of all properties for a single group.
  110. int BuildPropertyGroupDictionary(PropertyDictionary& group_properties, const String& group_type, const String& group_name, const PropertyDictionary& element_properties);
  111. // Builds decorator definitions from the parsed properties and instances decorators as
  112. // appropriate.
  113. void InstanceDecorators(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  114. // Attempts to instance a decorator.
  115. bool InstanceDecorator(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  116. // Builds font effect definitions from the parsed properties and instances font effects as
  117. // appropriate.
  118. void InstanceFontEffects(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  119. // Attempts to instance a font effect.
  120. bool InstanceFontEffect(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  121. // The attributes for the default state of the element, with no pseudo-classes.
  122. PropertyDictionary properties;
  123. // The overridden attributes for the element's pseudo-classes.
  124. PseudoClassPropertyDictionary pseudo_class_properties;
  125. // The instanced decorators for this element definition.
  126. DecoratorMap decorators;
  127. // The overridden decorators for the element's pseudo-classes.
  128. PseudoClassDecoratorMap pseudo_class_decorators;
  129. // The list of every decorator used by this element in every class.
  130. FontEffectList font_effects;
  131. // For each unique decorator name, this stores (in order of specificity) the name of the
  132. // pseudo-class that has a definition for it, and the index into the list of decorators.
  133. FontEffectIndex font_effect_index;
  134. // The list of volatile pseudo-classes in this definition, and how volatile they are.
  135. PseudoClassVolatilityMap pseudo_class_volatility;
  136. // True if this definition has the potential to change as sibling elements are added or removed.
  137. bool structurally_volatile;
  138. };
  139. }
  140. }
  141. #endif