PropertySpecification.cpp 17 KB

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