StyleSheetSpecification.cpp 14 KB

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