diff --git a/include/docpp.hpp b/include/docpp.hpp index ad7ef5b..9654190 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -161,6 +161,16 @@ namespace docpp { * @param property2 The second property */ 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 * @return int The size of the element @@ -347,6 +357,26 @@ namespace docpp { * @param section The section to insert */ 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 * @return int The size of the section @@ -627,6 +657,16 @@ namespace docpp { * @param property2 The second property */ 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 * @return int The size of the element @@ -728,6 +768,16 @@ namespace docpp { * @return int The size of the stylesheet */ 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 * @param index1 The index of the first element diff --git a/src/docpp.cpp b/src/docpp.cpp index 583cde0..d7fbed7 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -148,6 +148,14 @@ int docpp::HTML::HTMLElementProperties::find(const std::string& str) { 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 { return this->properties.size(); } @@ -432,6 +440,38 @@ int docpp::HTML::HTMLSection::find(const std::string& str) { 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 { return this->index; } @@ -700,6 +740,14 @@ int docpp::CSS::CSSElement::find(const std::string& str) { 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 { return this->element.second.size(); } @@ -838,6 +886,14 @@ int docpp::CSS::CSSStylesheet::size() const { 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) { if (index1 < 0 || index1 >= this->elements.size() || index2 < 0 || index2 >= this->elements.size()) { throw std::out_of_range("Index out of range"); diff --git a/tests/test.cpp b/tests/test.cpp index 6e32ffb..31e2707 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -306,6 +306,17 @@ SCENARIO("Test HTML", "[HTML]") { REQUIRE(section.get() == "

Test 1

Test 2

Test 3

"); }; + 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() == "

Test 1

"); + REQUIRE(section.back().get() == "

Test 3

"); + }; + std::vector tests{ test1, test2, @@ -325,6 +336,7 @@ SCENARIO("Test HTML", "[HTML]") { test16, test17, test18, + test19, }; for (const auto& test : tests) {