StyleSheetFactory.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "StyleSheetFactory.h"
  2. #include "../../Include/RmlUi/Core/Log.h"
  3. #include "../../Include/RmlUi/Core/StyleSheetContainer.h"
  4. #include "StreamFile.h"
  5. #include "StyleSheetNode.h"
  6. #include "StyleSheetParser.h"
  7. #include "StyleSheetSelector.h"
  8. namespace Rml {
  9. static UniquePtr<StyleSheetFactory> instance;
  10. StyleSheetFactory::StyleSheetFactory() :
  11. selectors{
  12. {"nth-child", StructuralSelectorType::Nth_Child},
  13. {"nth-last-child", StructuralSelectorType::Nth_Last_Child},
  14. {"nth-of-type", StructuralSelectorType::Nth_Of_Type},
  15. {"nth-last-of-type", StructuralSelectorType::Nth_Last_Of_Type},
  16. {"first-child", StructuralSelectorType::First_Child},
  17. {"last-child", StructuralSelectorType::Last_Child},
  18. {"first-of-type", StructuralSelectorType::First_Of_Type},
  19. {"last-of-type", StructuralSelectorType::Last_Of_Type},
  20. {"only-child", StructuralSelectorType::Only_Child},
  21. {"only-of-type", StructuralSelectorType::Only_Of_Type},
  22. {"empty", StructuralSelectorType::Empty},
  23. {"not", StructuralSelectorType::Not},
  24. {"scope", StructuralSelectorType::Scope},
  25. }
  26. {}
  27. StyleSheetFactory::~StyleSheetFactory() {}
  28. bool StyleSheetFactory::Initialise()
  29. {
  30. RMLUI_ASSERT(instance == nullptr);
  31. instance = UniquePtr<StyleSheetFactory>(new StyleSheetFactory);
  32. return true;
  33. }
  34. void StyleSheetFactory::Shutdown()
  35. {
  36. instance.reset();
  37. }
  38. const StyleSheetContainer* StyleSheetFactory::GetStyleSheetContainer(const String& sheet_name)
  39. {
  40. // Look up the sheet definition in the cache
  41. auto it = instance->stylesheets.find(sheet_name);
  42. if (it != instance->stylesheets.end())
  43. return it->second.get();
  44. // Don't currently have the sheet, attempt to load it
  45. UniquePtr<const StyleSheetContainer> sheet = instance->LoadStyleSheetContainer(sheet_name);
  46. if (!sheet)
  47. return nullptr;
  48. const StyleSheetContainer* result = sheet.get();
  49. // Add it to the cache.
  50. instance->stylesheets[sheet_name] = std::move(sheet);
  51. return result;
  52. }
  53. void StyleSheetFactory::ClearStyleSheetCache()
  54. {
  55. instance->stylesheets.clear();
  56. }
  57. StructuralSelector StyleSheetFactory::GetSelector(const String& name)
  58. {
  59. SelectorMap::const_iterator it;
  60. const size_t parameter_start = name.find('(');
  61. if (parameter_start == String::npos)
  62. it = instance->selectors.find(name);
  63. else
  64. it = instance->selectors.find(name.substr(0, parameter_start));
  65. if (it == instance->selectors.end())
  66. return StructuralSelector(StructuralSelectorType::Invalid, 0, 0);
  67. const StructuralSelectorType selector_type = it->second;
  68. bool requires_parameter = false;
  69. switch (selector_type)
  70. {
  71. case StructuralSelectorType::Nth_Child:
  72. case StructuralSelectorType::Nth_Last_Child:
  73. case StructuralSelectorType::Nth_Of_Type:
  74. case StructuralSelectorType::Nth_Last_Of_Type:
  75. case StructuralSelectorType::Not: requires_parameter = true; break;
  76. default: break;
  77. }
  78. const size_t parameter_end = name.rfind(')');
  79. const bool has_parameter = (parameter_start != String::npos && parameter_end != String::npos && parameter_start < parameter_end);
  80. if (requires_parameter != has_parameter)
  81. {
  82. Log::Message(Log::LT_WARNING, "Invalid selector ':%s' encountered, expected %s parameters", name.c_str(),
  83. requires_parameter ? "parenthesized" : "no");
  84. return StructuralSelector(StructuralSelectorType::Invalid, 0, 0);
  85. }
  86. // Parse the 'a' and 'b' values.
  87. int a = 1;
  88. int b = 0;
  89. if (has_parameter)
  90. {
  91. const String parameters = StringUtilities::StripWhitespace(StringView(name, parameter_start + 1, parameter_end - (parameter_start + 1)));
  92. if (selector_type == StructuralSelectorType::Not)
  93. {
  94. auto list = MakeShared<SelectorTree>();
  95. list->root = MakeUnique<StyleSheetNode>();
  96. list->leafs = StyleSheetParser::ConstructNodes(*list->root, parameters);
  97. int specificity = 0;
  98. for (const StyleSheetNode* node : list->leafs)
  99. specificity = Math::Max(specificity, node->GetSpecificity());
  100. return StructuralSelector(selector_type, std::move(list), specificity);
  101. }
  102. // Check for 'even' or 'odd' first.
  103. if (parameters == "even")
  104. {
  105. a = 2;
  106. b = 0;
  107. }
  108. else if (parameters == "odd")
  109. {
  110. a = 2;
  111. b = 1;
  112. }
  113. else
  114. {
  115. // Alrighty; we've got an equation in the form of [[+/-]an][(+/-)b]. So, foist up, we split on 'n'.
  116. const size_t n_index = parameters.find('n');
  117. if (n_index == String::npos)
  118. {
  119. // The equation is 0n + b. So a = 0, and we only have to parse b.
  120. a = 0;
  121. b = atoi(parameters.c_str());
  122. }
  123. else
  124. {
  125. if (n_index == 0)
  126. a = 1;
  127. else
  128. {
  129. const String a_parameter = parameters.substr(0, n_index);
  130. if (StringUtilities::StripWhitespace(a_parameter) == "-")
  131. a = -1;
  132. else
  133. a = atoi(a_parameter.c_str());
  134. }
  135. size_t pm_index = parameters.find('+', n_index + 1);
  136. if (pm_index != String::npos)
  137. b = 1;
  138. else
  139. {
  140. pm_index = parameters.find('-', n_index + 1);
  141. if (pm_index != String::npos)
  142. b = -1;
  143. }
  144. if (n_index == parameters.size() - 1 || pm_index == String::npos)
  145. b = 0;
  146. else
  147. b = b * atoi(parameters.data() + pm_index + 1);
  148. }
  149. }
  150. }
  151. return StructuralSelector(selector_type, a, b);
  152. }
  153. UniquePtr<const StyleSheetContainer> StyleSheetFactory::LoadStyleSheetContainer(const String& sheet)
  154. {
  155. UniquePtr<StyleSheetContainer> new_style_sheet;
  156. // Open stream, construct new sheet and pass the stream into the sheet
  157. auto stream = MakeUnique<StreamFile>();
  158. if (stream->Open(sheet))
  159. {
  160. new_style_sheet = MakeUnique<StyleSheetContainer>();
  161. if (!new_style_sheet->LoadStyleSheetContainer(stream.get()))
  162. {
  163. new_style_sheet.reset();
  164. }
  165. }
  166. return new_style_sheet;
  167. }
  168. } // namespace Rml