PropertySpecification.cpp 16 KB

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