PropertySpecification.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 ROCKETCOREPROPERTYSPECIFICATION_H
  28. #define ROCKETCOREPROPERTYSPECIFICATION_H
  29. #include "Header.h"
  30. #include "Element.h"
  31. #include "PropertyDefinition.h"
  32. namespace Rocket {
  33. namespace Core {
  34. class PropertyDictionary;
  35. struct PropertyShorthandDefinition;
  36. /**
  37. A property specification stores a group of property definitions.
  38. @author Peter Curry
  39. */
  40. class ROCKETCORE_API PropertySpecification
  41. {
  42. public:
  43. enum ShorthandType
  44. {
  45. // Normal; properties that fail to parse fall-through to the next until they parse correctly, and any
  46. // undeclared are not set.
  47. FALL_THROUGH,
  48. // A single failed parse will abort, and any undeclared are replicated from the last declared property.
  49. REPLICATE,
  50. // For 'padding', 'margin', etc; up four properties are expected.
  51. BOX,
  52. // BOX if four properties are shorthanded and they end in '-top', '-right', etc, otherwise FALL_THROUGH.
  53. AUTO
  54. };
  55. PropertySpecification();
  56. ~PropertySpecification();
  57. /// Registers a property with a new definition.
  58. /// @param[in] property_name The name to register the new property under.
  59. /// @param[in] default_value The default value to be used for an element if it has no other definition provided.
  60. /// @param[in] inherited True if this property is inherited from parent to child, false otherwise.
  61. /// @param[in] forces_layout True if this property requires its parent to be reformatted if changed.
  62. /// @return The new property definition, ready to have parsers attached.
  63. PropertyDefinition& RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout);
  64. /// Returns a property definition.
  65. /// @param[in] property_name The name of the desired property.
  66. /// @return The appropriate property definition if it could be found, NULL otherwise.
  67. const PropertyDefinition* GetProperty(const String& property_name) const;
  68. /// Returns the list of the names of all registered property definitions.
  69. /// @return The list with stored property names.
  70. const PropertyNameList& GetRegisteredProperties() const;
  71. /// Returns the list of the names of all registered inherited property definitions.
  72. /// @return The list with stored property names.
  73. const PropertyNameList& GetRegisteredInheritedProperties() const;
  74. /// Registers a shorthand property definition.
  75. /// @param[in] shorthand_name The name to register the new shorthand property under.
  76. /// @param[in] properties A comma-separated list of the properties this definition is shorthand for. The order in which they are specified here is the order in which the values will be processed.
  77. /// @param[in] type The type of shorthand to declare.
  78. /// @param True if all the property names exist, false otherwise.
  79. bool RegisterShorthand(const String& shorthand_name, const String& property_names, ShorthandType type = AUTO);
  80. /// Returns a shorthand definition.
  81. /// @param[in] shorthand_name The name of the desired shorthand.
  82. /// @return The appropriate shorthand definition if it could be found, NULL otherwise.
  83. const PropertyShorthandDefinition* GetShorthand(const String& shorthand_name) const;
  84. /// Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  85. /// @param dictionary The property dictionary which will hold all declared properties.
  86. /// @param property_name The name of the declared property.
  87. /// @param property_value The values the property is being set to.
  88. /// @return True if all properties were parsed successfully, false otherwise.
  89. bool ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file = "", int source_line_number = 0) const;
  90. /// Sets all undefined properties in the dictionary to their defaults.
  91. /// @param dictionary[in] The dictionary to set the default values on.
  92. void SetPropertyDefaults(PropertyDictionary& dictionary) const;
  93. private:
  94. typedef std::map< String, PropertyDefinition* > PropertyMap;
  95. typedef std::map< String, PropertyShorthandDefinition* > ShorthandMap;
  96. PropertyMap properties;
  97. ShorthandMap shorthands;
  98. PropertyNameList property_names;
  99. PropertyNameList inherited_property_names;
  100. bool ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const;
  101. };
  102. }
  103. }
  104. #endif