PropertyParserDecorator.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "PropertyParserDecorator.h"
  29. #include "../../Include/RmlUi/Core/DecoratorInstancer.h"
  30. #include "../../Include/RmlUi/Core/Factory.h"
  31. #include "../../Include/RmlUi/Core/PropertySpecification.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/StyleSheetTypes.h"
  34. namespace Rml {
  35. PropertyParserDecorator::PropertyParserDecorator()
  36. {}
  37. PropertyParserDecorator::~PropertyParserDecorator()
  38. {}
  39. bool PropertyParserDecorator::ParseValue(Property& property, const String& decorator_string_value, const ParameterMap& /*parameters*/) const
  40. {
  41. // Decorators are declared as
  42. // decorator: <decorator-value>[, <decorator-value> ...];
  43. // Where <decorator-value> is either a @decorator name:
  44. // decorator: invader-theme-background, ...;
  45. // or is an anonymous decorator with inline properties
  46. // decorator: tiled-box( <shorthand properties> ), ...;
  47. if (decorator_string_value.empty() || decorator_string_value == "none")
  48. {
  49. property.value = Variant();
  50. property.unit = Property::UNKNOWN;
  51. return true;
  52. }
  53. RMLUI_ZoneScoped;
  54. DecoratorDeclarationList decorators;
  55. // Make sure we don't split inside the parenthesis since they may appear in decorator shorthands.
  56. StringList decorator_string_list;
  57. StringUtilities::ExpandString(decorator_string_list, decorator_string_value, ',', '(', ')');
  58. decorators.value = decorator_string_value;
  59. decorators.list.reserve(decorator_string_list.size());
  60. // Get or instance each decorator in the comma-separated string list
  61. for (const String& decorator_string : decorator_string_list)
  62. {
  63. const size_t shorthand_open = decorator_string.find('(');
  64. const size_t shorthand_close = decorator_string.rfind(')');
  65. const bool invalid_parenthesis = (shorthand_open == String::npos || shorthand_close == String::npos || shorthand_open >= shorthand_close);
  66. if (invalid_parenthesis)
  67. {
  68. // We found no parenthesis, that means the value must be a name of a @decorator rule.
  69. decorators.list.emplace_back(DecoratorDeclaration{ decorator_string, nullptr, {} });
  70. }
  71. else
  72. {
  73. // Since we have parentheses it must be an anonymous decorator with inline properties
  74. const String type = StringUtilities::StripWhitespace(decorator_string.substr(0, shorthand_open));
  75. // Check for valid decorator type
  76. DecoratorInstancer* instancer = Factory::GetDecoratorInstancer(type);
  77. if (!instancer)
  78. {
  79. Log::Message(Log::LT_WARNING, "Decorator type '%s' not found.", type.c_str());
  80. return false;
  81. }
  82. const String shorthand = decorator_string.substr(shorthand_open + 1, shorthand_close - shorthand_open - 1);
  83. const PropertySpecification& specification = instancer->GetPropertySpecification();
  84. // Parse the shorthand properties given by the 'decorator' shorthand property
  85. PropertyDictionary properties;
  86. if (!specification.ParsePropertyDeclaration(properties, "decorator", shorthand))
  87. {
  88. // Empty values are allowed in decorators, if the value is not empty we must have encountered a parser error.
  89. if (!StringUtilities::StripWhitespace(shorthand).empty())
  90. return false;
  91. }
  92. // Set unspecified values to their defaults
  93. specification.SetPropertyDefaults(properties);
  94. decorators.list.emplace_back(DecoratorDeclaration{ type, instancer, std::move(properties) });
  95. }
  96. }
  97. if (decorators.list.empty())
  98. return false;
  99. property.value = Variant(MakeShared<DecoratorDeclarationList>(std::move(decorators)));
  100. property.unit = Property::DECORATOR;
  101. return true;
  102. }
  103. } // namespace Rml