From 298a3c96b699936d6c93912c4acd4c5bb1d823e7 Mon Sep 17 00:00:00 2001 From: speedie Date: Mon, 6 May 2024 11:06:45 +0200 Subject: [PATCH] Implement push_front() method for CSSElement, implement find() method that takes an std::string for CSSStylesheet. --- include/docpp.hpp | 13 ++++++++++++- src/docpp.cpp | 14 ++++++++++++++ tests/test.cpp | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/docpp.hpp b/include/docpp.hpp index 19f132e..5b9105a 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -487,7 +487,12 @@ namespace docpp { CSSElement() = default; /** - * @brief Push a property to the element + * @brief Prepend a property to the element + * @param property The property to push + */ + void push_front(const CSSProperty& property); + /** + * @brief Append a property to the element * @param property The property to push */ void push_back(const CSSProperty& property); @@ -565,6 +570,12 @@ namespace docpp { * @return int The index of the element */ int find(const CSSElement& element); + /** + * @brief Find an element in the stylesheet + * @param str The element to find, either the tag or the stylesheet itself + * @return int The index of the element + */ + int find(const std::string& str); /** * @brief Get the size of the stylesheet * @return int The size of the stylesheet diff --git a/src/docpp.cpp b/src/docpp.cpp index 862bc0a..054f8c1 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -442,6 +442,10 @@ void docpp::CSS::CSSElement::set(const std::pairelement = element; } +void docpp::CSS::CSSElement::push_front(const CSSProperty& property) { + this->element.second.insert(this->element.second.begin(), property); +} + void docpp::CSS::CSSElement::push_back(const CSSProperty& property) { this->element.second.push_back(property); } @@ -527,6 +531,16 @@ int docpp::CSS::CSSStylesheet::find(const CSSElement& element) { return docpp::CSS::CSSStylesheet::npos; } +int docpp::CSS::CSSStylesheet::find(const std::string& str) { + for (int i{0}; i < this->elements.size(); i++) { + if (!this->elements.at(i).get().compare(str) || !this->elements.at(i).getTag().compare(str)) { + return i; + } + } + + return docpp::CSS::CSSStylesheet::npos; +} + int docpp::CSS::CSSStylesheet::size() const { return this->elements.size(); } diff --git a/tests/test.cpp b/tests/test.cpp index 6ced40b..0c1d323 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -154,6 +154,10 @@ SCENARIO("Test HTML", "[HTML]") { css.swap(css.find(element), css.find(element2)); REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}div {color: blue;font-size: 12px;font-family: Arial;}"); + + element.push_front(docpp::CSS::CSSProperty("font-weight", "bold")); + + REQUIRE(element.get() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}"); }; auto test10 = []() {