diff --git a/include/docpp.hpp b/include/docpp.hpp index 5b9105a..2772390 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -496,6 +496,46 @@ namespace docpp { * @param property The property to push */ void push_back(const CSSProperty& property); + /** + * @brief Insert a property into the element + * @param index The index to insert the property + * @param property The property to insert + */ + void insert(const int index, const CSSProperty& property); + /** + * @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 CSSProperty& 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 CSSProperty& property1, const CSSProperty& property2); + /** + * @brief Get the size of the element + * @return int The size of the element + */ + int size() const; /** * @brief Set the properties of the element * @param properties The properties to set diff --git a/src/docpp.cpp b/src/docpp.cpp index 054f8c1..ab937a8 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -189,6 +189,7 @@ void docpp::HTML::HTMLSection::push_back(const HTMLSection& section) { void docpp::HTML::HTMLSection::erase(const int index) { bool erased{false}; + if (this->elements.find(index) != this->elements.end()) { this->elements.erase(index); erased = true; @@ -450,6 +451,58 @@ void docpp::CSS::CSSElement::push_back(const CSSProperty& property) { this->element.second.push_back(property); } +void docpp::CSS::CSSElement::insert(const int index, const CSSProperty& property) { + if (index < 0 || index >= this->element.second.size()) { + throw std::out_of_range("Index out of range"); + } + + this->element.second.insert(this->element.second.begin() + index, property); +} + +void docpp::CSS::CSSElement::erase(const int index) { + if (index < 0 || index >= this->element.second.size()) { + throw std::out_of_range("Index out of range"); + } + + this->element.second.erase(this->element.second.begin() + index); +} + +int docpp::CSS::CSSElement::find(const CSSProperty& property) { + for (int i{0}; i < this->element.second.size(); i++) { + if (this->element.second.at(i).get() == property.get()) { + return i; + } + } + + return docpp::CSS::CSSElement::npos; +} + +int docpp::CSS::CSSElement::find(const std::string& str) { + for (int i{0}; i < this->element.second.size(); i++) { + if (!this->element.second.at(i).getKey().compare(str) || !this->element.second.at(i).getValue().compare(str)) { + return i; + } + } + + return docpp::CSS::CSSElement::npos; +} + +int docpp::CSS::CSSElement::size() const { + return this->element.second.size(); +} + +void docpp::CSS::CSSElement::swap(const int index1, const int index2) { + if (index1 < 0 || index1 >= this->element.second.size() || index2 < 0 || index2 >= this->element.second.size()) { + throw std::out_of_range("Index out of range"); + } + + std::swap(this->element.second[index1], this->element.second[index2]); +} + +void docpp::CSS::CSSElement::swap(const CSSProperty& property1, const CSSProperty& property2) { + this->swap(this->find(property1), this->find(property2)); +} + std::string docpp::CSS::CSSElement::get(const int formatting) const { std::string ret{}; diff --git a/tests/test.cpp b/tests/test.cpp index 0c1d323..393bbaa 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -218,7 +218,27 @@ SCENARIO("Test HTML", "[HTML]") { 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}; + auto test14 = []() { + docpp::CSS::CSSElement element{"p", {{"color", "red"}, {"font-size", "16px"}, {"font-family", "Arial"}}}; + + const int red = element.find("color"); + + REQUIRE(red != docpp::CSS::CSSElement::npos); + + const int blue = element.find("blue"); + + REQUIRE(blue == docpp::CSS::CSSElement::npos); + + element.erase(red); + + REQUIRE(element.get() == "p {font-size: 16px;font-family: Arial;}"); + + element.insert(red, docpp::CSS::CSSProperty("color", "red")); + + 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}; for (const auto& test : tests) { test();