PropertySpecification.cpp 16 KB

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