XMLParser.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/TestsShell.h"
  29. #include <RmlUi/Core/ComputedValues.h>
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <RmlUi/Core/ElementText.h>
  34. #include <RmlUi/Core/Factory.h>
  35. #include <doctest.h>
  36. using namespace Rml;
  37. static const String document_xml_tags_in_css = R"(
  38. <rml>
  39. <head>
  40. <style>
  41. body {
  42. /* <body> */
  43. width: 200px;
  44. height: 200px;
  45. background-color: #00ff00;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. </body>
  51. </rml>
  52. )";
  53. static const String document_escaping = R"(
  54. <rml>
  55. <head>
  56. <style>
  57. p {
  58. font-family: LatoLatin;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <p id="p">&#x20AC;&#8364;</p>
  64. </body>
  65. </rml>
  66. )";
  67. static const String document_escaping_tags = R"(
  68. <rml>
  69. <head>
  70. <style>
  71. * {
  72. font-family: LatoLatin;
  73. }
  74. </style>
  75. </head>
  76. <body>&lt;p&gt;&amp;lt;span/&amp;gt;&lt;/p&gt;</body>
  77. </rml>
  78. )";
  79. TEST_CASE("XMLParser")
  80. {
  81. Context* context = TestsShell::GetContext();
  82. REQUIRE(context);
  83. // Style nodes should accept XML reserved characters, see https://github.com/mikke89/RmlUi/issues/341
  84. ElementDocument* document = context->LoadDocumentFromMemory(document_xml_tags_in_css);
  85. REQUIRE(document);
  86. document->Show();
  87. TestsShell::RenderLoop();
  88. const Colourb background = document->GetComputedValues().background_color();
  89. CHECK(background.red == 0);
  90. CHECK(background.green == 0xff);
  91. CHECK(background.blue == 0);
  92. CHECK(background.alpha == 0xff);
  93. document->Close();
  94. TestsShell::ShutdownShell();
  95. }
  96. TEST_CASE("XMLParser.escaping")
  97. {
  98. Context* context = TestsShell::GetContext();
  99. REQUIRE(context);
  100. ElementDocument* document = context->LoadDocumentFromMemory(document_escaping);
  101. REQUIRE(document);
  102. document->Show();
  103. TestsShell::RenderLoop();
  104. auto element = document->GetElementById("p");
  105. REQUIRE(element);
  106. CHECK(element->GetInnerRML() == "\xe2\x82\xac\xe2\x82\xac");
  107. document->Close();
  108. TestsShell::ShutdownShell();
  109. }
  110. TEST_CASE("XMLParser.escaping_tags")
  111. {
  112. Context* context = TestsShell::GetContext();
  113. REQUIRE(context);
  114. ElementDocument* document = context->LoadDocumentFromMemory(document_escaping_tags);
  115. REQUIRE(document);
  116. document->Show();
  117. TestsShell::RenderLoop();
  118. CHECK(document->GetNumChildren() == 1);
  119. CHECK(document->GetFirstElementChild()->GetTagName() == "#text");
  120. // Text-access should yield decoded value, while RML-access should yield encoded value
  121. CHECK(static_cast<ElementText*>(document->GetFirstChild())->GetText() == "<p>&lt;span/&gt;</p>");
  122. CHECK(document->GetInnerRML() == "&lt;p&gt;&amp;lt;span/&amp;gt;&lt;/p&gt;");
  123. document->Close();
  124. TestsShell::ShutdownShell();
  125. }
  126. TEST_CASE("XMLParser.comments_and_cdata")
  127. {
  128. const String document_source_pre = R"(
  129. <rml>
  130. <head>
  131. <style>
  132. body {
  133. font-family: LatoLatin;
  134. }
  135. </style>
  136. </head>
  137. <body>)";
  138. const String document_source_post = R"(</body></rml>)";
  139. struct TestCase {
  140. String rml;
  141. String expected_parsed_rml;
  142. };
  143. const TestCase tests[] = {
  144. {"<!-- <xyz> -->", ""},
  145. {"<!--<xyz>-->", ""},
  146. {"<!-- <xyz> ->-->", ""},
  147. {"<!-- <xyz> -- >-->", ""},
  148. {"<!-- <xyz> --->", ""},
  149. {"<!-- <xyz> ---->", ""},
  150. {"<!--- <xyz> ---->", ""},
  151. {"<!-- <p> --><p>hello</p><!-- </p> -->", "<p>hello</p>"},
  152. {"<![CDATA[hello]]>", "hello"},
  153. {"<![CDATA[hello]]]>", "hello]"},
  154. {"<![CDATA[hello]]world]]>", "hello]]world"},
  155. {"<![CDATA[\"hello\"]]>", "&quot;hello&quot;"},
  156. {"<![CDATA[<p>world</p>]]>", "<p>world</p>"},
  157. };
  158. Context* context = TestsShell::GetContext();
  159. REQUIRE(context);
  160. for (const TestCase& test : tests)
  161. {
  162. ElementDocument* document = context->LoadDocumentFromMemory(document_source_pre + test.rml + document_source_post);
  163. REQUIRE(document);
  164. CHECK(document->GetInnerRML() == test.expected_parsed_rml);
  165. document->Close();
  166. context->Update();
  167. }
  168. TestsShell::ShutdownShell();
  169. }