PropertySpecification.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "../Common/TestsInterface.h"
  29. #include <RmlUi/Core/Core.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/Factory.h>
  32. #include <RmlUi/Core/PropertyDefinition.h>
  33. #include <RmlUi/Core/PropertyDictionary.h>
  34. #include <RmlUi/Core/PropertySpecification.h>
  35. #include <RmlUi/Core/StyleSheetSpecification.h>
  36. #include <doctest.h>
  37. #include <limits.h>
  38. namespace Rml {
  39. class TestPropertySpecification {
  40. public:
  41. using SplitOption = PropertySpecification::SplitOption;
  42. TestPropertySpecification(const PropertySpecification& specification) : specification(specification) {}
  43. void ParsePropertyValues(StringList& values_list, const String& values, SplitOption split_option) const
  44. {
  45. specification.ParsePropertyValues(values_list, values, split_option);
  46. }
  47. private:
  48. const PropertySpecification& specification;
  49. };
  50. } // namespace Rml
  51. using namespace Rml;
  52. static String Stringify(const StringList& list)
  53. {
  54. String result = "[";
  55. for (int i = 0; i < (int)list.size(); i++)
  56. {
  57. if (i != 0)
  58. result += "; ";
  59. result += list[i];
  60. }
  61. result += ']';
  62. return result;
  63. }
  64. TEST_CASE("PropertySpecification.ParsePropertyValues")
  65. {
  66. TestsSystemInterface system_interface;
  67. TestsRenderInterface render_interface;
  68. SetRenderInterface(&render_interface);
  69. SetSystemInterface(&system_interface);
  70. Rml::Initialise();
  71. using SplitOption = TestPropertySpecification::SplitOption;
  72. const TestPropertySpecification& specification = TestPropertySpecification(StyleSheetSpecification::GetPropertySpecification());
  73. struct Expected {
  74. Expected(const char* value) : values{String(value)} {}
  75. Expected(std::initializer_list<String> list) : values(list) {}
  76. StringList values;
  77. };
  78. auto Parse = [&](const String& test_value, const Expected& expected, SplitOption split = SplitOption::Whitespace) {
  79. StringList parsed_values;
  80. specification.ParsePropertyValues(parsed_values, test_value, split);
  81. const String split_str[] = {"none", "whitespace", "comma"};
  82. INFO("\n\tSplit: ", split_str[(int)split], "\n\tInput: ", test_value, "\n\tExpected: ", Stringify(expected.values),
  83. "\n\tResult: ", Stringify(parsed_values));
  84. CHECK(parsed_values == expected.values);
  85. };
  86. Parse("red", "red");
  87. Parse(" red ", "red");
  88. Parse("inline-block", "inline-block");
  89. Parse("none red", {"none", "red"});
  90. Parse("none red", {"none", "red"});
  91. Parse("none\t \r \nred", {"none", "red"});
  92. Parse("none red", "none red", SplitOption::None);
  93. Parse(" none red ", "none red", SplitOption::None);
  94. Parse("none red", "none red", SplitOption::None);
  95. Parse("none\t \r \nred", "none\t \r \nred", SplitOption::None);
  96. Parse("none,red", "none,red", SplitOption::None);
  97. Parse(" \"none,red\" ", "none,red", SplitOption::None);
  98. Parse("none,red", {"none,red"});
  99. Parse("none, red", {"none,", "red"});
  100. Parse("none , red", {"none", ",", "red"});
  101. Parse("none , red", {"none", ",", "red"});
  102. Parse("none,,red", "none,,red");
  103. Parse("none,,,red", "none,,,red");
  104. Parse("none,red", {"none", "red"}, SplitOption::Comma);
  105. Parse("none, red", {"none", "red"}, SplitOption::Comma);
  106. Parse("none , red", {"none", "red"}, SplitOption::Comma);
  107. Parse("none , red", {"none", "red"}, SplitOption::Comma);
  108. Parse("none,,red", {"none", "red"}, SplitOption::Comma);
  109. Parse("none,,,red", {"none", "red"}, SplitOption::Comma);
  110. Parse("none, , ,red", {"none", "red"}, SplitOption::Comma);
  111. Parse("\"string with spaces\"", "string with spaces");
  112. Parse("\"string with spaces\" two", {"string with spaces", "two"});
  113. Parse("\"string with spaces\"two", {"string with spaces", "two"});
  114. Parse("\"string with spaces\"two", "string with spaces two", SplitOption::None);
  115. Parse("\"string (with) ((parenthesis\" two", {"string (with) ((parenthesis", "two"});
  116. Parse("\"none,,red\" two", {"none,,red", "two"});
  117. Parse("aa(bb( cc ) dd) ee", {"aa(bb( cc ) dd)", "ee"});
  118. Parse("aa(\"bb cc ) dd\") ee", {"aa(\"bb cc ) dd\")", "ee"});
  119. Parse("aa(\"bb cc \\) dd\") ee", {"aa(\"bb cc \\) dd\")", "ee"});
  120. Parse("aa(\"bb cc \\) dd\") ee", "aa(\"bb cc \\) dd\") ee", SplitOption::Comma);
  121. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\"),", "aa,", "bb() cc"});
  122. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\")", "aa", "\"bb() cc\""}, SplitOption::Comma);
  123. Parse("none(\"long string\"), aa, bb() cc", {"none(\"long string\")", "aa", "bb() cc"}, SplitOption::Comma);
  124. Parse("tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )", "tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )");
  125. Parse("tiled-horizontal( title-bar-l, title-bar-c,\n\ttitle-bar-r )", "tiled-horizontal( title-bar-l, title-bar-c,\n\ttitle-bar-r )");
  126. Parse("tiled-horizontal( title-bar-l, title-bar-c )", "tiled-horizontal( title-bar-l, title-bar-c )", SplitOption::Comma);
  127. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  128. {"linear-gradient(110deg, #fff, #000 10%)", "border-box,", "image(invader.png)"});
  129. Parse("linear-gradient(110deg, rgba( 255, 255, 255, 255 ), #000 10%) border-box, image(invader.png)",
  130. {"linear-gradient(110deg, rgba( 255, 255, 255, 255 ), #000 10%)", "border-box,", "image(invader.png)"});
  131. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  132. {"linear-gradient(110deg, #fff, #000 10%) border-box", "image(invader.png)"}, SplitOption::Comma);
  133. Parse("linear-gradient(110deg, rgba( 255, 255, 255, 255 ), #000 10%) border-box, image(invader.png)",
  134. {"linear-gradient(110deg, rgba( 255, 255, 255, 255 ), #000 10%) border-box", "image(invader.png)"}, SplitOption::Comma);
  135. Parse(R"(image( a\) b ))", {R"(image( a\))", "b", ")"});
  136. Parse(R"(image( a\) b ))", R"(image( a\) b ))", SplitOption::Comma);
  137. Parse(R"(image( ))", R"(image( ))");
  138. Parse(R"(image( a\\b ))", R"(image( a\\b ))");
  139. Parse(R"(image( a\\\b ))", R"(image( a\\\b ))");
  140. Parse(R"(image( a\\\\b ))", R"(image( a\\\\b ))");
  141. Parse(R"(image("a\)b"))", R"(image("a\)b"))");
  142. Parse(R"(image("a\\)b"))", R"(image("a\)b"))");
  143. Parse(R"(image("a\\b"))", R"(image("a\b"))");
  144. Parse(R"(image("a\\\b"))", R"(image("a\\b"))");
  145. Parse(R"(image("a\\\\b"))", R"(image("a\\b"))");
  146. Rml::Shutdown();
  147. }
  148. TEST_CASE("PropertySpecification.string")
  149. {
  150. TestsSystemInterface system_interface;
  151. TestsRenderInterface render_interface;
  152. SetRenderInterface(&render_interface);
  153. SetSystemInterface(&system_interface);
  154. Rml::Initialise();
  155. PropertySpecification specification(1, 0);
  156. const PropertyId id = specification.RegisterProperty("name", "", false, false).AddParser("string").GetId();
  157. // Parse value into the <string> property.
  158. auto Parse = [&](const String& test_value, const String& expected) {
  159. PropertyDictionary properties;
  160. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  161. const auto num_properties = properties.GetProperties().size();
  162. CHECK(parse_success);
  163. CHECK(num_properties == 1);
  164. CHECK(properties.GetProperty(id));
  165. if (const Property* property = properties.GetProperty(id))
  166. {
  167. const String parsed_value = property->Get<String>();
  168. CHECK_MESSAGE(parsed_value == expected, "Test value: ", test_value);
  169. }
  170. };
  171. Parse("a", "a");
  172. Parse(" a ", "a");
  173. Parse("green", "green");
  174. Parse("image(ress:///.ress#/images/a.png)", "image(ress:///.ress#/images/a.png)");
  175. Parse(R"(image("ress:///.ress#/images/a.png"))", R"(image("ress:///.ress#/images/a.png"))");
  176. Parse(R"("ress:///.ress#/images/a.png")", R"(ress:///.ress#/images/a.png)");
  177. Parse(R"("escaped\"quotes")", R"(escaped"quotes)");
  178. Parse(R"("escaped\\backslash")", R"(escaped\backslash)");
  179. Parse(R"("bad_\escape")", R"(bad_\escape)");
  180. Parse(R"(C:\Windows\test.png)", R"(C:\Windows\test.png)");
  181. Parse(R"("C:\Windows\test.png")", R"(C:\Windows\test.png)");
  182. Parse(R"(C:\\Windows\\test.png)", R"(C:\\Windows\\test.png)");
  183. Parse(R"("C:\\Windows\\test.png")", R"(C:\Windows\test.png)");
  184. Parse(R"(\\host\test.png)", R"(\\host\test.png)");
  185. Parse(R"(\\\host\test.png)", R"(\\\host\test.png)");
  186. Parse(R"("\\host\\test.png")", R"(\host\test.png)");
  187. Parse("image(a)", "image(a)");
  188. Parse(R"(image(a))", R"(image(a))");
  189. Parse(R"(image(a, "b"))", R"(image(a, "b"))");
  190. Parse(R"V("image(a, \"b\")")V", R"V(image(a, "b"))V");
  191. Rml::Shutdown();
  192. }
  193. TEST_CASE("PropertyParser.Keyword")
  194. {
  195. TestsSystemInterface system_interface;
  196. TestsRenderInterface render_interface;
  197. SetRenderInterface(&render_interface);
  198. SetSystemInterface(&system_interface);
  199. Rml::Initialise();
  200. // Test keyword parser. Ensure that the keyword values are correct.
  201. PropertySpecification specification(20, 0);
  202. auto Parse = [&](const PropertyId id, const String& test_value, int expected_value) {
  203. PropertyDictionary properties;
  204. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  205. if (expected_value == INT_MAX)
  206. {
  207. CHECK(!parse_success);
  208. }
  209. else
  210. {
  211. CHECK(parse_success);
  212. CHECK(properties.GetProperties().size() == 1);
  213. const int parsed_value = properties.GetProperty(id)->Get<int>();
  214. CHECK_MESSAGE(parsed_value == expected_value, "Test value: ", test_value);
  215. const String parsed_value_str = properties.GetProperty(id)->ToString();
  216. CHECK(parsed_value_str == test_value);
  217. }
  218. };
  219. const PropertyId simple = specification.RegisterProperty("simple", "", false, false).AddParser("keyword", "a, b, c").GetId();
  220. Parse(simple, "a", 0);
  221. Parse(simple, "b", 1);
  222. Parse(simple, "c", 2);
  223. Parse(simple, "d", INT_MAX);
  224. Parse(simple, "0", INT_MAX);
  225. Parse(simple, "2", INT_MAX);
  226. const PropertyId values = specification.RegisterProperty("values", "", false, false).AddParser("keyword", "a=50, b, c=-200").GetId();
  227. Parse(values, "a", 50);
  228. Parse(values, "b", 51);
  229. Parse(values, "c", -200);
  230. Parse(values, "d", INT_MAX);
  231. Parse(values, "0", INT_MAX);
  232. Parse(values, "2", INT_MAX);
  233. const PropertyId numbers =
  234. specification.RegisterProperty("numbers", "", false, false).AddParser("keyword", "a=10, b=20, c=30").AddParser("number").GetId();
  235. Parse(numbers, "a", 10);
  236. Parse(numbers, "b", 20);
  237. Parse(numbers, "c", 30);
  238. Parse(numbers, "d", INT_MAX);
  239. Parse(numbers, "0", 0);
  240. Parse(numbers, "2", 2);
  241. Parse(numbers, "20", 20);
  242. Rml::Shutdown();
  243. }
  244. TEST_CASE("PropertyParser.InvalidShorthands")
  245. {
  246. TestsSystemInterface system_interface;
  247. TestsRenderInterface render_interface;
  248. SetRenderInterface(&render_interface);
  249. SetSystemInterface(&system_interface);
  250. Rml::Initialise();
  251. ElementPtr element = Factory::InstanceElement(nullptr, "*", "div", {});
  252. struct TestCase {
  253. bool expected_result;
  254. String name;
  255. String value;
  256. };
  257. Vector<TestCase> tests = {
  258. {true, "margin", "10px "}, //
  259. {true, "margin", "10px 20px "}, //
  260. {true, "margin", "10px 20px 30px "}, //
  261. {true, "margin", "10px 20px 30px 40px"}, //
  262. {false, "margin", ""}, // Too few values
  263. {false, "margin", "10px 20px 30px 40px 50px"}, // Too many values
  264. {true, "flex", "1 2 3px"}, //
  265. {false, "flex", "1 2 3px 4"}, // Too many values
  266. {false, "flex", "1px 2 3 4"}, // Wrong order
  267. {true, "perspective-origin", "center center"}, //
  268. {true, "perspective-origin", "left top"}, //
  269. {false, "perspective-origin", "center center center"}, // Too many values
  270. {false, "perspective-origin", "left top 50px"}, // Too many values
  271. {false, "perspective-origin", "50px 50px left"}, // Too many values
  272. {false, "perspective-origin", "top left"}, // Wrong order
  273. {false, "perspective-origin", "50px left"}, // Wrong order
  274. {true, "font", "20px arial"}, //
  275. {false, "font", "arial 20px"}, // Wrong order
  276. {true, "decorator", "gradient(vertical blue red)"}, //
  277. {false, "decorator", "gradient(blue red vertical)"}, // Wrong order
  278. {false, "decorator", "gradient(blue vertical red)"}, // Wrong order
  279. {false, "decorator", "gradient(vertical blue red green)"}, // Too many values
  280. {true, "filter", "drop-shadow(blue 10px 20px 30px)"}, //
  281. {false, "filter", "drop-shadow(10px 20px 30px blue)"}, // Wrong order
  282. {false, "filter", "drop-shadow(10px blue 20px 30px)"}, // Wrong order
  283. {false, "filter", "drop-shadow(blue 10px 20px 30px 40px)"}, // Too many values
  284. {true, "overflow", "hidden"}, //
  285. {true, "overflow", "scroll hidden"}, //
  286. {false, "overflow", ""}, // Too few values
  287. {false, "overflow", "scroll hidden scroll"}, // Too many values
  288. };
  289. for (const TestCase& test : tests)
  290. {
  291. PropertyDictionary properties;
  292. INFO(test.name, ": ", test.value);
  293. CHECK(StyleSheetSpecification::ParsePropertyDeclaration(properties, test.name, test.value) == test.expected_result);
  294. // Ensure we get a warning when trying to set the invalid property on an element.
  295. system_interface.SetNumExpectedWarnings(test.expected_result ? 0 : 1);
  296. CHECK(element->SetProperty(test.name, test.value) == test.expected_result);
  297. }
  298. element.reset();
  299. Rml::Shutdown();
  300. }