PropertySpecification.cpp 14 KB

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