PropertySpecification.cpp 15 KB

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