/* * This source file is part of RmlUi, the HTML/CSS Interface Middleware * * For the latest information, see http://github.com/mikke89/RmlUi * * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd * Copyright (c) 2019 The RmlUi Team, and contributors * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #include "../Common/TestsInterface.h" #include #include #include #include #include #include using namespace Rml; static const String doc_begin = R"( Demo

)"; struct Selector { String selector; String expected_ids; }; static const Vector selectors = { { "span", "Y D0 D1 F0" }, { ".hello", "X Z H" }, { ".hello.world", "Z" }, { "div.hello", "X Z" }, { "body .hello", "X Z H" }, { "body>.hello", "X Z" }, { "body > .hello", "X Z" }, { ".parent *", "A B C D D0 D1 E F F0 G H" }, { ".parent > *", "A B C D E F G H" }, { ":checked", "I" }, { ".parent :nth-child(odd)", "A C D0 E F0 G" }, { ".parent > :nth-child(even)", "B D F H" }, { ":first-child", "X A D0 F0" }, { ":last-child", "D1 F0 H I" }, { "p:nth-child(2)", "B" }, { "h1:nth-child(2)", "" }, { "p:nth-child(3n+1)", "D G" }, { "p:nth-child(3n + 1)", "D G" }, { "#P > :nth-last-child(2n+1)", "B D F H" }, { "#P p:nth-of-type(odd)", "B D G" }, { "p:nth-last-of-type(3n+1)", "D H" }, { ":first-of-type", "X Y A B D0 E F0 I" }, { ":last-of-type", "Y P A D1 E F0 H I" }, { ":only-child", "F0" }, { ":only-of-type", "Y A E F0 I" }, { "span:empty", "Y D0 D1 F0" }, { ".hello.world, #P span, #I", "Z D0 D1 F0 I" }, { "body * span", "D0 D1 F0" }, }; // Recursively iterate through 'element' and all of its descendants to find all // elements matching a particular property used to tag matching selectors. static void GetMatchingIds(String& matching_ids, Element* element) { String id = element->GetId(); if (!id.empty() && element->GetProperty("drag") == (int)Style::Drag::Drag) { matching_ids += id + ' '; } for (int i = 0; i < element->GetNumChildren(); i++) { GetMatchingIds(matching_ids, element->GetChild(i)); } } // Return the list of IDs that should match the above 'selectors.expected_ids'. static String ElementListToIds(const ElementList& elements) { String result; for (Element* element : elements) { result += element->GetId() + ' '; } if (!result.empty()) result.pop_back(); return result; } TEST_CASE("Selectors") { const Vector2i window_size(1024, 768); TestsSystemInterface system_interface; TestsRenderInterface render_interface; SetRenderInterface(&render_interface); SetSystemInterface(&system_interface); Initialise(); Context* context = Rml::CreateContext("main", window_size); REQUIRE(context); SUBCASE("RCSS document selectors") { for (const Selector& selector : selectors) { const String selector_css = selector.selector + " { drag: drag; } "; const String document_string = doc_begin + selector_css + doc_end; ElementDocument* document = context->LoadDocumentFromMemory(document_string); REQUIRE(document); String matching_ids; GetMatchingIds(matching_ids, document); if (!matching_ids.empty()) matching_ids.pop_back(); CHECK_MESSAGE(matching_ids == selector.expected_ids, "Selector: " << selector.selector); context->UnloadDocument(document); } } SUBCASE("QuerySelector(All)") { const String document_string = doc_begin + doc_end; ElementDocument* document = context->LoadDocumentFromMemory(document_string); REQUIRE(document); for (const Selector& selector : selectors) { ElementList elements; document->QuerySelectorAll(elements, selector.selector); String matching_ids = ElementListToIds(elements); Element* first_element = document->QuerySelector(selector.selector); if (first_element) { CHECK_MESSAGE(first_element == elements[0], "QuerySelector does not return the first match of QuerySelectorAll."); } else { CHECK_MESSAGE(elements.empty(), "QuerySelector found nothing, while QuerySelectorAll found " << elements.size() << " element(s)."); } CHECK_MESSAGE(matching_ids == selector.expected_ids, "QuerySelector: " << selector.selector); context->UnloadDocument(document); } } Rml::Shutdown(); }