PropertySpecification.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/PropertySpecification.h"
  29. #include "../../Include/RmlUi/Core/Debug.h"
  30. #include "../../Include/RmlUi/Core/Log.h"
  31. #include "../../Include/RmlUi/Core/PropertyDefinition.h"
  32. #include "../../Include/RmlUi/Core/PropertyDictionary.h"
  33. #include "PropertyShorthandDefinition.h"
  34. #include "IdNameMap.h"
  35. #include <limits.h>
  36. #include <stdint.h>
  37. namespace Rml {
  38. PropertySpecification::PropertySpecification(size_t reserve_num_properties, size_t reserve_num_shorthands) :
  39. // Increment reserve numbers by one because the 'invalid' property occupies the first element
  40. properties(reserve_num_properties + 1), shorthands(reserve_num_shorthands + 1),
  41. property_map(MakeUnique<PropertyIdNameMap>(reserve_num_properties + 1)), shorthand_map(MakeUnique<ShorthandIdNameMap>(reserve_num_shorthands + 1))
  42. {
  43. }
  44. PropertySpecification::~PropertySpecification()
  45. {
  46. }
  47. // Registers a property with a new definition.
  48. PropertyDefinition& PropertySpecification::RegisterProperty(const String& property_name, const String& default_value, bool inherited, bool forces_layout, PropertyId id)
  49. {
  50. if (id == PropertyId::Invalid)
  51. id = property_map->GetOrCreateId(property_name);
  52. else
  53. property_map->AddPair(id, property_name);
  54. size_t index = (size_t)id;
  55. if (index >= size_t(PropertyId::MaxNumIds))
  56. {
  57. Log::Message(Log::LT_ERROR, "Fatal error while registering property '%s': Maximum number of allowed properties exceeded. Continuing execution may lead to crash.", property_name.c_str());
  58. RMLUI_ERROR;
  59. return *properties[0];
  60. }
  61. if (index < properties.size())
  62. {
  63. // We don't want to owerwrite an existing entry.
  64. if (properties[index])
  65. {
  66. Log::Message(Log::LT_ERROR, "While registering property '%s': The property is already registered.", property_name.c_str());
  67. return *properties[index];
  68. }
  69. }
  70. else
  71. {
  72. // Resize vector to hold the new index
  73. properties.resize((index*3)/2 + 1);
  74. }
  75. // Create and insert the new property
  76. properties[index] = MakeUnique<PropertyDefinition>(id, default_value, inherited, forces_layout);
  77. property_ids.Insert(id);
  78. if (inherited)
  79. property_ids_inherited.Insert(id);
  80. if (forces_layout)
  81. property_ids_forcing_layout.Insert(id);
  82. return *properties[index];
  83. }
  84. // Returns a property definition.
  85. const PropertyDefinition* PropertySpecification::GetProperty(PropertyId id) const
  86. {
  87. if (id == PropertyId::Invalid || (size_t)id >= properties.size())
  88. return nullptr;
  89. return properties[(size_t)id].get();
  90. }
  91. const PropertyDefinition* PropertySpecification::GetProperty(const String& property_name) const
  92. {
  93. return GetProperty(property_map->GetId(property_name));
  94. }
  95. // Fetches a list of the names of all registered property definitions.
  96. const PropertyIdSet& PropertySpecification::GetRegisteredProperties(void) const
  97. {
  98. return property_ids;
  99. }
  100. // Fetches a list of the names of all registered property definitions.
  101. const PropertyIdSet& PropertySpecification::GetRegisteredInheritedProperties(void) const
  102. {
  103. return property_ids_inherited;
  104. }
  105. const PropertyIdSet& PropertySpecification::GetRegisteredPropertiesForcingLayout() const
  106. {
  107. return property_ids_forcing_layout;
  108. }
  109. // Registers a shorthand property definition.
  110. ShorthandId PropertySpecification::RegisterShorthand(const String& shorthand_name, const String& property_names, ShorthandType type, ShorthandId id)
  111. {
  112. if (id == ShorthandId::Invalid)
  113. id = shorthand_map->GetOrCreateId(shorthand_name);
  114. else
  115. shorthand_map->AddPair(id, shorthand_name);
  116. StringList property_list;
  117. StringUtilities::ExpandString(property_list, StringUtilities::ToLower(property_names));
  118. // Construct the new shorthand definition and resolve its properties.
  119. UniquePtr<ShorthandDefinition> property_shorthand(new ShorthandDefinition());
  120. for (const String& raw_name : property_list)
  121. {
  122. ShorthandItem item;
  123. bool optional = false;
  124. String name = raw_name;
  125. if (!raw_name.empty() && raw_name.back() == '?')
  126. {
  127. optional = true;
  128. name.pop_back();
  129. }
  130. PropertyId property_id = property_map->GetId(name);
  131. if (property_id != PropertyId::Invalid)
  132. {
  133. // We have a valid property
  134. if (const PropertyDefinition* property = GetProperty(property_id))
  135. item = ShorthandItem(property_id, property, optional);
  136. }
  137. else
  138. {
  139. // Otherwise, we must be a shorthand
  140. ShorthandId shorthand_id = shorthand_map->GetId(name);
  141. // Test for valid shorthand id. The recursive types (and only those) can hold other shorthands.
  142. if (shorthand_id != ShorthandId::Invalid && (type == ShorthandType::RecursiveRepeat || type == ShorthandType::RecursiveCommaSeparated))
  143. {
  144. if (const ShorthandDefinition * shorthand = GetShorthand(shorthand_id))
  145. item = ShorthandItem(shorthand_id, shorthand, optional);
  146. }
  147. }
  148. if (item.type == ShorthandItemType::Invalid)
  149. {
  150. Log::Message(Log::LT_ERROR, "Shorthand property '%s' was registered with invalid property '%s'.", shorthand_name.c_str(), name.c_str());
  151. return ShorthandId::Invalid;
  152. }
  153. property_shorthand->items.push_back(item);
  154. }
  155. property_shorthand->id = id;
  156. property_shorthand->type = type;
  157. const size_t index = (size_t)id;
  158. if (index >= size_t(ShorthandId::MaxNumIds))
  159. {
  160. Log::Message(Log::LT_ERROR, "Error while registering shorthand '%s': Maximum number of allowed shorthands exceeded.", shorthand_name.c_str());
  161. return ShorthandId::Invalid;
  162. }
  163. if (index < shorthands.size())
  164. {
  165. // We don't want to owerwrite an existing entry.
  166. if (shorthands[index])
  167. {
  168. Log::Message(Log::LT_ERROR, "The shorthand '%s' already exists, ignoring.", shorthand_name.c_str());
  169. return ShorthandId::Invalid;
  170. }
  171. }
  172. else
  173. {
  174. // Resize vector to hold the new index
  175. shorthands.resize((index * 3) / 2 + 1);
  176. }
  177. shorthands[index] = std::move(property_shorthand);
  178. return id;
  179. }
  180. // Returns a shorthand definition.
  181. const ShorthandDefinition* PropertySpecification::GetShorthand(ShorthandId id) const
  182. {
  183. if (id == ShorthandId::Invalid || (size_t)id >= shorthands.size())
  184. return nullptr;
  185. return shorthands[(size_t)id].get();
  186. }
  187. const ShorthandDefinition* PropertySpecification::GetShorthand(const String& shorthand_name) const
  188. {
  189. return GetShorthand(shorthand_map->GetId(shorthand_name));
  190. }
  191. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, const String& property_name, const String& property_value) const
  192. {
  193. // Try as a property first
  194. PropertyId property_id = property_map->GetId(property_name);
  195. if (property_id != PropertyId::Invalid)
  196. return ParsePropertyDeclaration(dictionary, property_id, property_value);
  197. // Then, as a shorthand
  198. ShorthandId shorthand_id = shorthand_map->GetId(property_name);
  199. if (shorthand_id != ShorthandId::Invalid)
  200. return ParseShorthandDeclaration(dictionary, shorthand_id, property_value);
  201. return false;
  202. }
  203. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, PropertyId property_id, const String& property_value) const
  204. {
  205. // Parse as a single property.
  206. const PropertyDefinition* property_definition = GetProperty(property_id);
  207. if (!property_definition)
  208. return false;
  209. StringList property_values;
  210. if (!ParsePropertyValues(property_values, property_value, false) || property_values.size() == 0)
  211. return false;
  212. Property new_property;
  213. if (!property_definition->ParseValue(new_property, property_values[0]))
  214. return false;
  215. dictionary.SetProperty(property_id, new_property);
  216. return true;
  217. }
  218. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  219. bool PropertySpecification::ParseShorthandDeclaration(PropertyDictionary& dictionary, ShorthandId shorthand_id, const String& property_value) const
  220. {
  221. StringList property_values;
  222. if (!ParsePropertyValues(property_values, property_value, true) || property_values.size() == 0)
  223. return false;
  224. // Parse as a shorthand.
  225. const ShorthandDefinition* shorthand_definition = GetShorthand(shorthand_id);
  226. if (!shorthand_definition)
  227. return false;
  228. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  229. // than four values
  230. if (shorthand_definition->type == ShorthandType::Box &&
  231. property_values.size() < 4)
  232. {
  233. // This array tells which property index each side is parsed from
  234. Array<int, 4> box_side_to_value_index = { 0,0,0,0 };
  235. switch (property_values.size())
  236. {
  237. case 1:
  238. // Only one value is defined, so it is parsed onto all four sides.
  239. box_side_to_value_index = { 0,0,0,0 };
  240. break;
  241. case 2:
  242. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  243. // the left and right.
  244. box_side_to_value_index = { 0,1,0,1 };
  245. break;
  246. case 3:
  247. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  248. // right, and the third onto the bottom.
  249. box_side_to_value_index = { 0,1,2,1 };
  250. break;
  251. default:
  252. RMLUI_ERROR;
  253. break;
  254. }
  255. for (int i = 0; i < 4; i++)
  256. {
  257. RMLUI_ASSERT(shorthand_definition->items[i].type == ShorthandItemType::Property);
  258. Property new_property;
  259. int value_index = box_side_to_value_index[i];
  260. if (!shorthand_definition->items[i].property_definition->ParseValue(new_property, property_values[value_index]))
  261. return false;
  262. dictionary.SetProperty(shorthand_definition->items[i].property_definition->GetId(), new_property);
  263. }
  264. }
  265. else if (shorthand_definition->type == ShorthandType::RecursiveRepeat)
  266. {
  267. bool result = true;
  268. for (size_t i = 0; i < shorthand_definition->items.size(); i++)
  269. {
  270. const ShorthandItem& item = shorthand_definition->items[i];
  271. if (item.type == ShorthandItemType::Property)
  272. result &= ParsePropertyDeclaration(dictionary, item.property_id, property_value);
  273. else if (item.type == ShorthandItemType::Shorthand)
  274. result &= ParseShorthandDeclaration(dictionary, item.shorthand_id, property_value);
  275. else
  276. result = false;
  277. }
  278. if (!result)
  279. return false;
  280. }
  281. else if (shorthand_definition->type == ShorthandType::RecursiveCommaSeparated)
  282. {
  283. StringList subvalues;
  284. StringUtilities::ExpandString(subvalues, property_value);
  285. size_t num_optional = 0;
  286. for (auto& item : shorthand_definition->items)
  287. if (item.optional)
  288. num_optional += 1;
  289. if (subvalues.size() + num_optional < shorthand_definition->items.size())
  290. {
  291. // Not enough subvalues declared.
  292. return false;
  293. }
  294. size_t subvalue_i = 0;
  295. for (size_t i = 0; i < shorthand_definition->items.size() && subvalue_i < subvalues.size(); i++)
  296. {
  297. bool result = false;
  298. const ShorthandItem& item = shorthand_definition->items[i];
  299. if (item.type == ShorthandItemType::Property)
  300. result = ParsePropertyDeclaration(dictionary, item.property_id, subvalues[subvalue_i]);
  301. else if (item.type == ShorthandItemType::Shorthand)
  302. result = ParseShorthandDeclaration(dictionary, item.shorthand_id, subvalues[subvalue_i]);
  303. if (result)
  304. subvalue_i += 1;
  305. else if (!item.optional)
  306. return false;
  307. }
  308. }
  309. else
  310. {
  311. size_t value_index = 0;
  312. size_t property_index = 0;
  313. for (; value_index < property_values.size() && property_index < shorthand_definition->items.size(); property_index++)
  314. {
  315. Property new_property;
  316. if (!shorthand_definition->items[property_index].property_definition->ParseValue(new_property, property_values[value_index]))
  317. {
  318. // This definition failed to parse; if we're falling through, try the next property. If there is no
  319. // next property, then abort!
  320. if (shorthand_definition->type == ShorthandType::FallThrough)
  321. {
  322. if (property_index + 1 < shorthand_definition->items.size())
  323. continue;
  324. }
  325. return false;
  326. }
  327. dictionary.SetProperty(shorthand_definition->items[property_index].property_id, new_property);
  328. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  329. if (shorthand_definition->type != ShorthandType::Replicate ||
  330. value_index < property_values.size() - 1)
  331. value_index++;
  332. }
  333. }
  334. return true;
  335. }
  336. // Sets all undefined properties in the dictionary to their defaults.
  337. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  338. {
  339. for (const auto& property : properties)
  340. {
  341. if (property && dictionary.GetProperty(property->GetId()) == nullptr)
  342. dictionary.SetProperty(property->GetId(), *property->GetDefaultValue());
  343. }
  344. }
  345. String PropertySpecification::PropertiesToString(const PropertyDictionary& dictionary) const
  346. {
  347. String result;
  348. for (auto& pair : dictionary.GetProperties())
  349. {
  350. result += property_map->GetName(pair.first) + ": " + pair.second.ToString() + '\n';
  351. }
  352. return result;
  353. }
  354. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  355. {
  356. String value;
  357. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  358. ParseState state = VALUE;
  359. int open_parentheses = 0;
  360. size_t character_index = 0;
  361. char previous_character = 0;
  362. while (character_index < values.size())
  363. {
  364. char character = values[character_index];
  365. character_index++;
  366. switch (state)
  367. {
  368. case VALUE:
  369. {
  370. if (character == ';')
  371. {
  372. value = StringUtilities::StripWhitespace(value);
  373. if (value.size() > 0)
  374. {
  375. values_list.push_back(value);
  376. value.clear();
  377. }
  378. }
  379. else if (StringUtilities::IsWhitespace(character))
  380. {
  381. if (split_values)
  382. {
  383. value = StringUtilities::StripWhitespace(value);
  384. if (value.size() > 0)
  385. {
  386. values_list.push_back(value);
  387. value.clear();
  388. }
  389. }
  390. else
  391. value += character;
  392. }
  393. else if (character == '"')
  394. {
  395. if (split_values)
  396. {
  397. value = StringUtilities::StripWhitespace(value);
  398. if (value.size() > 0)
  399. {
  400. values_list.push_back(value);
  401. value.clear();
  402. }
  403. state = VALUE_QUOTE;
  404. }
  405. else
  406. {
  407. value += ' ';
  408. state = VALUE_QUOTE;
  409. }
  410. }
  411. else if (character == '(')
  412. {
  413. open_parentheses = 1;
  414. value += character;
  415. state = VALUE_PARENTHESIS;
  416. }
  417. else
  418. {
  419. value += character;
  420. }
  421. }
  422. break;
  423. case VALUE_PARENTHESIS:
  424. {
  425. if (previous_character == '/')
  426. {
  427. if (character == ')' || character == '(')
  428. value += character;
  429. else
  430. {
  431. value += '/';
  432. value += character;
  433. }
  434. }
  435. else
  436. {
  437. if (character == '(')
  438. {
  439. open_parentheses++;
  440. value += character;
  441. }
  442. else if (character == ')')
  443. {
  444. open_parentheses--;
  445. value += character;
  446. if (open_parentheses == 0)
  447. state = VALUE;
  448. }
  449. else if (character != '/')
  450. {
  451. value += character;
  452. }
  453. }
  454. }
  455. break;
  456. case VALUE_QUOTE:
  457. {
  458. if (previous_character == '/')
  459. {
  460. if (character == '"')
  461. value += character;
  462. else
  463. {
  464. value += '/';
  465. value += character;
  466. }
  467. }
  468. else
  469. {
  470. if (character == '"')
  471. {
  472. if (split_values)
  473. {
  474. value = StringUtilities::StripWhitespace(value);
  475. if (value.size() > 0)
  476. {
  477. values_list.push_back(value);
  478. value.clear();
  479. }
  480. }
  481. else
  482. value += ' ';
  483. state = VALUE;
  484. }
  485. else if (character != '/')
  486. {
  487. value += character;
  488. }
  489. }
  490. }
  491. }
  492. previous_character = character;
  493. }
  494. if (state == VALUE)
  495. {
  496. value = StringUtilities::StripWhitespace(value);
  497. if (value.size() > 0)
  498. values_list.push_back(value);
  499. }
  500. return true;
  501. }
  502. } // namespace Rml