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.
This commit is contained in:
Jacob 2024-05-06 02:45:15 +02:00
parent 8f1f175138
commit f9bf0bbfd4
3 changed files with 49 additions and 1 deletions

View file

@ -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

View file

@ -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;
}

View file

@ -192,7 +192,29 @@ SCENARIO("Test HTML", "[HTML]") {
REQUIRE(section.get() == "<html><p>Test 0</p><p>Test 1</p><p>Test 2</p><p>Test 3</p></html>");
};
std::vector<void (*)()> 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("<p>Test 2</p>")};
REQUIRE(pos != docpp::HTML::HTMLSection::npos);
const int pos2{section.find("<p>Test 6</p>")};
section.erase(pos);
section.erase(pos2);
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p><p>Test 4</p><p>Test 5</p></html>");
};
std::vector<void (*)()> tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13};
for (const auto& test : tests) {
test();