From cddf11235fa07036eb5d9503b265e37912dc42bd Mon Sep 17 00:00:00 2001 From: speedie Date: Mon, 6 May 2024 13:19:16 +0200 Subject: [PATCH] Add position functions for HTMLElementProperties. --- include/docpp.hpp | 47 +++++++++++++++++++++++++++++--- src/docpp.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++---- tests/test.cpp | 27 ++++++++++++++++++- 3 files changed, 132 insertions(+), 10 deletions(-) diff --git a/include/docpp.hpp b/include/docpp.hpp index 2772390..83711f3 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -110,14 +110,53 @@ namespace docpp { * @brief Get the properties of the element * @return std::vector The properties of the element */ - std::vector get() const; + std::vector getProperties() const; /** * @brief Set the properties of the element * @param properties The properties to set */ void set(const std::vector& properties); /** - * @brief Add a property to the element + * @brief Erase a property from the element + * @param index The index of the property to erase + */ + void erase(const int index); + /** + * @brief Find a property in the element + * @param property The property to find + * @return int The index of the property + */ + int find(const HTMLProperty& property); + /** + * @brief Find a property in the element + * @param str The property to find + * @return int The index of the property + */ + int find(const std::string& str); + /** + * @brief Swap two properties in the element + * @param index1 The index of the first property + * @param index2 The index of the second property + */ + void swap(const int index1, const int index2); + /** + * @brief Swap two properties in the element + * @param property1 The first property + * @param property2 The second property + */ + void swap(const HTMLProperty& property1, const HTMLProperty& property2); + /** + * @brief Get the size of the element + * @return int The size of the element + */ + int size() const; + /** + * @brief Prepend a property to the element + * @param property The property to add + */ + void push_front(const HTMLProperty& property); + /** + * @brief Append a property to the element * @param property The property to add */ void push_back(const HTMLProperty& property); @@ -332,12 +371,12 @@ namespace docpp { * @brief Get the elements of the section * @return std::vector The elements of the section */ - std::vector getHTMLElements(); + std::vector getHTMLElements() const; /** * @brief Get the sections of the section * @return std::vector The sections of the section */ - std::vector getHTMLSections(); + std::vector getHTMLSections() const; /** * @brief Dump the entire section. diff --git a/src/docpp.cpp b/src/docpp.cpp index ab937a8..8caf2f2 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -59,7 +59,7 @@ docpp::HTML::HTMLElementProperties::HTMLElementProperties(const docpp::HTML::HTM this->push_back(property); } -std::vector docpp::HTML::HTMLElementProperties::get() const { +std::vector docpp::HTML::HTMLElementProperties::getProperties() const { return this->properties; } @@ -67,10 +67,68 @@ void docpp::HTML::HTMLElementProperties::set(const std::vectorproperties = properties; } +void docpp::HTML::HTMLElementProperties::erase(const int index) { + if (index < 0 || index >= this->properties.size()) { + throw std::out_of_range("Index out of range"); + } + + this->properties.erase(this->properties.begin() + index); +} + +void docpp::HTML::HTMLElementProperties::push_front(const docpp::HTML::HTMLProperty& property) { + this->properties.insert(this->properties.begin(), property); +} + void docpp::HTML::HTMLElementProperties::push_back(const docpp::HTML::HTMLProperty& property) { this->properties.push_back(property); } +int docpp::HTML::HTMLElementProperties::find(const docpp::HTML::HTMLProperty& property) { + for (int i{0}; i < this->properties.size(); i++) { + if (!this->properties.at(i).getKey().compare(property.getKey())) { + return i; + } else if (!this->properties.at(i).getValue().compare(property.getValue())) { + return i; + } else if (this->properties.at(i).getValue().find(property.getValue()) != std::string::npos) { + return i; + } else if (this->properties.at(i).getKey().find(property.getKey()) != std::string::npos) { + return i; + } else if (this->properties.at(i).get() == property.get()) { + return i; + } + } + + return docpp::HTML::HTMLElementProperties::npos; +} + +int docpp::HTML::HTMLElementProperties::find(const std::string& str) { + for (int i{0}; i < this->properties.size(); i++) { + if (!this->properties.at(i).getKey().compare(str) || !this->properties.at(i).getValue().compare(str)) { + return i; + } else if (this->properties.at(i).getKey().find(str) != std::string::npos || this->properties.at(i).getValue().find(str) != std::string::npos) { + return i; + } + } + + return docpp::HTML::HTMLElementProperties::npos; +} + +int docpp::HTML::HTMLElementProperties::size() const { + return this->properties.size(); +} + +void docpp::HTML::HTMLElementProperties::swap(const int index1, const int index2) { + if (index1 < 0 || index1 >= this->properties.size() || index2 < 0 || index2 >= this->properties.size()) { + throw std::out_of_range("Index out of range"); + } + + std::swap(this->properties[index1], this->properties[index2]); +} + +void docpp::HTML::HTMLElementProperties::swap(const docpp::HTML::HTMLProperty& property1, const docpp::HTML::HTMLProperty& property2) { + this->swap(this->find(property1), this->find(property2)); +} + docpp::HTML::HTMLElement::HTMLElement(const std::string& tag, const HTMLElementProperties& properties, const std::string& data, const int type) { this->set(tag, properties, data, type); } @@ -87,7 +145,7 @@ std::string docpp::HTML::HTMLElement::get(const int formatting) const { ret += "<" + this->tag; - for (const auto& it : this->properties.get()) { + for (const auto& it : this->properties.getProperties()) { if (!it.getKey().compare("")) continue; if (!it.getValue().compare("")) continue; @@ -292,7 +350,7 @@ int docpp::HTML::HTMLSection::size() const { return this->index; } -std::vector docpp::HTML::HTMLSection::getHTMLElements() { +std::vector docpp::HTML::HTMLSection::getHTMLElements() const { std::vector ret{}; ret.reserve(this->index); for (int i{0}; i < this->index; i++) { @@ -303,7 +361,7 @@ std::vector docpp::HTML::HTMLSection::getHTMLElements( return std::move(ret); } -std::vector docpp::HTML::HTMLSection::getHTMLSections() { +std::vector docpp::HTML::HTMLSection::getHTMLSections() const { std::vector ret{}; ret.reserve(this->index); @@ -321,7 +379,7 @@ std::string docpp::HTML::HTMLSection::get(const int formatting) const { ret += "<" + this->tag; - for (const auto& it : this->properties.get()) { + for (const auto& it : this->properties.getProperties()) { if (!it.getKey().compare("")) continue; if (!it.getValue().compare("")) continue; diff --git a/tests/test.cpp b/tests/test.cpp index 393bbaa..e513da4 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -238,7 +238,32 @@ SCENARIO("Test HTML", "[HTML]") { REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); }; - std::vector tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14}; + auto test15 = []() { + docpp::HTML::HTMLElementProperties prop{}; + + prop.push_back(docpp::HTML::HTMLProperty(std::pair("id", "test_id"))); + prop.push_back(docpp::HTML::HTMLProperty(std::pair("class", "class1 class2 class3"))); + + REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "

"); + + const int pos = prop.find("class"); + + REQUIRE(pos != docpp::HTML::HTMLElementProperties::npos); + + const int pos2 = prop.find("class2"); + + REQUIRE(pos2 != docpp::HTML::HTMLElementProperties::npos); + + const int pos3 = prop.find("class4"); + + REQUIRE(pos3 == docpp::HTML::HTMLElementProperties::npos); + + prop.erase(pos); + + REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "

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