Implement push_front() method for CSSElement, implement find() method

that takes an std::string for CSSStylesheet.
This commit is contained in:
Jacob 2024-05-06 11:06:45 +02:00
parent f9bf0bbfd4
commit 298a3c96b6
3 changed files with 30 additions and 1 deletions

View file

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

View file

@ -442,6 +442,10 @@ void docpp::CSS::CSSElement::set(const std::pair<std::string, std::vector<CSSPro
this->element = 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();
}

View file

@ -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 = []() {