StyleSheetSpecification.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "../../Include/Rocket/Core/StyleSheetSpecification.h"
  29. #include "PropertyParserNumber.h"
  30. #include "PropertyParserColour.h"
  31. #include "PropertyParserKeyword.h"
  32. #include "PropertyParserString.h"
  33. #include "PropertyParserTransform.h"
  34. namespace Rocket {
  35. namespace Core {
  36. static StyleSheetSpecification* instance = NULL;
  37. StyleSheetSpecification::StyleSheetSpecification()
  38. {
  39. ROCKET_ASSERT(instance == NULL);
  40. instance = this;
  41. }
  42. StyleSheetSpecification::~StyleSheetSpecification()
  43. {
  44. ROCKET_ASSERT(instance == this);
  45. instance = NULL;
  46. }
  47. bool StyleSheetSpecification::Initialise()
  48. {
  49. if (instance == NULL)
  50. {
  51. new StyleSheetSpecification();
  52. instance->RegisterDefaultParsers();
  53. instance->RegisterDefaultProperties();
  54. }
  55. return true;
  56. }
  57. void StyleSheetSpecification::Shutdown()
  58. {
  59. if (instance != NULL)
  60. {
  61. for (ParserMap::iterator iterator = instance->parsers.begin(); iterator != instance->parsers.end(); iterator++)
  62. (*iterator).second->Release();
  63. delete instance;
  64. }
  65. }
  66. // Registers a parser for use in property definitions.
  67. bool StyleSheetSpecification::RegisterParser(const String& parser_name, PropertyParser* parser)
  68. {
  69. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  70. if (iterator != instance->parsers.end())
  71. (*iterator).second->Release();
  72. instance->parsers[parser_name] = parser;
  73. return true;
  74. }
  75. // Returns the parser registered with a specific name.
  76. PropertyParser* StyleSheetSpecification::GetParser(const String& parser_name)
  77. {
  78. ParserMap::iterator iterator = instance->parsers.find(parser_name);
  79. if (iterator == instance->parsers.end())
  80. return NULL;
  81. return (*iterator).second;
  82. }
  83. // Registers a property with a new definition.
  84. PropertyDefinition& StyleSheetSpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout)
  85. {
  86. return instance->properties.RegisterProperty(property_name, default_value, inherited, forces_layout);
  87. }
  88. // Returns a property definition.
  89. const PropertyDefinition* StyleSheetSpecification::GetProperty(const String& property_name)
  90. {
  91. return instance->properties.GetProperty(property_name);
  92. }
  93. // Fetches a list of the names of all registered property definitions.
  94. const PropertyNameList& StyleSheetSpecification::GetRegisteredProperties()
  95. {
  96. return instance->properties.GetRegisteredProperties();
  97. }
  98. const PropertyNameList & StyleSheetSpecification::GetRegisteredInheritedProperties()
  99. {
  100. return instance->properties.GetRegisteredInheritedProperties();
  101. }
  102. // Registers a shorthand property definition.
  103. bool StyleSheetSpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, PropertySpecification::ShorthandType type)
  104. {
  105. return instance->properties.RegisterShorthand(shorthand_name, property_names, type);
  106. }
  107. // Returns a shorthand definition.
  108. const PropertyShorthandDefinition* StyleSheetSpecification::GetShorthand(const String& shorthand_name)
  109. {
  110. return instance->properties.GetShorthand(shorthand_name);
  111. }
  112. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  113. bool StyleSheetSpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number)
  114. {
  115. return instance->properties.ParsePropertyDeclaration(dictionary, property_name, property_value, source_file, source_line_number);
  116. }
  117. // Registers Rocket's default parsers.
  118. void StyleSheetSpecification::RegisterDefaultParsers()
  119. {
  120. RegisterParser("number", new PropertyParserNumber(Property::NUMBER));
  121. RegisterParser("length", new PropertyParserNumber(Property::LENGTH, Property::PX));
  122. RegisterParser("length_percent", new PropertyParserNumber(Property::LENGTH_PERCENT, Property::PX));
  123. RegisterParser("number_length_percent", new PropertyParserNumber(Property::NUMBER_LENGTH_PERCENT, Property::PX));
  124. RegisterParser("angle", new PropertyParserNumber(Property::ANGLE, Property::RAD));
  125. RegisterParser("keyword", new PropertyParserKeyword());
  126. RegisterParser("string", new PropertyParserString());
  127. RegisterParser(COLOR, new PropertyParserColour());
  128. RegisterParser(TRANSFORM, new PropertyParserTransform());
  129. }
  130. // Registers Rocket's default style properties.
  131. void StyleSheetSpecification::RegisterDefaultProperties()
  132. {
  133. // Style property specifications (ala RCSS).
  134. RegisterProperty(MARGIN_TOP, "0px", false, true)
  135. .AddParser("keyword", "auto")
  136. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  137. RegisterProperty(MARGIN_RIGHT, "0px", false, true)
  138. .AddParser("keyword", "auto")
  139. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  140. RegisterProperty(MARGIN_BOTTOM, "0px", false, true)
  141. .AddParser("keyword", "auto")
  142. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  143. RegisterProperty(MARGIN_LEFT, "0px", false, true)
  144. .AddParser("keyword", "auto")
  145. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  146. RegisterShorthand(MARGIN, "margin-top, margin-right, margin-bottom, margin-left");
  147. RegisterProperty(PADDING_TOP, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  148. RegisterProperty(PADDING_RIGHT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  149. RegisterProperty(PADDING_BOTTOM, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  150. RegisterProperty(PADDING_LEFT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  151. RegisterShorthand(PADDING, "padding-top, padding-right, padding-bottom, padding-left");
  152. RegisterProperty(BORDER_TOP_WIDTH, "0px", false, true).AddParser("length");
  153. RegisterProperty(BORDER_RIGHT_WIDTH, "0px", false, true).AddParser("length");
  154. RegisterProperty(BORDER_BOTTOM_WIDTH, "0px", false, true).AddParser("length");
  155. RegisterProperty(BORDER_LEFT_WIDTH, "0px", false, true).AddParser("length");
  156. RegisterShorthand(BORDER_WIDTH, "border-top-width, border-right-width, border-bottom-width, border-left-width");
  157. RegisterProperty(BORDER_TOP_COLOR, "black", false, false).AddParser(COLOR);
  158. RegisterProperty(BORDER_RIGHT_COLOR, "black", false, false).AddParser(COLOR);
  159. RegisterProperty(BORDER_BOTTOM_COLOR, "black", false, false).AddParser(COLOR);
  160. RegisterProperty(BORDER_LEFT_COLOR, "black", false, false).AddParser(COLOR);
  161. RegisterShorthand(BORDER_COLOR, "border-top-color, border-right-color, border-bottom-color, border-left-color");
  162. RegisterShorthand(BORDER_TOP, "border-top-width, border-top-color");
  163. RegisterShorthand(BORDER_RIGHT, "border-right-width, border-right-color");
  164. RegisterShorthand(BORDER_BOTTOM, "border-bottom-width, border-bottom-color");
  165. RegisterShorthand(BORDER_LEFT, "border-left-width, border-left-color");
  166. RegisterShorthand(BORDER, "border-top, border-right, border-bottom, border-left", PropertySpecification::RECURSIVE);
  167. RegisterProperty(DISPLAY, "inline", false, true).AddParser("keyword", "none, block, inline, inline-block");
  168. RegisterProperty(POSITION, "static", false, true).AddParser("keyword", "static, relative, absolute, fixed");
  169. RegisterProperty(TOP, "auto", false, false)
  170. .AddParser("keyword", "auto")
  171. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  172. RegisterProperty(RIGHT, "auto", false, false)
  173. .AddParser("keyword", "auto")
  174. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  175. RegisterProperty(BOTTOM, "auto", false, false)
  176. .AddParser("keyword", "auto")
  177. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  178. RegisterProperty(LEFT, "auto", false, false)
  179. .AddParser("keyword", "auto")
  180. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  181. RegisterProperty(FLOAT, "none", false, true).AddParser("keyword", "none, left, right");
  182. RegisterProperty(CLEAR, "none", false, true).AddParser("keyword", "none, left, right, both");
  183. RegisterProperty(Z_INDEX, "auto", false, false)
  184. .AddParser("keyword", "auto, top, bottom")
  185. .AddParser("number");
  186. RegisterProperty(WIDTH, "auto", false, true)
  187. .AddParser("keyword", "auto")
  188. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  189. RegisterProperty(MIN_WIDTH, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  190. RegisterProperty(MAX_WIDTH, "-1px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockWidth);
  191. RegisterProperty(HEIGHT, "auto", false, true)
  192. .AddParser("keyword", "auto")
  193. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  194. RegisterProperty(MIN_HEIGHT, "0px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  195. RegisterProperty(MAX_HEIGHT, "-1px", false, true).AddParser("length_percent").SetRelativeTarget(RelativeTarget::ContainingBlockHeight);
  196. RegisterProperty(LINE_HEIGHT, "1.2", true, true).AddParser("number_length_percent").SetRelativeTarget(RelativeTarget::FontSize);
  197. RegisterProperty(VERTICAL_ALIGN, "baseline", false, true)
  198. .AddParser("keyword", "baseline, middle, sub, super, text-top, text-bottom, top, bottom")
  199. .AddParser("length_percent").SetRelativeTarget(RelativeTarget::LineHeight);
  200. RegisterProperty(OVERFLOW_X, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  201. RegisterProperty(OVERFLOW_Y, "visible", false, true).AddParser("keyword", "visible, hidden, auto, scroll");
  202. RegisterShorthand("overflow", "overflow-x, overflow-y", PropertySpecification::REPLICATE);
  203. RegisterProperty(CLIP, "auto", true, false).AddParser("keyword", "auto, none").AddParser("number");
  204. RegisterProperty(VISIBILITY, "visible", false, false).AddParser("keyword", "visible, hidden");
  205. // Need some work on this if we are to include images.
  206. RegisterProperty(BACKGROUND_COLOR, "transparent", false, false).AddParser(COLOR);
  207. RegisterShorthand(BACKGROUND, BACKGROUND_COLOR);
  208. RegisterProperty(COLOR, "white", true, false).AddParser(COLOR);
  209. RegisterProperty(IMAGE_COLOR, "white", false, false).AddParser(COLOR);
  210. RegisterProperty(OPACITY, "1", true, false).AddParser("number");
  211. RegisterProperty(FONT_FAMILY, "", true, true).AddParser("string");
  212. RegisterProperty(FONT_CHARSET, "U+0020-007E", true, false).AddParser("string");
  213. RegisterProperty(FONT_STYLE, "normal", true, true).AddParser("keyword", "normal, italic");
  214. RegisterProperty(FONT_WEIGHT, "normal", true, true).AddParser("keyword", "normal, bold");
  215. RegisterProperty(FONT_SIZE, "12px", true, true).AddParser("length").AddParser("length_percent").SetRelativeTarget(RelativeTarget::ParentFontSize);
  216. RegisterShorthand(FONT, "font-style, font-weight, font-size, font-family, font-charset");
  217. RegisterProperty(TEXT_ALIGN, LEFT, true, true).AddParser("keyword", "left, right, center, justify");
  218. RegisterProperty(TEXT_DECORATION, "none", true, false).AddParser("keyword", "none, underline"/*"none, underline, overline, line-through"*/);
  219. RegisterProperty(TEXT_TRANSFORM, "none", true, true).AddParser("keyword", "none, capitalize, uppercase, lowercase");
  220. RegisterProperty(WHITE_SPACE, "normal", true, true).AddParser("keyword", "normal, pre, nowrap, pre-wrap, pre-line");
  221. RegisterProperty(CURSOR, "auto", true, false).AddParser("keyword", "auto").AddParser("string");
  222. // Functional property specifications.
  223. RegisterProperty(DRAG, "none", false, false).AddParser("keyword", "none, drag, drag-drop, block, clone");
  224. RegisterProperty(TAB_INDEX, "none", false, false).AddParser("keyword", "none, auto");
  225. RegisterProperty(FOCUS, "auto", true, false).AddParser("keyword", "none, auto");
  226. RegisterProperty(SCROLLBAR_MARGIN, "0", false, false).AddParser("length");
  227. RegisterProperty(POINTER_EVENTS, "auto", true, false).AddParser("keyword", "auto, none");
  228. // Perspective and Transform specifications
  229. RegisterProperty(PERSPECTIVE, "none", false, false).AddParser("keyword", "none").AddParser("length");
  230. RegisterProperty(PERSPECTIVE_ORIGIN_X, "50%", false, false).AddParser("keyword", "left, center, right").AddParser("length_percent");
  231. RegisterProperty(PERSPECTIVE_ORIGIN_Y, "50%", false, false).AddParser("keyword", "top, center, bottom").AddParser("length_percent");
  232. RegisterShorthand(PERSPECTIVE_ORIGIN, "perspective-origin-x, perspective-origin-y");
  233. RegisterProperty(TRANSFORM, "none", false, false).AddParser("keyword", "none").AddParser(TRANSFORM);
  234. RegisterProperty(TRANSFORM_ORIGIN_X, "50%", false, false).AddParser("keyword", "left, center, right").AddParser("length_percent");
  235. RegisterProperty(TRANSFORM_ORIGIN_Y, "50%", false, false).AddParser("keyword", "top, center, bottom").AddParser("length_percent");
  236. RegisterProperty(TRANSFORM_ORIGIN_Z, "0", false, false).AddParser("length");
  237. RegisterShorthand(TRANSFORM_ORIGIN, "transform-origin-x, transform-origin-y, transform-origin-z");
  238. }
  239. }
  240. }