From f9bf0bbfd40078177a0b7eff99292c2e31168ebb Mon Sep 17 00:00:00 2001 From: speedie Date: Mon, 6 May 2024 02:45:15 +0200 Subject: [PATCH] Add new HTMLSection::find() method that takes an std::string parameter containing a deserialized section or element, and returns the position of an identical section or element, or npos. --- include/docpp.hpp | 6 ++++++ src/docpp.cpp | 20 ++++++++++++++++++++ tests/test.cpp | 24 +++++++++++++++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/include/docpp.hpp b/include/docpp.hpp index 87773f2..19f132e 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -256,6 +256,12 @@ namespace docpp { * @return int The index of the section */ int find(const HTMLSection& section); + /** + * @brief Find an element or section in the section + * @param str The element or section to find + * @return int The index of the element or section + */ + int find(const std::string& str); /** * @brief Insert an element into the section * @param index The index to insert the element diff --git a/src/docpp.cpp b/src/docpp.cpp index 23b7ef4..862bc0a 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -267,6 +267,26 @@ int docpp::HTML::HTMLSection::find(const HTMLSection& section) { return docpp::HTML::HTMLSection::npos; } +int docpp::HTML::HTMLSection::find(const std::string& str) { + const auto elements = this->getHTMLElements(); + + for (int i{0}; i < elements.size(); i++) { + if (!elements.at(i).get().compare(str)) { + return i; + } + } + + const auto sections = this->getHTMLSections(); + + for (int i{0}; i < sections.size(); i++) { + if (!sections.at(i).get().compare(str)) { + return i; + } + } + + return docpp::HTML::HTMLSection::npos; +} + int docpp::HTML::HTMLSection::size() const { return this->index; } diff --git a/tests/test.cpp b/tests/test.cpp index 826d5ac..6ced40b 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -192,7 +192,29 @@ SCENARIO("Test HTML", "[HTML]") { REQUIRE(section.get() == "

Test 0

Test 1

Test 2

Test 3

"); }; - std::vector tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12}; + auto test13 = []() { + docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {}); + + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1")); + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2")); + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3")); + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 4")); + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 5")); + section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 6")); + + const int pos{section.find("

Test 2

")}; + + REQUIRE(pos != docpp::HTML::HTMLSection::npos); + + const int pos2{section.find("

Test 6

")}; + + section.erase(pos); + section.erase(pos2); + + REQUIRE(section.get() == "

Test 1

Test 3

Test 4

Test 5

"); + }; + + std::vector tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13}; for (const auto& test : tests) { test();