StyleSheetFactory.cpp 6.7 KB

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