ElementDefinition.h 9.1 KB

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