diff --git a/include/docpp.hpp b/include/docpp.hpp index 83711f3..c56f3c5 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -116,6 +116,18 @@ namespace docpp { * @param properties The properties to set */ void set(const std::vector& properties); + /** + * @brief Get the property at an index + * @param index The index of the property + * @return HTMLProperty The property at the index + */ + HTMLProperty at(const int index) const; + /** + * @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 HTMLProperty& property); /** * @brief Erase a property from the element * @param index The index of the property to erase @@ -268,6 +280,18 @@ namespace docpp { * @param section The section to add */ void push_back(const HTMLSection& section); + /** + * @brief Get the element at an index. To get a section, use at_section() + * @param index The index of the element + * @return HTMLElement The element at the index + */ + HTMLElement at(const int index) const; + /** + * @brief Get the section at an index. To get an element, use at() + * @param index The index of the section + * @return HTMLSection The section at the index + */ + HTMLSection at_section(const int index) const; /** * @brief Erase an element from the section. Note that this will NOT change the size/index. * @param index The index of the element to erase @@ -552,6 +576,12 @@ namespace docpp { * @return int The index of the property */ int find(const CSSProperty& property); + /** + * @brief Get the property at an index + * @param index The index of the property + * @return CSSProperty The property at the index + */ + CSSProperty at(const int index) const; /** * @brief Find a property in the element * @param str The property to find @@ -655,6 +685,12 @@ namespace docpp { * @return int The index of the element */ int find(const std::string& str); + /** + * @brief Get the element at an index + * @param index The index of the element + * @return CSSElement The element at the index + */ + CSSElement at(const int index) const; /** * @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 8caf2f2..1128d1b 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -63,10 +63,26 @@ std::vector docpp::HTML::HTMLElementProperties::getPr return this->properties; } +docpp::HTML::HTMLProperty docpp::HTML::HTMLElementProperties::at(const int index) const { + if (index < 0 || index >= this->properties.size()) { + throw std::out_of_range("Index out of range"); + } + + return this->properties.at(index); +} + void docpp::HTML::HTMLElementProperties::set(const std::vector& properties) { this->properties = properties; } +void docpp::HTML::HTMLElementProperties::insert(const int index, const docpp::HTML::HTMLProperty& property) { + if (index < 0 || index >= this->properties.size()) { + throw std::out_of_range("Index out of range"); + } + + this->properties.insert(this->properties.begin() + index, property); +} + void docpp::HTML::HTMLElementProperties::erase(const int index) { if (index < 0 || index >= this->properties.size()) { throw std::out_of_range("Index out of range"); @@ -302,6 +318,22 @@ void docpp::HTML::HTMLSection::insert(const int index, const HTMLSection& sectio this->index = std::max(this->index, index) + 1; } +docpp::HTML::HTMLElement docpp::HTML::HTMLSection::at(const int index) const { + if (this->elements.find(index) != this->elements.end()) { + return this->elements.at(index); + } + + throw std::out_of_range("Index out of range"); +} + +docpp::HTML::HTMLSection docpp::HTML::HTMLSection::at_section(const int index) const { + if (this->sections.find(index) != this->sections.end()) { + return this->sections.at(index); + } + + throw std::out_of_range("Index out of range"); +} + int docpp::HTML::HTMLSection::find(const HTMLElement& element) { for (int i{0}; i < this->size(); i++) { const auto it = this->getHTMLElements().at(i); @@ -525,6 +557,14 @@ void docpp::CSS::CSSElement::erase(const int index) { this->element.second.erase(this->element.second.begin() + index); } +docpp::CSS::CSSProperty docpp::CSS::CSSElement::at(const int index) const { + if (index < 0 || index >= this->element.second.size()) { + throw std::out_of_range("Index out of range"); + } + + return this->element.second.at(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()) { @@ -632,6 +672,14 @@ void docpp::CSS::CSSStylesheet::erase(const int index) { this->elements.erase(this->elements.begin() + index); } +docpp::CSS::CSSElement docpp::CSS::CSSStylesheet::at(const int index) const { + if (index < 0 || index >= this->elements.size()) { + throw std::out_of_range("Index out of range"); + } + + return this->elements.at(index); +} + int docpp::CSS::CSSStylesheet::find(const CSSElement& element) { for (int i{0}; i < this->elements.size(); i++) { if (this->elements.at(i).get() == element.get()) { diff --git a/tests/test.cpp b/tests/test.cpp index e513da4..b61bce6 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -252,6 +252,9 @@ SCENARIO("Test HTML", "[HTML]") { const int pos2 = prop.find("class2"); + REQUIRE(prop.at(pos2).getKey() == "class"); + REQUIRE(prop.at(pos2).getValue() == "class1 class2 class3"); + REQUIRE(pos2 != docpp::HTML::HTMLElementProperties::npos); const int pos3 = prop.find("class4");