PropertySpecification.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. namespace Rocket {
  34. namespace Core {
  35. PropertySpecification::PropertySpecification(size_t reserve_num_properties, size_t reserve_num_shorthands)
  36. : properties(reserve_num_properties, nullptr), shorthands(reserve_num_shorthands, nullptr), property_map(reserve_num_properties), shorthand_map(reserve_num_shorthands)
  37. {
  38. }
  39. PropertySpecification::~PropertySpecification()
  40. {
  41. for (PropertyDefinition* p : properties)
  42. delete p;
  43. for (ShorthandDefinition* s : shorthands)
  44. delete s;
  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, PropertyId id)
  48. {
  49. if (id == PropertyId::Invalid)
  50. id = property_map.GetOrCreateId(property_name);
  51. else
  52. property_map.AddPair(id, property_name);
  53. size_t index = (size_t)id;
  54. ROCKET_ASSERT(index < (size_t)INT16_MAX);
  55. if (index < properties.size())
  56. {
  57. // We don't want to owerwrite an existing entry.
  58. if (properties[index])
  59. {
  60. Log::Message(Log::LT_ERROR, "While registering property '%s': The property is already registered, ignoring.", property_name.c_str());
  61. return *properties[index];
  62. }
  63. }
  64. else
  65. {
  66. // Resize vector to hold the new index
  67. properties.resize((index*3)/2 + 1, nullptr);
  68. }
  69. // Create and insert the new property
  70. PropertyDefinition* property_definition = new PropertyDefinition(id, default_value, inherited, forces_layout);
  71. properties[index] = property_definition;
  72. property_names.insert(id);
  73. if (inherited)
  74. inherited_property_names.insert(id);
  75. return *property_definition;
  76. }
  77. // Returns a property definition.
  78. const PropertyDefinition* PropertySpecification::GetProperty(PropertyId id) const
  79. {
  80. if (id == PropertyId::Invalid || (size_t)id >= properties.size())
  81. return nullptr;
  82. return properties[(size_t)id];
  83. }
  84. const PropertyDefinition* PropertySpecification::GetProperty(const String& property_name) const
  85. {
  86. return GetProperty(property_map.GetId(property_name));
  87. }
  88. // Fetches a list of the names of all registered property definitions.
  89. const PropertyNameList& PropertySpecification::GetRegisteredProperties(void) const
  90. {
  91. return property_names;
  92. }
  93. // Fetches a list of the names of all registered property definitions.
  94. const PropertyNameList& PropertySpecification::GetRegisteredInheritedProperties(void) const
  95. {
  96. return inherited_property_names;
  97. }
  98. // Registers a shorthand property definition.
  99. bool PropertySpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, ShorthandType type, ShorthandId id)
  100. {
  101. if (id == ShorthandId::Invalid)
  102. id = shorthand_map.GetOrCreateId(shorthand_name);
  103. else
  104. shorthand_map.AddPair(id, shorthand_name);
  105. StringList property_list;
  106. StringUtilities::ExpandString(property_list, ToLower(property_names));
  107. ShorthandItemIdList id_list;
  108. id_list.reserve(property_list.size());
  109. for (auto& name : property_list)
  110. {
  111. PropertyId property_id = property_map.GetId(name);
  112. if (property_id != PropertyId::Invalid)
  113. id_list.emplace_back(property_id);
  114. else
  115. {
  116. ShorthandId shorthand_id = shorthand_map.GetId(name);
  117. if (shorthand_id != ShorthandId::Invalid)
  118. id_list.emplace_back(shorthand_id);
  119. }
  120. }
  121. if (id_list.empty() || id_list.size() != property_list.size())
  122. return false;
  123. if (id_list.empty())
  124. return false;
  125. // Construct the new shorthand definition and resolve its properties.
  126. std::unique_ptr<ShorthandDefinition> property_shorthand(new ShorthandDefinition());
  127. for (size_t i = 0; i < id_list.size(); i++)
  128. {
  129. ShorthandItem item;
  130. if (id_list[i].type == ShorthandItemType::Property)
  131. {
  132. const PropertyDefinition* property = GetProperty(id_list[i].property_id);
  133. if(property)
  134. item = ShorthandItem(id_list[i].property_id, property);
  135. }
  136. else if (id_list[i].type == ShorthandItemType::Shorthand && type == ShorthandType::Recursive)
  137. {
  138. // The recursive type (and only that) can hold other shorthands
  139. const ShorthandDefinition* shorthand = GetShorthand(id_list[i].shorthand_id);
  140. if (shorthand)
  141. item = ShorthandItem(id_list[i].shorthand_id, shorthand);
  142. }
  143. if (item.type == ShorthandItemType::Invalid)
  144. {
  145. Log::Message(Log::LT_ERROR, "Shorthand property '%s' was registered with invalid property '%s'.", shorthand_name.c_str(), property_list[i].c_str());
  146. return false;
  147. }
  148. property_shorthand->items.push_back(item);
  149. }
  150. property_shorthand->id = id;
  151. property_shorthand->type = type;
  152. const size_t index = (size_t)id;
  153. ROCKET_ASSERT(index < (size_t)INT16_MAX);
  154. if (index < shorthands.size())
  155. {
  156. // We don't want to owerwrite an existing entry.
  157. if (shorthands[index])
  158. {
  159. Log::Message(Log::LT_ERROR, "The shorthand '%s' already exists, ignoring.", shorthand_name.c_str());
  160. return false;
  161. }
  162. }
  163. else
  164. {
  165. // Resize vector to hold the new index
  166. shorthands.resize((index * 3) / 2 + 1, nullptr);
  167. }
  168. shorthands[index] = property_shorthand.release();
  169. return true;
  170. }
  171. // Returns a shorthand definition.
  172. const ShorthandDefinition* PropertySpecification::GetShorthand(ShorthandId id) const
  173. {
  174. if (id == ShorthandId::Invalid || (size_t)id >= shorthands.size())
  175. return nullptr;
  176. return shorthands[(size_t)id];
  177. }
  178. const ShorthandDefinition* PropertySpecification::GetShorthand(const String& shorthand_name) const
  179. {
  180. return GetShorthand(shorthand_map.GetId(shorthand_name));
  181. }
  182. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value, const String& source_file, int source_line_number) const
  183. {
  184. // Try as a property first
  185. PropertyId property_id = property_map.GetId(property_name);
  186. if (property_id != PropertyId::Invalid)
  187. return ParsePropertyDeclaration(dictionary, property_id, property_value, source_file, source_line_number);
  188. // Then, as a shorthand
  189. ShorthandId shorthand_id = shorthand_map.GetId(property_name);
  190. if (shorthand_id != ShorthandId::Invalid)
  191. return ParseShorthandDeclaration(dictionary, shorthand_id, property_value, source_file, source_line_number);
  192. return false;
  193. }
  194. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, PropertyId property_id, const String& property_value, const String& source_file, int source_line_number) const
  195. {
  196. // Parse as a single property.
  197. const PropertyDefinition* property_definition = GetProperty(property_id);
  198. if (!property_definition)
  199. return false;
  200. StringList property_values;
  201. if (!ParsePropertyValues(property_values, property_value, false) || property_values.size() == 0)
  202. return false;
  203. Property new_property;
  204. new_property.source = source_file;
  205. new_property.source_line_number = source_line_number;
  206. if (!property_definition->ParseValue(new_property, property_values[0]))
  207. return false;
  208. dictionary.SetProperty(property_id, new_property);
  209. return true;
  210. }
  211. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  212. bool PropertySpecification::ParseShorthandDeclaration(PropertyDictionary& dictionary, ShorthandId shorthand_id, const String& property_value, const String& source_file, int source_line_number) const
  213. {
  214. StringList property_values;
  215. if (!ParsePropertyValues(property_values, property_value, true) || property_values.size() == 0)
  216. return false;
  217. // Parse as a shorthand.
  218. const ShorthandDefinition* shorthand_definition = GetShorthand(shorthand_id);
  219. if (!shorthand_definition)
  220. return false;
  221. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  222. // than four values
  223. if (shorthand_definition->type == ShorthandType::Box &&
  224. property_values.size() < 4)
  225. {
  226. // This array tells which property index each side is parsed from
  227. std::array<int, 4> box_side_to_value_index = { 0,0,0,0 };
  228. switch (property_values.size())
  229. {
  230. case 1:
  231. // Only one value is defined, so it is parsed onto all four sides.
  232. box_side_to_value_index = { 0,0,0,0 };
  233. break;
  234. case 2:
  235. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  236. // the left and right.
  237. box_side_to_value_index = { 0,1,0,1 };
  238. break;
  239. case 3:
  240. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  241. // right, and the third onto the bottom.
  242. box_side_to_value_index = { 0,1,2,1 };
  243. break;
  244. default:
  245. ROCKET_ERROR;
  246. break;
  247. }
  248. for (int i = 0; i < 4; i++)
  249. {
  250. ROCKET_ASSERT(shorthand_definition->items[i].type == ShorthandItemType::Property);
  251. Property new_property;
  252. int value_index = box_side_to_value_index[i];
  253. if (!shorthand_definition->items[i].property_definition->ParseValue(new_property, property_values[value_index]))
  254. return false;
  255. new_property.source = source_file;
  256. new_property.source_line_number = source_line_number;
  257. dictionary.SetProperty(shorthand_definition->items[i].property_definition->GetId(), new_property);
  258. }
  259. }
  260. else if (shorthand_definition->type == ShorthandType::Recursive)
  261. {
  262. bool result = true;
  263. for (size_t i = 0; i < shorthand_definition->items.size(); i++)
  264. {
  265. const ShorthandItem& item = shorthand_definition->items[i];
  266. if (item.type == ShorthandItemType::Property)
  267. result &= ParsePropertyDeclaration(dictionary, item.property_id, property_value, source_file, source_line_number);
  268. else if (item.type == ShorthandItemType::Shorthand)
  269. result &= ParseShorthandDeclaration(dictionary, item.shorthand_id, property_value, source_file, source_line_number);
  270. else
  271. result = false;
  272. }
  273. if (!result)
  274. return false;
  275. }
  276. else
  277. {
  278. size_t value_index = 0;
  279. size_t property_index = 0;
  280. for (; value_index < property_values.size() && property_index < shorthand_definition->items.size(); property_index++)
  281. {
  282. Property new_property;
  283. new_property.source = source_file;
  284. new_property.source_line_number = source_line_number;
  285. if (!shorthand_definition->items[property_index].property_definition->ParseValue(new_property, property_values[value_index]))
  286. {
  287. // This definition failed to parse; if we're falling through, try the next property. If there is no
  288. // next property, then abort!
  289. if (shorthand_definition->type == ShorthandType::FallThrough)
  290. {
  291. if (property_index + 1 < shorthand_definition->items.size())
  292. continue;
  293. }
  294. return false;
  295. }
  296. dictionary.SetProperty(shorthand_definition->items[property_index].property_id, new_property);
  297. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  298. if (shorthand_definition->type != ShorthandType::Replicate ||
  299. value_index < property_values.size() - 1)
  300. value_index++;
  301. }
  302. }
  303. return true;
  304. }
  305. // Sets all undefined properties in the dictionary to their defaults.
  306. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  307. {
  308. for (PropertyDefinition* property : properties)
  309. {
  310. if (property && dictionary.GetProperty(property->GetId()) == NULL)
  311. dictionary.SetProperty(property->GetId(), *property->GetDefaultValue());
  312. }
  313. }
  314. String PropertySpecification::PropertiesToString(const PropertyDictionary& dictionary) const
  315. {
  316. String result;
  317. for (auto& pair : dictionary.GetProperties())
  318. {
  319. result += property_map.GetName(pair.first) + ": " + pair.second.ToString() + '\n';
  320. }
  321. return result;
  322. }
  323. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  324. {
  325. String value;
  326. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  327. ParseState state = VALUE;
  328. int open_parentheses = 0;
  329. size_t character_index = 0;
  330. char previous_character = 0;
  331. while (character_index < values.size())
  332. {
  333. char character = values[character_index];
  334. character_index++;
  335. switch (state)
  336. {
  337. case VALUE:
  338. {
  339. if (character == ';')
  340. {
  341. value = StringUtilities::StripWhitespace(value);
  342. if (value.size() > 0)
  343. {
  344. values_list.push_back(value);
  345. value.clear();
  346. }
  347. }
  348. else if (StringUtilities::IsWhitespace(character))
  349. {
  350. if (split_values)
  351. {
  352. value = StringUtilities::StripWhitespace(value);
  353. if (value.size() > 0)
  354. {
  355. values_list.push_back(value);
  356. value.clear();
  357. }
  358. }
  359. else
  360. value += character;
  361. }
  362. else if (character == '"')
  363. {
  364. if (split_values)
  365. {
  366. value = StringUtilities::StripWhitespace(value);
  367. if (value.size() > 0)
  368. {
  369. values_list.push_back(value);
  370. value.clear();
  371. }
  372. state = VALUE_QUOTE;
  373. }
  374. else
  375. {
  376. value += ' ';
  377. state = VALUE_QUOTE;
  378. }
  379. }
  380. else if (character == '(')
  381. {
  382. open_parentheses = 1;
  383. value += character;
  384. state = VALUE_PARENTHESIS;
  385. }
  386. else
  387. {
  388. value += character;
  389. }
  390. }
  391. break;
  392. case VALUE_PARENTHESIS:
  393. {
  394. if (previous_character == '/')
  395. {
  396. if (character == ')' || character == '(')
  397. value += character;
  398. else
  399. {
  400. value += '/';
  401. value += character;
  402. }
  403. }
  404. else
  405. {
  406. if (character == '(')
  407. {
  408. open_parentheses++;
  409. value += character;
  410. }
  411. else if (character == ')')
  412. {
  413. open_parentheses--;
  414. value += character;
  415. if (open_parentheses == 0)
  416. state = VALUE;
  417. }
  418. else if (character != '/')
  419. {
  420. value += character;
  421. }
  422. }
  423. }
  424. break;
  425. case VALUE_QUOTE:
  426. {
  427. if (previous_character == '/')
  428. {
  429. if (character == '"')
  430. value += character;
  431. else
  432. {
  433. value += '/';
  434. value += character;
  435. }
  436. }
  437. else
  438. {
  439. if (character == '"')
  440. {
  441. if (split_values)
  442. {
  443. value = StringUtilities::StripWhitespace(value);
  444. if (value.size() > 0)
  445. {
  446. values_list.push_back(value);
  447. value.clear();
  448. }
  449. }
  450. else
  451. value += ' ';
  452. state = VALUE;
  453. }
  454. else if (character != '/')
  455. {
  456. value += character;
  457. }
  458. }
  459. }
  460. }
  461. previous_character = character;
  462. }
  463. if (state == VALUE)
  464. {
  465. value = StringUtilities::StripWhitespace(value);
  466. if (value.size() > 0)
  467. values_list.push_back(value);
  468. }
  469. return true;
  470. }
  471. }
  472. }