StyleSheetFactory.cpp 6.8 KB

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