PropertySpecification.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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/PropertySpecification.h"
  29. #include "PropertyShorthandDefinition.h"
  30. #include "../../Include/Rocket/Core/Log.h"
  31. #include "../../Include/Rocket/Core/PropertyDefinition.h"
  32. #include "../../Include/Rocket/Core/PropertyDictionary.h"
  33. #include "../../Include/Rocket/Core/StyleSheetSpecification.h"
  34. namespace Rocket {
  35. namespace Core {
  36. PropertySpecification::PropertySpecification()
  37. {
  38. }
  39. PropertySpecification::~PropertySpecification()
  40. {
  41. for (PropertyMap::iterator iterator = properties.begin(); iterator != properties.end(); ++iterator)
  42. delete (*iterator).second;
  43. for (ShorthandMap::iterator iterator = shorthands.begin(); iterator != shorthands.end(); ++iterator)
  44. delete (*iterator).second;
  45. }
  46. // Registers a property with a new definition.
  47. PropertyDefinition& PropertySpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout)
  48. {
  49. String lower_case_name = property_name.ToLower();
  50. // Create the property and validate the default value.
  51. PropertyDefinition* property_definition = new PropertyDefinition(default_value, inherited, forces_layout);
  52. // Delete any existing property.
  53. PropertyMap::iterator iterator = properties.find(lower_case_name);
  54. if (iterator != properties.end())
  55. {
  56. delete (*iterator).second;
  57. }
  58. else
  59. {
  60. property_names.insert(lower_case_name);
  61. if (inherited)
  62. {
  63. inherited_property_names.insert(lower_case_name);
  64. }
  65. }
  66. properties[lower_case_name] = property_definition;
  67. return *property_definition;
  68. }
  69. // Returns a property definition.
  70. const PropertyDefinition* PropertySpecification::GetProperty(const String& property_name) const
  71. {
  72. PropertyMap::const_iterator iterator = properties.find(property_name);
  73. if (iterator == properties.end())
  74. return NULL;
  75. return (*iterator).second;
  76. }
  77. // Fetches a list of the names of all registered property definitions.
  78. const PropertyNameList& PropertySpecification::GetRegisteredProperties(void) const
  79. {
  80. return property_names;
  81. }
  82. // Fetches a list of the names of all registered property definitions.
  83. const PropertyNameList& PropertySpecification::GetRegisteredInheritedProperties(void) const
  84. {
  85. return inherited_property_names;
  86. }
  87. // Registers a shorthand property definition.
  88. bool PropertySpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, ShorthandType type)
  89. {
  90. StringList properties;
  91. StringUtilities::ExpandString(properties, property_names.ToLower());
  92. if (properties.empty())
  93. return false;
  94. String lower_case_name = shorthand_name.ToLower();
  95. // Construct the new shorthand definition and resolve its properties.
  96. PropertyShorthandDefinition* property_shorthand = new PropertyShorthandDefinition();
  97. for (size_t i = 0; i < properties.size(); i++)
  98. {
  99. const PropertyDefinition* property = GetProperty(properties[i]);
  100. if (property == NULL)
  101. {
  102. Log::Message(Log::LT_ERROR, "Shorthand property '%s' was registered with invalid property '%s'.", shorthand_name.CString(), properties[i].CString());
  103. delete property_shorthand;
  104. return false;
  105. }
  106. property_shorthand->properties.push_back(PropertyShorthandDefinition::PropertyDefinitionList::value_type(properties[i], property));
  107. }
  108. if (type == AUTO)
  109. {
  110. if (properties.size() == 4 &&
  111. properties[0].Find("-top") != String::npos &&
  112. properties[1].Find("-right") != String::npos &&
  113. properties[2].Find("-bottom") != String::npos &&
  114. properties[3].Find("-left") != String::npos)
  115. property_shorthand->type = BOX;
  116. else
  117. property_shorthand->type = FALL_THROUGH;
  118. }
  119. else
  120. property_shorthand->type = type;
  121. shorthands[lower_case_name] = property_shorthand;
  122. return true;
  123. }
  124. // Returns a shorthand definition.
  125. const PropertyShorthandDefinition* PropertySpecification::GetShorthand(const String& shorthand_name) const
  126. {
  127. ShorthandMap::const_iterator iterator = shorthands.find(shorthand_name);
  128. if (iterator == shorthands.end())
  129. return NULL;
  130. return (*iterator).second;
  131. }
  132. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  133. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number) const
  134. {
  135. String lower_case_name = property_name.ToLower();
  136. // Attempt to parse as a single property.
  137. const PropertyDefinition* property_definition = GetProperty(lower_case_name);
  138. StringList property_values;
  139. if (!ParsePropertyValues(property_values, property_value, property_definition == NULL) || property_values.size() == 0)
  140. return false;
  141. if (property_definition != NULL)
  142. {
  143. Property new_property;
  144. new_property.source = source_file;
  145. new_property.source_line_number = source_line_number;
  146. if (property_definition->ParseValue(new_property, property_values[0]))
  147. {
  148. dictionary.SetProperty(lower_case_name, new_property);
  149. return true;
  150. }
  151. return false;
  152. }
  153. // Try as a shorthand.
  154. const PropertyShorthandDefinition* shorthand_definition = GetShorthand(lower_case_name);
  155. if (shorthand_definition != NULL)
  156. {
  157. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  158. // than four values
  159. if (shorthand_definition->type == BOX &&
  160. property_values.size() < 4)
  161. {
  162. switch (property_values.size())
  163. {
  164. // Only one value is defined, so it is parsed onto all four sides.
  165. case 1:
  166. {
  167. for (int i = 0; i < 4; i++)
  168. {
  169. Property new_property;
  170. if (!shorthand_definition->properties[i].second->ParseValue(new_property, property_values[0]))
  171. return false;
  172. new_property.source = source_file;
  173. new_property.source_line_number = source_line_number;
  174. dictionary.SetProperty(shorthand_definition->properties[i].first, new_property);
  175. }
  176. }
  177. break;
  178. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  179. // the left and right.
  180. case 2:
  181. {
  182. // Parse the first value into the top and bottom properties.
  183. Property new_property;
  184. new_property.source = source_file;
  185. new_property.source_line_number = source_line_number;
  186. if (!shorthand_definition->properties[0].second->ParseValue(new_property, property_values[0]))
  187. return false;
  188. dictionary.SetProperty(shorthand_definition->properties[0].first, new_property);
  189. if (!shorthand_definition->properties[2].second->ParseValue(new_property, property_values[0]))
  190. return false;
  191. dictionary.SetProperty(shorthand_definition->properties[2].first, new_property);
  192. // Parse the second value into the left and right properties.
  193. if (!shorthand_definition->properties[1].second->ParseValue(new_property, property_values[1]))
  194. return false;
  195. dictionary.SetProperty(shorthand_definition->properties[1].first, new_property);
  196. if (!shorthand_definition->properties[3].second->ParseValue(new_property, property_values[1]))
  197. return false;
  198. dictionary.SetProperty(shorthand_definition->properties[3].first, new_property);
  199. }
  200. break;
  201. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  202. // right, and the third onto the bottom.
  203. case 3:
  204. {
  205. // Parse the first value into the top property.
  206. Property new_property;
  207. new_property.source = source_file;
  208. new_property.source_line_number = source_line_number;
  209. if (!shorthand_definition->properties[0].second->ParseValue(new_property, property_values[0]))
  210. return false;
  211. dictionary.SetProperty(shorthand_definition->properties[0].first, new_property);
  212. // Parse the second value into the left and right properties.
  213. if (!shorthand_definition->properties[1].second->ParseValue(new_property, property_values[1]))
  214. return false;
  215. dictionary.SetProperty(shorthand_definition->properties[1].first, new_property);
  216. if (!shorthand_definition->properties[3].second->ParseValue(new_property, property_values[1]))
  217. return false;
  218. dictionary.SetProperty(shorthand_definition->properties[3].first, new_property);
  219. // Parse the third value into the bottom property.
  220. if (!shorthand_definition->properties[2].second->ParseValue(new_property, property_values[2]))
  221. return false;
  222. dictionary.SetProperty(shorthand_definition->properties[2].first, new_property);
  223. }
  224. break;
  225. default: break;
  226. }
  227. }
  228. else
  229. {
  230. size_t value_index = 0;
  231. size_t property_index = 0;
  232. for (; value_index < property_values.size() && property_index < shorthand_definition->properties.size(); property_index++)
  233. {
  234. Property new_property;
  235. new_property.source = source_file;
  236. new_property.source_line_number = source_line_number;
  237. if (!shorthand_definition->properties[property_index].second->ParseValue(new_property, property_values[value_index]))
  238. {
  239. // This definition failed to parse; if we're falling through, try the next property. If there is no
  240. // next property, then abort!
  241. if (shorthand_definition->type == FALL_THROUGH)
  242. {
  243. if (property_index + 1 < shorthand_definition->properties.size())
  244. continue;
  245. }
  246. return false;
  247. }
  248. dictionary.SetProperty(shorthand_definition->properties[property_index].first, new_property);
  249. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  250. if (shorthand_definition->type != REPLICATE ||
  251. value_index < property_values.size() - 1)
  252. value_index++;
  253. }
  254. }
  255. return true;
  256. }
  257. // Can't find it! Store as an unknown string value.
  258. Property new_property(property_value, Property::UNKNOWN);
  259. new_property.source = source_file;
  260. new_property.source_line_number = source_line_number;
  261. dictionary.SetProperty(lower_case_name, new_property);
  262. return true;
  263. }
  264. // Sets all undefined properties in the dictionary to their defaults.
  265. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  266. {
  267. for (PropertyMap::const_iterator i = properties.begin(); i != properties.end(); ++i)
  268. {
  269. if (dictionary.GetProperty((*i).first) == NULL)
  270. dictionary.SetProperty((*i).first, *(*i).second->GetDefaultValue());
  271. }
  272. }
  273. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  274. {
  275. String value;
  276. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  277. ParseState state = VALUE;
  278. int open_parentheses = 0;
  279. size_t character_index = 0;
  280. char previous_character = 0;
  281. while (character_index < values.Length())
  282. {
  283. char character = values[character_index];
  284. character_index++;
  285. switch (state)
  286. {
  287. case VALUE:
  288. {
  289. if (character == ';')
  290. {
  291. value = StringUtilities::StripWhitespace(value);
  292. if (value.Length() > 0)
  293. {
  294. values_list.push_back(value);
  295. value.Clear();
  296. }
  297. }
  298. else if (StringUtilities::IsWhitespace(character))
  299. {
  300. if (split_values)
  301. {
  302. value = StringUtilities::StripWhitespace(value);
  303. if (value.Length() > 0)
  304. {
  305. values_list.push_back(value);
  306. value.Clear();
  307. }
  308. }
  309. else
  310. value.Append(character);
  311. }
  312. else if (character == '"')
  313. {
  314. if (split_values)
  315. {
  316. value = StringUtilities::StripWhitespace(value);
  317. if (value.Length() > 0)
  318. {
  319. values_list.push_back(value);
  320. value.Clear();
  321. }
  322. state = VALUE_QUOTE;
  323. }
  324. else
  325. {
  326. value.Append(' ');
  327. state = VALUE_QUOTE;
  328. }
  329. }
  330. else if (character == '(')
  331. {
  332. open_parentheses = 1;
  333. value.Append(character);
  334. state = VALUE_PARENTHESIS;
  335. }
  336. else
  337. {
  338. value.Append(character);
  339. }
  340. }
  341. break;
  342. case VALUE_PARENTHESIS:
  343. {
  344. if (previous_character == '/')
  345. {
  346. if (character == ')' || character == '(')
  347. value.Append(character);
  348. else
  349. {
  350. value.Append('/');
  351. value.Append(character);
  352. }
  353. }
  354. else
  355. {
  356. if (character == '(')
  357. {
  358. open_parentheses++;
  359. value.Append(character);
  360. }
  361. else if (character == ')')
  362. {
  363. open_parentheses--;
  364. value.Append(character);
  365. if (open_parentheses == 0)
  366. state = VALUE;
  367. }
  368. else if (character != '/')
  369. {
  370. value.Append(character);
  371. }
  372. }
  373. }
  374. break;
  375. case VALUE_QUOTE:
  376. {
  377. if (previous_character == '/')
  378. {
  379. if (character == '"')
  380. value.Append(character);
  381. else
  382. {
  383. value.Append('/');
  384. value.Append(character);
  385. }
  386. }
  387. else
  388. {
  389. if (character == '"')
  390. {
  391. if (split_values)
  392. {
  393. value = StringUtilities::StripWhitespace(value);
  394. if (value.Length() > 0)
  395. {
  396. values_list.push_back(value);
  397. value.Clear();
  398. }
  399. }
  400. else
  401. value.Append(' ');
  402. state = VALUE;
  403. }
  404. else if (character != '/')
  405. {
  406. value.Append(character);
  407. }
  408. }
  409. }
  410. }
  411. previous_character = character;
  412. }
  413. if (state == VALUE)
  414. {
  415. value = StringUtilities::StripWhitespace(value);
  416. if (value.Length() > 0)
  417. values_list.push_back(value);
  418. }
  419. return true;
  420. }
  421. }
  422. }