PropertySpecification.cpp 14 KB

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