PropertySpecification.cpp 15 KB

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