Add position functions for CSSElement.

This commit is contained in:
Jacob 2024-05-06 11:47:24 +02:00
parent 298a3c96b6
commit 292b6018d6
3 changed files with 114 additions and 1 deletions

View file

@ -496,6 +496,46 @@ namespace docpp {
* @param property The property to push * @param property The property to push
*/ */
void push_back(const CSSProperty& property); 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 * @brief Set the properties of the element
* @param properties The properties to set * @param properties The properties to set

View file

@ -189,6 +189,7 @@ void docpp::HTML::HTMLSection::push_back(const HTMLSection& section) {
void docpp::HTML::HTMLSection::erase(const int index) { void docpp::HTML::HTMLSection::erase(const int index) {
bool erased{false}; bool erased{false};
if (this->elements.find(index) != this->elements.end()) { if (this->elements.find(index) != this->elements.end()) {
this->elements.erase(index); this->elements.erase(index);
erased = true; erased = true;
@ -450,6 +451,58 @@ void docpp::CSS::CSSElement::push_back(const CSSProperty& property) {
this->element.second.push_back(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 docpp::CSS::CSSElement::get(const int formatting) const {
std::string ret{}; std::string ret{};

View file

@ -218,7 +218,27 @@ SCENARIO("Test HTML", "[HTML]") {
REQUIRE(section.get() == "<html><p>Test 1</p><p>Test 3</p><p>Test 4</p><p>Test 5</p></html>"); 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}; 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<void (*)()> tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14};
for (const auto& test : tests) { for (const auto& test : tests) {
test(); test();