PropertySpecification.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 >= shorthands.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, const String& property_name, const String& property_value, const String& source_file, int source_line_number) const
  181. {
  182. // Try as a property first
  183. PropertyId property_id = property_map.GetId(property_name);
  184. if (property_id != PropertyId::Invalid)
  185. return ParsePropertyDeclaration(dictionary, property_id, property_value, source_file, source_line_number);
  186. // Then, as a shorthand
  187. ShorthandId shorthand_id = shorthand_map.GetId(property_name);
  188. if (shorthand_id != ShorthandId::Invalid)
  189. return ParseShorthandDeclaration(dictionary, shorthand_id, property_value, source_file, source_line_number);
  190. return false;
  191. }
  192. bool PropertySpecification::ParsePropertyDeclaration(PropertyDictionary& dictionary, PropertyId property_id, const String& property_value, const String& source_file, int source_line_number) const
  193. {
  194. // Parse as a single property.
  195. const PropertyDefinition* property_definition = GetProperty(property_id);
  196. if (!property_definition)
  197. return false;
  198. StringList property_values;
  199. if (!ParsePropertyValues(property_values, property_value, false) || property_values.size() == 0)
  200. return false;
  201. Property new_property;
  202. new_property.source = source_file;
  203. new_property.source_line_number = source_line_number;
  204. if (!property_definition->ParseValue(new_property, property_values[0]))
  205. return false;
  206. dictionary.SetProperty(property_id, new_property);
  207. return true;
  208. }
  209. // Parses a property declaration, setting any parsed and validated properties on the given dictionary.
  210. bool PropertySpecification::ParseShorthandDeclaration(PropertyDictionary& dictionary, ShorthandId shorthand_id, const String& property_value, const String& source_file, int source_line_number) const
  211. {
  212. StringList property_values;
  213. if (!ParsePropertyValues(property_values, property_value, true) || property_values.size() == 0)
  214. return false;
  215. // Parse as a shorthand.
  216. const ShorthandDefinition* shorthand_definition = GetShorthand(shorthand_id);
  217. if (!shorthand_definition)
  218. return false;
  219. // If this definition is a 'box'-style shorthand (x-top, x-right, x-bottom, x-left, etc) and there are fewer
  220. // than four values
  221. if (shorthand_definition->type == ShorthandType::Box &&
  222. property_values.size() < 4)
  223. {
  224. // This array tells which property index each side is parsed from
  225. std::array<int, 4> box_side_to_value_index = { 0,0,0,0 };
  226. switch (property_values.size())
  227. {
  228. case 1:
  229. // Only one value is defined, so it is parsed onto all four sides.
  230. box_side_to_value_index = { 0,0,0,0 };
  231. break;
  232. case 2:
  233. // Two values are defined, so the first one is parsed onto the top and bottom value, the second onto
  234. // the left and right.
  235. box_side_to_value_index = { 0,1,0,1 };
  236. break;
  237. case 3:
  238. // Three values are defined, so the first is parsed into the top value, the second onto the left and
  239. // right, and the third onto the bottom.
  240. box_side_to_value_index = { 0,1,2,1 };
  241. break;
  242. default:
  243. ROCKET_ERROR;
  244. break;
  245. }
  246. for (int i = 0; i < 4; i++)
  247. {
  248. ROCKET_ASSERT(shorthand_definition->items[i].type == ShorthandItemType::Property);
  249. Property new_property;
  250. int value_index = box_side_to_value_index[i];
  251. if (!shorthand_definition->items[i].property_definition->ParseValue(new_property, property_values[value_index]))
  252. return false;
  253. new_property.source = source_file;
  254. new_property.source_line_number = source_line_number;
  255. dictionary.SetProperty(shorthand_definition->items[i].property_definition->GetId(), new_property);
  256. }
  257. }
  258. else if (shorthand_definition->type == ShorthandType::Recursive)
  259. {
  260. bool result = true;
  261. for (size_t i = 0; i < shorthand_definition->items.size(); i++)
  262. {
  263. const ShorthandItem& item = shorthand_definition->items[i];
  264. if (item.type == ShorthandItemType::Property)
  265. result &= ParsePropertyDeclaration(dictionary, item.property_id, property_value, source_file, source_line_number);
  266. else if (item.type == ShorthandItemType::Shorthand)
  267. result &= ParseShorthandDeclaration(dictionary, item.shorthand_id, property_value, source_file, source_line_number);
  268. else
  269. result = false;
  270. }
  271. if (!result)
  272. return false;
  273. }
  274. else
  275. {
  276. size_t value_index = 0;
  277. size_t property_index = 0;
  278. for (; value_index < property_values.size() && property_index < shorthand_definition->items.size(); property_index++)
  279. {
  280. Property new_property;
  281. new_property.source = source_file;
  282. new_property.source_line_number = source_line_number;
  283. if (!shorthand_definition->items[property_index].property_definition->ParseValue(new_property, property_values[value_index]))
  284. {
  285. // This definition failed to parse; if we're falling through, try the next property. If there is no
  286. // next property, then abort!
  287. if (shorthand_definition->type == ShorthandType::FallThrough)
  288. {
  289. if (property_index + 1 < shorthand_definition->items.size())
  290. continue;
  291. }
  292. return false;
  293. }
  294. dictionary.SetProperty(shorthand_definition->items[property_index].property_id, new_property);
  295. // Increment the value index, unless we're replicating the last value and we're up to the last value.
  296. if (shorthand_definition->type != ShorthandType::Replicate ||
  297. value_index < property_values.size() - 1)
  298. value_index++;
  299. }
  300. }
  301. return true;
  302. }
  303. // Sets all undefined properties in the dictionary to their defaults.
  304. void PropertySpecification::SetPropertyDefaults(PropertyDictionary& dictionary) const
  305. {
  306. for (PropertyDefinition* property : properties)
  307. {
  308. if (dictionary.GetProperty(property->GetId()) == NULL)
  309. dictionary.SetProperty(property->GetId(), *property->GetDefaultValue());
  310. }
  311. }
  312. bool PropertySpecification::ParsePropertyValues(StringList& values_list, const String& values, bool split_values) const
  313. {
  314. String value;
  315. enum ParseState { VALUE, VALUE_PARENTHESIS, VALUE_QUOTE };
  316. ParseState state = VALUE;
  317. int open_parentheses = 0;
  318. size_t character_index = 0;
  319. char previous_character = 0;
  320. while (character_index < values.size())
  321. {
  322. char character = values[character_index];
  323. character_index++;
  324. switch (state)
  325. {
  326. case VALUE:
  327. {
  328. if (character == ';')
  329. {
  330. value = StringUtilities::StripWhitespace(value);
  331. if (value.size() > 0)
  332. {
  333. values_list.push_back(value);
  334. value.clear();
  335. }
  336. }
  337. else if (StringUtilities::IsWhitespace(character))
  338. {
  339. if (split_values)
  340. {
  341. value = StringUtilities::StripWhitespace(value);
  342. if (value.size() > 0)
  343. {
  344. values_list.push_back(value);
  345. value.clear();
  346. }
  347. }
  348. else
  349. value += character;
  350. }
  351. else if (character == '"')
  352. {
  353. if (split_values)
  354. {
  355. value = StringUtilities::StripWhitespace(value);
  356. if (value.size() > 0)
  357. {
  358. values_list.push_back(value);
  359. value.clear();
  360. }
  361. state = VALUE_QUOTE;
  362. }
  363. else
  364. {
  365. value += ' ';
  366. state = VALUE_QUOTE;
  367. }
  368. }
  369. else if (character == '(')
  370. {
  371. open_parentheses = 1;
  372. value += character;
  373. state = VALUE_PARENTHESIS;
  374. }
  375. else
  376. {
  377. value += character;
  378. }
  379. }
  380. break;
  381. case VALUE_PARENTHESIS:
  382. {
  383. if (previous_character == '/')
  384. {
  385. if (character == ')' || character == '(')
  386. value += character;
  387. else
  388. {
  389. value += '/';
  390. value += character;
  391. }
  392. }
  393. else
  394. {
  395. if (character == '(')
  396. {
  397. open_parentheses++;
  398. value += character;
  399. }
  400. else if (character == ')')
  401. {
  402. open_parentheses--;
  403. value += character;
  404. if (open_parentheses == 0)
  405. state = VALUE;
  406. }
  407. else if (character != '/')
  408. {
  409. value += character;
  410. }
  411. }
  412. }
  413. break;
  414. case VALUE_QUOTE:
  415. {
  416. if (previous_character == '/')
  417. {
  418. if (character == '"')
  419. value += character;
  420. else
  421. {
  422. value += '/';
  423. value += character;
  424. }
  425. }
  426. else
  427. {
  428. if (character == '"')
  429. {
  430. if (split_values)
  431. {
  432. value = StringUtilities::StripWhitespace(value);
  433. if (value.size() > 0)
  434. {
  435. values_list.push_back(value);
  436. value.clear();
  437. }
  438. }
  439. else
  440. value += ' ';
  441. state = VALUE;
  442. }
  443. else if (character != '/')
  444. {
  445. value += character;
  446. }
  447. }
  448. }
  449. }
  450. previous_character = character;
  451. }
  452. if (state == VALUE)
  453. {
  454. value = StringUtilities::StripWhitespace(value);
  455. if (value.size() > 0)
  456. values_list.push_back(value);
  457. }
  458. return true;
  459. }
  460. }
  461. }