Add front() and back() methods for the various classes.

This commit is contained in:
Jacob 2024-05-07 22:58:53 +02:00
parent 43daa77f6d
commit 17d8bf6b4b
3 changed files with 118 additions and 0 deletions

View file

@ -161,6 +161,16 @@ namespace docpp {
* @param property2 The second property * @param property2 The second property
*/ */
void swap(const HTMLProperty& property1, const HTMLProperty& property2); void swap(const HTMLProperty& property1, const HTMLProperty& property2);
/**
* @brief Get the first property of the element
* @return HTMLProperty The first property of the element
*/
HTMLProperty front() const;
/**
* @brief Get the last property of the element
* @return HTMLProperty The last property of the element
*/
HTMLProperty back() const;
/** /**
* @brief Get the size of the element * @brief Get the size of the element
* @return int The size of the element * @return int The size of the element
@ -347,6 +357,26 @@ namespace docpp {
* @param section The section to insert * @param section The section to insert
*/ */
void insert(const int index, const HTMLSection& section); void insert(const int index, const HTMLSection& section);
/**
* @brief Get the first element of the section
* @return HTMLElement The first element of the section
*/
HTMLElement front() const;
/**
* @brief Get the last element of the section
* @return HTMLElement The last element of the section
*/
HTMLElement back() const;
/**
* @brief Get the first section of the section
* @return HTMLSection The first section of the section
*/
HTMLSection front_section() const;
/**
* @brief Get the last section of the section
* @return HTMLSection The last section of the section
*/
HTMLSection back_section() const;
/** /**
* @brief Get the size of the section * @brief Get the size of the section
* @return int The size of the section * @return int The size of the section
@ -627,6 +657,16 @@ namespace docpp {
* @param property2 The second property * @param property2 The second property
*/ */
void swap(const CSSProperty& property1, const CSSProperty& property2); void swap(const CSSProperty& property1, const CSSProperty& property2);
/**
* @brief Get the first property of the element
* @return CSSProperty The first property of the element
*/
CSSProperty front() const;
/**
* @brief Get the last property of the element
* @return CSSProperty The last property of the element
*/
CSSProperty back() const;
/** /**
* @brief Get the size of the element * @brief Get the size of the element
* @return int The size of the element * @return int The size of the element
@ -728,6 +768,16 @@ namespace docpp {
* @return int The size of the stylesheet * @return int The size of the stylesheet
*/ */
int size() const; int size() const;
/**
* @brief Get the first element of the stylesheet
* @return CSSElement The first element of the stylesheet
*/
CSSElement front() const;
/**
* @brief Get the last element of the stylesheet
* @return CSSElement The last element of the stylesheet
*/
CSSElement back() const;
/** /**
* @brief Swap two elements in the stylesheet * @brief Swap two elements in the stylesheet
* @param index1 The index of the first element * @param index1 The index of the first element

View file

@ -148,6 +148,14 @@ int docpp::HTML::HTMLElementProperties::find(const std::string& str) {
return docpp::HTML::HTMLElementProperties::npos; return docpp::HTML::HTMLElementProperties::npos;
} }
docpp::HTML::HTMLProperty docpp::HTML::HTMLElementProperties::front() const {
return this->properties.front();
}
docpp::HTML::HTMLProperty docpp::HTML::HTMLElementProperties::back() const {
return this->properties.back();
}
int docpp::HTML::HTMLElementProperties::size() const { int docpp::HTML::HTMLElementProperties::size() const {
return this->properties.size(); return this->properties.size();
} }
@ -432,6 +440,38 @@ int docpp::HTML::HTMLSection::find(const std::string& str) {
return docpp::HTML::HTMLSection::npos; return docpp::HTML::HTMLSection::npos;
} }
docpp::HTML::HTMLElement docpp::HTML::HTMLSection::front() const {
if (this->elements.find(0) != this->elements.end()) {
return this->elements.at(0);
}
throw std::out_of_range("Index out of range");
}
docpp::HTML::HTMLSection docpp::HTML::HTMLSection::front_section() const {
if (this->sections.find(0) != this->sections.end()) {
return this->sections.at(0);
}
throw std::out_of_range("Index out of range");
}
docpp::HTML::HTMLElement docpp::HTML::HTMLSection::back() const {
if (this->elements.find(this->index - 1) != this->elements.end()) {
return this->elements.at(this->index - 1);
}
throw std::out_of_range("Index out of range");
}
docpp::HTML::HTMLSection docpp::HTML::HTMLSection::back_section() const {
if (this->sections.find(this->index - 1) != this->sections.end()) {
return this->sections.at(this->index - 1);
}
throw std::out_of_range("Index out of range");
}
int docpp::HTML::HTMLSection::size() const { int docpp::HTML::HTMLSection::size() const {
return this->index; return this->index;
} }
@ -700,6 +740,14 @@ int docpp::CSS::CSSElement::find(const std::string& str) {
return docpp::CSS::CSSElement::npos; return docpp::CSS::CSSElement::npos;
} }
docpp::CSS::CSSProperty docpp::CSS::CSSElement::front() const {
return this->element.second.front();
}
docpp::CSS::CSSProperty docpp::CSS::CSSElement::back() const {
return this->element.second.back();
}
int docpp::CSS::CSSElement::size() const { int docpp::CSS::CSSElement::size() const {
return this->element.second.size(); return this->element.second.size();
} }
@ -838,6 +886,14 @@ int docpp::CSS::CSSStylesheet::size() const {
return this->elements.size(); return this->elements.size();
} }
docpp::CSS::CSSElement docpp::CSS::CSSStylesheet::front() const {
return this->elements.front();
}
docpp::CSS::CSSElement docpp::CSS::CSSStylesheet::back() const {
return this->elements.back();
}
void docpp::CSS::CSSStylesheet::swap(const int index1, const int index2) { void docpp::CSS::CSSStylesheet::swap(const int index1, const int index2) {
if (index1 < 0 || index1 >= this->elements.size() || index2 < 0 || index2 >= this->elements.size()) { if (index1 < 0 || index1 >= this->elements.size() || index2 < 0 || index2 >= this->elements.size()) {
throw std::out_of_range("Index out of range"); throw std::out_of_range("Index out of range");

View file

@ -306,6 +306,17 @@ SCENARIO("Test HTML", "[HTML]") {
REQUIRE(section.get() == "<p>Test 1</p><p>Test 2</p><p>Test 3</p>"); REQUIRE(section.get() == "<p>Test 1</p><p>Test 2</p><p>Test 3</p>");
}; };
auto test19 = []() {
docpp::HTML::HTMLSection section = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {});
section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 1"));
section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 2"));
section.push_back(docpp::HTML::HTMLElement("p", {}, "Test 3"));
REQUIRE(section.front().get() == "<p>Test 1</p>");
REQUIRE(section.back().get() == "<p>Test 3</p>");
};
std::vector<void (*)()> tests{ std::vector<void (*)()> tests{
test1, test1,
test2, test2,
@ -325,6 +336,7 @@ SCENARIO("Test HTML", "[HTML]") {
test16, test16,
test17, test17,
test18, test18,
test19,
}; };
for (const auto& test : tests) { for (const auto& test : tests) {