ElementDefinition.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /// Iterates over the properties in the definition.
  74. /// @param[inout] index Index of the property to fetch. This is incremented to the next valid index after the fetch.
  75. /// @param[in] pseudo_classes The pseudo-classes defined on the querying element.
  76. /// @param[out] property_pseudo_classes The pseudo-classes the property is defined by.
  77. /// @param[out] property_name The name of the property at the specified index.
  78. /// @param[out] property The property at the specified index.
  79. /// @return True if a property was successfully fetched.
  80. bool IterateProperties(int& index, const PseudoClassList& pseudo_classes, String& property_name, const Property*& property, const PseudoClassList** property_pseudo_classes = nullptr) const;
  81. /// Returns the list of the element definition's instanced decorators in the default state.
  82. /// @return The list of instanced decorators.
  83. const DecoratorMap& GetDecorators() const;
  84. /// Returns the map of pseudo-class names to overriding instanced decorators.
  85. /// @return The map of the overriding decorators for each pseudo-class.
  86. const PseudoClassDecoratorMap& GetPseudoClassDecorators() const;
  87. /// Appends this definition's font effects (appropriately for the given pseudo classes) into a
  88. /// provided map of effects.
  89. /// @param[out] font_effects The outgoing map of font effects.
  90. /// @param[in] pseudo_classes Pseudo-classes active on the querying element.
  91. void GetFontEffects(FontEffectMap& font_effects, const PseudoClassList& pseudo_classes) const;
  92. /// Returns the volatility of a pseudo-class.
  93. /// @param[in] pseudo_class The name of the pseudo-class to check for volatility.
  94. /// @return The volatility of the pseudo-class.
  95. PseudoClassVolatility GetPseudoClassVolatility(const String& pseudo_class) const;
  96. /// Returns true if this definition is built from nodes using structural selectors, and therefore is reliant on
  97. /// siblings remaining stable.
  98. /// @return True if this definition is structurally volatile.
  99. bool IsStructurallyVolatile() const;
  100. /// Returns an iterator to the first property matching the active set of pseudo_classes.
  101. ElementDefinitionIterator begin(const StringList& pseudo_classes) const;
  102. /// Returns an iterator to the property following the last property matching the active set of pseudo_classes.
  103. ElementDefinitionIterator end(const StringList& pseudo_classes) const;
  104. /// Returns true if the pseudo-class requirement of a rule is met by a list of an element's pseudo-classes.
  105. static bool IsPseudoClassRuleApplicable(const StringList& rule_pseudo_classes, const PseudoClassList& element_pseudo_classes);
  106. protected:
  107. /// Destroys the definition.
  108. void OnReferenceDeactivate();
  109. private:
  110. typedef std::pair< String, PropertyDictionary > PropertyGroup;
  111. typedef SmallUnorderedMap< String, PropertyGroup > PropertyGroupMap;
  112. typedef std::vector< std::pair< StringList, int > > PseudoClassFontEffectIndex;
  113. typedef SmallUnorderedMap< String, PseudoClassFontEffectIndex > FontEffectIndex;
  114. typedef SmallUnorderedMap< String, PseudoClassVolatility > PseudoClassVolatilityMap;
  115. // Finds all propery declarations for a group.
  116. void BuildPropertyGroup(PropertyGroupMap& groups, const String& group_type, const PropertyDictionary& element_properties, const PropertyGroupMap* default_properties = NULL);
  117. // Updates a property dictionary of all properties for a single group.
  118. int BuildPropertyGroupDictionary(PropertyDictionary& group_properties, const String& group_type, const String& group_name, const PropertyDictionary& element_properties);
  119. // Builds decorator definitions from the parsed properties and instances decorators as
  120. // appropriate.
  121. void InstanceDecorators(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  122. // Attempts to instance a decorator.
  123. bool InstanceDecorator(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  124. // Builds font effect definitions from the parsed properties and instances font effects as
  125. // appropriate.
  126. void InstanceFontEffects(const PseudoClassPropertyMap& merged_pseudo_class_properties);
  127. // Attempts to instance a font effect.
  128. bool InstanceFontEffect(const String& name, const String& type, const PropertyDictionary& properties, const StringList& pseudo_class = StringList());
  129. // The attributes for the default state of the element, with no pseudo-classes.
  130. PropertyDictionary properties;
  131. // The overridden attributes for the element's pseudo-classes.
  132. PseudoClassPropertyDictionary pseudo_class_properties;
  133. // The instanced decorators for this element definition.
  134. DecoratorMap decorators;
  135. // The overridden decorators for the element's pseudo-classes.
  136. PseudoClassDecoratorMap pseudo_class_decorators;
  137. // The list of every decorator used by this element in every class.
  138. FontEffectList font_effects;
  139. // For each unique decorator name, this stores (in order of specificity) the name of the
  140. // pseudo-class that has a definition for it, and the index into the list of decorators.
  141. FontEffectIndex font_effect_index;
  142. // The list of volatile pseudo-classes in this definition, and how volatile they are.
  143. PseudoClassVolatilityMap pseudo_class_volatility;
  144. // True if this definition has the potential to change as sibling elements are added or removed.
  145. bool structurally_volatile;
  146. };
  147. }
  148. }
  149. #endif