PropertyParserDecorator.cpp 4.7 KB

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