PropertySpecification.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 = ToLower(property_name);
  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, ToLower(property_names));
  92. if (properties.empty())
  93. return false;
  94. String lower_case_name = ToLower(shorthand_name);
  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. bool shorthand_found = false;
  101. if (property == NULL && type == RECURSIVE)
  102. {
  103. // See if recursive type points to another shorthand instead
  104. const PropertyShorthandDefinition* shorthand = GetShorthand(properties[i]);
  105. shorthand_found = (shorthand != NULL);
  106. }
  107. if (property == NULL && !shorthand_found)
  108. {
  109. Log::Message(Log::LT_ERROR, "Shorthand property '%s' was registered with invalid property '%s'.", shorthand_name.c_str(), properties[i].c_str());
  110. delete property_shorthand;
  111. return false;
  112. }
  113. property_shorthand->properties.push_back(PropertyShorthandDefinition::PropertyDefinitionList::value_type(properties[i], property));
  114. }
  115. if (type == AUTO)
  116. {
  117. if (properties.size() == 4 &&
  118. properties[0].find("-top") != String::npos &&
  119. properties[1].find("-right") != String::npos &&
  120. properties[2].find("-bottom") != String::npos &&
  121. properties[3].find("-left") != String::npos)
  122. property_shorthand->type = BOX;
  123. else
  124. property_shorthand->type = FALL_THROUGH;
  125. }
  126. else
  127. property_shorthand->type = type;
  128. shorthands[lower_case_name] = property_shorthand;
  129. return true;
  130. }
  131. // Returns a shorthand definition.
  132. const PropertyShorthandDefinition* PropertySpecification::GetShorthand(const String& shorthand_name) const
  133. {
  134. ShorthandMap::const_iterator iterator = shorthands.find(shorthand_name);
  135. if (iterator == shorthands.end())
  136. return NULL;
  137. return (*iterator).second;
  138. }
  139. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  140. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number) const
  141. {
  142. String lower_case_name = ToLower(property_name);
  143. // Attempt to parse as a single property.
  144. const PropertyDefinition* property_definition = GetProperty(lower_case_name);
  145. StringList property_values;
  146. if (!ParsePropertyValues(property_values, property_value, property_definition == NULL) || property_values.size() == 0)
  147. return false;
  148. if (property_definition != NULL)
  149. {
  150. Property new_property;
  151. new_property.source = source_file;
  152. new_property.source_line_number = source_line_number;
  153. if (property_definition->ParseValue(new_property, property_values[0]))
  154. {
  155. dictionary.SetProperty(lower_case_name, new_property);
  156. return true;
  157. }
  158. return false;
  159. }
  160. // Try as a shorthand.
  161. const PropertyShorthandDefinition* shorthand_definition = GetShorthand(lower_case_name);
  162. if (shorthand_definition != NULL)
  163. {
  164. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  165. // than four values
  166. if (shorthand_definition->type == BOX &&
  167. property_values.size() < 4)
  168. {
  169. switch (property_values.size())
  170. {
  171. // Only one value is defined, so it is parsed onto all four sides.
  172. case 1:
  173. {
  174. for (int i = 0; i < 4; i++)
  175. {
  176. Property new_property;
  177. if (!shorthand_definition->properties[i].second->ParseValue(new_property, property_values[0]))
  178. return false;
  179. new_property.source = source_file;
  180. new_property.source_line_number = source_line_number;
  181. dictionary.SetProperty(shorthand_definition->properties[i].first, new_property);
  182. }
  183. }
  184. break;
  185. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  186. // the left and right.
  187. case 2:
  188. {
  189. // Parse the first value into the top and bottom properties.
  190. Property new_property;
  191. new_property.source = source_file;
  192. new_property.source_line_number = source_line_number;
  193. if (!shorthand_definition->properties[0].second->ParseValue(new_property, property_values[0]))
  194. return false;
  195. dictionary.SetProperty(shorthand_definition->properties[0].first, new_property);
  196. if (!shorthand_definition->properties[2].second->ParseValue(new_property, property_values[0]))
  197. return false;
  198. dictionary.SetProperty(shorthand_definition->properties[2].first, new_property);
  199. // Parse the second value into the left and right properties.
  200. if (!shorthand_definition->properties[1].second->ParseValue(new_property, property_values[1]))
  201. return false;
  202. dictionary.SetProperty(shorthand_definition->properties[1].first, new_property);
  203. if (!shorthand_definition->properties[3].second->ParseValue(new_property, property_values[1]))
  204. return false;
  205. dictionary.SetProperty(shorthand_definition->properties[3].first, new_property);
  206. }
  207. break;
  208. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  209. // right, and the third onto the bottom.
  210. case 3:
  211. {
  212. // Parse the first value into the top property.
  213. Property new_property;
  214. new_property.source = source_file;
  215. new_property.source_line_number = source_line_number;
  216. if (!shorthand_definition->properties[0].second->ParseValue(new_property, property_values[0]))
  217. return false;
  218. dictionary.SetProperty(shorthand_definition->properties[0].first, new_property);
  219. // Parse the second value into the left and right properties.
  220. if (!shorthand_definition->properties[1].second->ParseValue(new_property, property_values[1]))
  221. return false;
  222. dictionary.SetProperty(shorthand_definition->properties[1].first, new_property);
  223. if (!shorthand_definition->properties[3].second->ParseValue(new_property, property_values[1]))
  224. return false;
  225. dictionary.SetProperty(shorthand_definition->properties[3].first, new_property);
  226. // Parse the third value into the bottom property.
  227. if (!shorthand_definition->properties[2].second->ParseValue(new_property, property_values[2]))
  228. return false;
  229. dictionary.SetProperty(shorthand_definition->properties[2].first, new_property);
  230. }
  231. break;
  232. default: break;
  233. }
  234. }
  235. else if (shorthand_definition->type == RECURSIVE)
  236. {
  237. bool result = false;
  238. for (size_t i = 0; i < shorthand_definition->properties.size(); i++)
  239. {
  240. const auto& property_name = shorthand_definition->properties[i].first;
  241. result |= ParsePropertyDeclaration(dictionary, property_name, property_value, source_file, source_line_number);
  242. }
  243. if (!result)
  244. return false;
  245. }
  246. else
  247. {
  248. size_t value_index = 0;
  249. size_t property_index = 0;
  250. for (; value_index < property_values.size() && property_index < shorthand_definition->properties.size(); property_index++)
  251. {
  252. Property new_property;
  253. new_property.source = source_file;
  254. new_property.source_line_number = source_line_number;
  255. if (!shorthand_definition->properties[property_index].second->ParseValue(new_property, property_values[value_index]))
  256. {
  257. // This definition failed to parse; if we're falling through, try the next property. If there is no
  258. // next property, then abort!
  259. if (shorthand_definition->type == FALL_THROUGH)
  260. {
  261. if (property_index + 1 < shorthand_definition->properties.size())
  262. continue;
  263. }
  264. return false;
  265. }
  266. dictionary.SetProperty(shorthand_definition->properties[property_index].first, new_property);
  267. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  268. if (shorthand_definition->type != REPLICATE ||
  269. value_index < property_values.size() - 1)
  270. value_index++;
  271. }
  272. }
  273. return true;
  274. }
  275. // Can't find it! Store as an unknown string value.
  276. Property new_property(property_value, Property::UNKNOWN);
  277. new_property.source = source_file;
  278. new_property.source_line_number = source_line_number;
  279. dictionary.SetProperty(lower_case_name, new_property);
  280. return true;
  281. }
  282. // Sets all undefined properties in the dictionary to their defaults.
  283. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  284. {
  285. for (PropertyMap::const_iterator i = properties.begin(); i != properties.end(); ++i)
  286. {
  287. if (dictionary.GetProperty((*i).first) == NULL)
  288. dictionary.SetProperty((*i).first, *(*i).second->GetDefaultValue());
  289. }
  290. }
  291. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  292. {
  293. String value;
  294. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  295. ParseState state = VALUE;
  296. int open_parentheses = 0;
  297. size_t character_index = 0;
  298. char previous_character = 0;
  299. while (character_index < values.size())
  300. {
  301. char character = values[character_index];
  302. character_index++;
  303. switch (state)
  304. {
  305. case VALUE:
  306. {
  307. if (character == ';')
  308. {
  309. value = StringUtilities::StripWhitespace(value);
  310. if (value.size() > 0)
  311. {
  312. values_list.push_back(value);
  313. value.clear();
  314. }
  315. }
  316. else if (StringUtilities::IsWhitespace(character))
  317. {
  318. if (split_values)
  319. {
  320. value = StringUtilities::StripWhitespace(value);
  321. if (value.size() > 0)
  322. {
  323. values_list.push_back(value);
  324. value.clear();
  325. }
  326. }
  327. else
  328. value += character;
  329. }
  330. else if (character == '"')
  331. {
  332. if (split_values)
  333. {
  334. value = StringUtilities::StripWhitespace(value);
  335. if (value.size() > 0)
  336. {
  337. values_list.push_back(value);
  338. value.clear();
  339. }
  340. state = VALUE_QUOTE;
  341. }
  342. else
  343. {
  344. value += ' ';
  345. state = VALUE_QUOTE;
  346. }
  347. }
  348. else if (character == '(')
  349. {
  350. open_parentheses = 1;
  351. value += character;
  352. state = VALUE_PARENTHESIS;
  353. }
  354. else
  355. {
  356. value += character;
  357. }
  358. }
  359. break;
  360. case VALUE_PARENTHESIS:
  361. {
  362. if (previous_character == '/')
  363. {
  364. if (character == ')' || character == '(')
  365. value += character;
  366. else
  367. {
  368. value += '/';
  369. value += character;
  370. }
  371. }
  372. else
  373. {
  374. if (character == '(')
  375. {
  376. open_parentheses++;
  377. value += character;
  378. }
  379. else if (character == ')')
  380. {
  381. open_parentheses--;
  382. value += character;
  383. if (open_parentheses == 0)
  384. state = VALUE;
  385. }
  386. else if (character != '/')
  387. {
  388. value += character;
  389. }
  390. }
  391. }
  392. break;
  393. case VALUE_QUOTE:
  394. {
  395. if (previous_character == '/')
  396. {
  397. if (character == '"')
  398. value += character;
  399. else
  400. {
  401. value += '/';
  402. value += character;
  403. }
  404. }
  405. else
  406. {
  407. if (character == '"')
  408. {
  409. if (split_values)
  410. {
  411. value = StringUtilities::StripWhitespace(value);
  412. if (value.size() > 0)
  413. {
  414. values_list.push_back(value);
  415. value.clear();
  416. }
  417. }
  418. else
  419. value += ' ';
  420. state = VALUE;
  421. }
  422. else if (character != '/')
  423. {
  424. value += character;
  425. }
  426. }
  427. }
  428. }
  429. previous_character = character;
  430. }
  431. if (state == VALUE)
  432. {
  433. value = StringUtilities::StripWhitespace(value);
  434. if (value.size() > 0)
  435. values_list.push_back(value);
  436. }
  437. return true;
  438. }
  439. }
  440. }