PropertySpecification.cpp 16 KB

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