PropertySpecification.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. bool ParsePropertyValues(StringList& values_list, const String& values, SplitOption split_option) const
  44. {
  45. return 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. bool success = 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(success);
  85. CHECK(parsed_values == expected.values);
  86. };
  87. Parse("red", "red");
  88. Parse(" red ", "red");
  89. Parse("inline-block", "inline-block");
  90. Parse("none red", {"none", "red"});
  91. Parse("none red", {"none", "red"});
  92. Parse("none\t \r \nred", {"none", "red"});
  93. Parse("none red", "none red", SplitOption::None);
  94. Parse(" none red ", "none red", SplitOption::None);
  95. Parse("none red", "none red", SplitOption::None);
  96. Parse("none\t \r \nred", "none\t \r \nred", SplitOption::None);
  97. Parse("none,red", "none,red", SplitOption::None);
  98. Parse(" \"none,red\" ", "none,red", SplitOption::None);
  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");
  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("none, , ,red", {"none", "red"}, SplitOption::Comma);
  112. Parse("\"string with spaces\"", "string with spaces");
  113. Parse("\"string with spaces\" two", {"string with spaces", "two"});
  114. Parse("\"string with spaces\"two", {"string with spaces", "two"});
  115. Parse("\"string with spaces\"two", "string with spaces two", SplitOption::None);
  116. Parse("\"string (with) ((parenthesis\" two", {"string (with) ((parenthesis", "two"});
  117. Parse("\"none,,red\" two", {"none,,red", "two"});
  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"});
  121. Parse("aa(\"bb cc \\) dd\") ee", "aa(\"bb cc \\) dd\") ee", SplitOption::Comma);
  122. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\"),", "aa,", "bb() cc"});
  123. Parse("none(\"long string\"), aa, \"bb() cc\"", {"none(\"long string\")", "aa", "\"bb() cc\""}, SplitOption::Comma);
  124. Parse("none(\"long string\"), aa, bb() cc", {"none(\"long string\")", "aa", "bb() cc"}, SplitOption::Comma);
  125. Parse("tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )", "tiled-horizontal( title-bar-l, title-bar-c, title-bar-r )");
  126. 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 )");
  127. Parse("tiled-horizontal( title-bar-l, title-bar-c )", "tiled-horizontal( title-bar-l, title-bar-c )", SplitOption::Comma);
  128. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  129. {"linear-gradient(110deg, #fff, #000 10%)", "border-box,", "image(invader.png)"});
  130. Parse("linear-gradient(110deg, #fff, #000 10%) border-box, image(invader.png)",
  131. {"linear-gradient(110deg, #fff, #000 10%) border-box", "image(invader.png)"}, SplitOption::Comma);
  132. Parse(R"(image( a\) b ))", {R"(image( a\))", "b", ")"});
  133. Parse(R"(image( a\) b ))", R"(image( a\) b ))", SplitOption::Comma);
  134. Parse(R"(image( ))", R"(image( ))");
  135. Parse(R"(image( a\\b ))", R"(image( a\\b ))");
  136. Parse(R"(image( a\\\b ))", R"(image( a\\\b ))");
  137. Parse(R"(image( a\\\\b ))", R"(image( a\\\\b ))");
  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. Rml::Shutdown();
  144. }
  145. TEST_CASE("PropertySpecification.string")
  146. {
  147. TestsSystemInterface system_interface;
  148. TestsRenderInterface render_interface;
  149. SetRenderInterface(&render_interface);
  150. SetSystemInterface(&system_interface);
  151. Rml::Initialise();
  152. PropertySpecification specification(1, 0);
  153. const PropertyId id = specification.RegisterProperty("name", "", false, false).AddParser("string").GetId();
  154. // Parse value into the <string> property.
  155. auto Parse = [&](const String& test_value, const String& expected) {
  156. PropertyDictionary properties;
  157. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  158. const auto num_properties = properties.GetProperties().size();
  159. CHECK(parse_success);
  160. CHECK(num_properties == 1);
  161. CHECK(properties.GetProperty(id));
  162. if (const Property* property = properties.GetProperty(id))
  163. {
  164. const String parsed_value = property->Get<String>();
  165. CHECK_MESSAGE(parsed_value == expected, "Test value: ", test_value);
  166. }
  167. };
  168. Parse("a", "a");
  169. Parse(" a ", "a");
  170. Parse("green", "green");
  171. Parse("image(ress:///.ress#/images/a.png)", "image(ress:///.ress#/images/a.png)");
  172. Parse(R"(image("ress:///.ress#/images/a.png"))", R"(image("ress:///.ress#/images/a.png"))");
  173. Parse(R"("ress:///.ress#/images/a.png")", R"(ress:///.ress#/images/a.png)");
  174. Parse(R"("escaped\"quotes")", R"(escaped"quotes)");
  175. Parse(R"("escaped\\backslash")", R"(escaped\backslash)");
  176. Parse(R"("bad_\escape")", R"(bad_\escape)");
  177. Parse(R"(C:\Windows\test.png)", R"(C:\Windows\test.png)");
  178. Parse(R"("C:\Windows\test.png")", R"(C:\Windows\test.png)");
  179. Parse(R"(C:\\Windows\\test.png)", R"(C:\\Windows\\test.png)");
  180. Parse(R"("C:\\Windows\\test.png")", R"(C:\Windows\test.png)");
  181. Parse(R"(\\host\test.png)", R"(\\host\test.png)");
  182. Parse(R"(\\\host\test.png)", R"(\\\host\test.png)");
  183. Parse(R"("\\host\\test.png")", R"(\host\test.png)");
  184. Parse("image(a)", "image(a)");
  185. Parse(R"(image(a))", R"(image(a))");
  186. Parse(R"(image(a, "b"))", R"(image(a, "b"))");
  187. Parse(R"V("image(a, \"b\")")V", R"V(image(a, "b"))V");
  188. Rml::Shutdown();
  189. }
  190. TEST_CASE("PropertyParser.Keyword")
  191. {
  192. TestsSystemInterface system_interface;
  193. TestsRenderInterface render_interface;
  194. SetRenderInterface(&render_interface);
  195. SetSystemInterface(&system_interface);
  196. Rml::Initialise();
  197. // Test keyword parser. Ensure that the keyword values are correct.
  198. PropertySpecification specification(20, 0);
  199. auto Parse = [&](const PropertyId id, const String& test_value, int expected_value) {
  200. PropertyDictionary properties;
  201. const bool parse_success = specification.ParsePropertyDeclaration(properties, id, test_value);
  202. if (expected_value == INT_MAX)
  203. {
  204. CHECK(!parse_success);
  205. }
  206. else
  207. {
  208. CHECK(parse_success);
  209. CHECK(properties.GetProperties().size() == 1);
  210. const int parsed_value = properties.GetProperty(id)->Get<int>();
  211. CHECK_MESSAGE(parsed_value == expected_value, "Test value: ", test_value);
  212. const String parsed_value_str = properties.GetProperty(id)->ToString();
  213. CHECK(parsed_value_str == test_value);
  214. }
  215. };
  216. const PropertyId simple = specification.RegisterProperty("simple", "", false, false).AddParser("keyword", "a, b, c").GetId();
  217. Parse(simple, "a", 0);
  218. Parse(simple, "b", 1);
  219. Parse(simple, "c", 2);
  220. Parse(simple, "d", INT_MAX);
  221. Parse(simple, "0", INT_MAX);
  222. Parse(simple, "2", INT_MAX);
  223. const PropertyId values = specification.RegisterProperty("values", "", false, false).AddParser("keyword", "a=50, b, c=-200").GetId();
  224. Parse(values, "a", 50);
  225. Parse(values, "b", 51);
  226. Parse(values, "c", -200);
  227. Parse(values, "d", INT_MAX);
  228. Parse(values, "0", INT_MAX);
  229. Parse(values, "2", INT_MAX);
  230. const PropertyId numbers =
  231. specification.RegisterProperty("numbers", "", false, false).AddParser("keyword", "a=10, b=20, c=30").AddParser("number").GetId();
  232. Parse(numbers, "a", 10);
  233. Parse(numbers, "b", 20);
  234. Parse(numbers, "c", 30);
  235. Parse(numbers, "d", INT_MAX);
  236. Parse(numbers, "0", 0);
  237. Parse(numbers, "2", 2);
  238. Parse(numbers, "20", 20);
  239. Rml::Shutdown();
  240. }
  241. TEST_CASE("PropertyParser.InvalidShorthands")
  242. {
  243. TestsSystemInterface system_interface;
  244. TestsRenderInterface render_interface;
  245. SetRenderInterface(&render_interface);
  246. SetSystemInterface(&system_interface);
  247. Rml::Initialise();
  248. ElementPtr element = Factory::InstanceElement(nullptr, "*", "div", {});
  249. struct TestCase {
  250. bool expected_result;
  251. String name;
  252. String value;
  253. };
  254. Vector<TestCase> tests = {
  255. {true, "margin", "10px "}, //
  256. {true, "margin", "10px 20px "}, //
  257. {true, "margin", "10px 20px 30px "}, //
  258. {true, "margin", "10px 20px 30px 40px"}, //
  259. {false, "margin", ""}, // Too few values
  260. {false, "margin", "10px 20px 30px 40px 50px"}, // Too many values
  261. {true, "flex", "1 2 3px"}, //
  262. {false, "flex", "1 2 3px 4"}, // Too many values
  263. {false, "flex", "1px 2 3 4"}, // Wrong order
  264. {true, "perspective-origin", "center center"}, //
  265. {true, "perspective-origin", "left top"}, //
  266. {false, "perspective-origin", "center center center"}, // Too many values
  267. {false, "perspective-origin", "left top 50px"}, // Too many values
  268. {false, "perspective-origin", "50px 50px left"}, // Too many values
  269. {false, "perspective-origin", "top left"}, // Wrong order
  270. {false, "perspective-origin", "50px left"}, // Wrong order
  271. {true, "font", "20px arial"}, //
  272. {false, "font", "arial 20px"}, // Wrong order
  273. {true, "decorator", "gradient(vertical blue red)"}, //
  274. {false, "decorator", "gradient(blue red vertical)"}, // Wrong order
  275. {false, "decorator", "gradient(blue vertical red)"}, // Wrong order
  276. {false, "decorator", "gradient(vertical blue red green)"}, // Too many values
  277. {true, "filter", "drop-shadow(blue 10px 20px 30px)"}, //
  278. {false, "filter", "drop-shadow(10px 20px 30px blue)"}, // Wrong order
  279. {false, "filter", "drop-shadow(10px blue 20px 30px)"}, // Wrong order
  280. {false, "filter", "drop-shadow(blue 10px 20px 30px 40px)"}, // Too many values
  281. {true, "overflow", "hidden"}, //
  282. {true, "overflow", "scroll hidden"}, //
  283. {false, "overflow", ""}, // Too few values
  284. {false, "overflow", "scroll hidden scroll"}, // Too many values
  285. };
  286. for (const TestCase& test : tests)
  287. {
  288. PropertyDictionary properties;
  289. INFO(test.name, ": ", test.value);
  290. CHECK(StyleSheetSpecification::ParsePropertyDeclaration(properties, test.name, test.value) == test.expected_result);
  291. // Ensure we get a warning when trying to set the invalid property on an element.
  292. system_interface.SetNumExpectedWarnings(test.expected_result ? 0 : 1);
  293. CHECK(element->SetProperty(test.name, test.value) == test.expected_result);
  294. }
  295. element.reset();
  296. Rml::Shutdown();
  297. }