StyleSheetFactory.cpp 6.8 KB

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