Add methods, allowing you to get elements from a section by reference

and make changes to them.
This commit is contained in:
Jacob 2024-06-18 04:04:37 +02:00
parent c29d29d592
commit 9479ff7834
2 changed files with 122 additions and 3 deletions

View file

@ -800,6 +800,18 @@ namespace docpp {
* @return Section The section at the index
*/
Section at_section(const size_type index) const;
/**
* @brief Get the element at an index. To get a section, use at_section()
* @param index The index of the element
* @return Element The element at the index
*/
Element& at(const size_type index);
/**
* @brief Get the section at an index. To get an element, use at()
* @param index The index of the section
* @return Section The section at the index
*/
Section& at_section(const size_type index);
/**
* @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
@ -850,21 +862,41 @@ namespace docpp {
* @return Element The first element of the section
*/
Element front() const;
/**
* @brief Get the first element of the section
* @return Element The first element of the section
*/
Element& front();
/**
* @brief Get the last element of the section
* @return Element The last element of the section
*/
Element back() const;
/**
* @brief Get the last element of the section
* @return Element The last element of the section
*/
Element& back();
/**
* @brief Get the first section of the section
* @return Section The first section of the section
*/
Section front_section() const;
/**
* @brief Get the first section of the section
* @return Section The first section of the section
*/
Section& front_section();
/**
* @brief Get the last section of the section
* @return Section The last section of the section
*/
Section back_section() const;
/**
* @brief Get the last section of the section
* @return Section The last section of the section
*/
Section& back_section();
/**
* @brief Get the size of the section
* @return size_type The size of the section
@ -1093,13 +1125,16 @@ namespace docpp {
}
return T(this->get(formatting, tabc));
}
/**
* @brief Get the section
* @return Section The section
*/
Section get_section() const;
/**
* @brief Get the section
* @return Section The section
*/
Section& get_section();
/**
* @brief Get the doctype of the document
* @return std::string The doctype of the document
@ -1393,6 +1428,12 @@ namespace docpp {
* @return Property The property at the index
*/
Property at(const size_type index) const;
/**
* @brief Get the property at an index
* @param index The index of the property
* @return Property The property at the index
*/
Property& at(const size_type index);
/**
* @brief Find a property in the element
* @param str The property to find
@ -1416,11 +1457,21 @@ namespace docpp {
* @return Property The first property of the element
*/
Property front() const;
/**
* @brief Get the first property of the element
* @return Property The first property of the element
*/
Property& front();
/**
* @brief Get the last property of the element
* @return Property The last property of the element
*/
Property back() const;
/**
* @brief Get the last property of the element
* @return Property The last property of the element
*/
Property& back();
/**
* @brief Get the size of the element
* @return size_type The size of the element

View file

@ -469,7 +469,7 @@ std::unordered_map<docpp::HTML::Tag, std::pair<std::string, docpp::HTML::Type>>
{Tag::Bold, {"b", Type::Non_Self_Closing}},
{Tag::Br, {"br", Type::Non_Closed}},
{Tag::Break, {"br", Type::Non_Closed}},
{Tag::Button, {"button", Type::Non_Closed}},
{Tag::Button, {"button", Type::Non_Self_Closing}},
{Tag::Caption, {"caption", Type::Non_Self_Closing}},
{Tag::Canvas, {"canvas", Type::Non_Self_Closing}},
{Tag::Center, {"center", Type::Non_Self_Closing}},
@ -711,6 +711,14 @@ docpp::HTML::Element docpp::HTML::Section::at(const size_type index) const {
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Element& docpp::HTML::Section::at(const size_type index) {
if (this->elements.find(index) != this->elements.end()) {
return this->elements.at(index);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section docpp::HTML::Section::at_section(const size_type index) const {
if (this->sections.find(index) != this->sections.end()) {
return this->sections.at(index);
@ -719,6 +727,14 @@ docpp::HTML::Section docpp::HTML::Section::at_section(const size_type index) con
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section& docpp::HTML::Section::at_section(const size_type index) {
if (this->sections.find(index) != this->sections.end()) {
return this->sections.at(index);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section::size_type docpp::HTML::Section::find(const Element& element) {
for (size_type i{0}; i < this->size(); i++) {
const Element it = this->get_elements().at(i);
@ -779,6 +795,14 @@ docpp::HTML::Element docpp::HTML::Section::front() const {
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Element& docpp::HTML::Section::front() {
if (this->elements.find(0) != this->elements.end()) {
return this->elements.at(0);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section docpp::HTML::Section::front_section() const {
if (this->sections.find(0) != this->sections.end()) {
return this->sections.at(0);
@ -787,6 +811,14 @@ docpp::HTML::Section docpp::HTML::Section::front_section() const {
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section& docpp::HTML::Section::front_section() {
if (this->sections.find(0) != this->sections.end()) {
return this->sections.at(0);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Element docpp::HTML::Section::back() const {
if (this->elements.find(this->index - 1) != this->elements.end()) {
return this->elements.at(this->index - 1);
@ -795,6 +827,14 @@ docpp::HTML::Element docpp::HTML::Section::back() const {
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Element& docpp::HTML::Section::back() {
if (this->elements.find(this->index - 1) != this->elements.end()) {
return this->elements.at(this->index - 1);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section docpp::HTML::Section::back_section() const {
if (this->sections.find(this->index - 1) != this->sections.end()) {
return this->sections.at(this->index - 1);
@ -803,6 +843,14 @@ docpp::HTML::Section docpp::HTML::Section::back_section() const {
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section& docpp::HTML::Section::back_section() {
if (this->sections.find(this->index - 1) != this->sections.end()) {
return this->sections.at(this->index - 1);
}
throw docpp::out_of_range("Index out of range");
}
docpp::HTML::Section::size_type docpp::HTML::Section::size() const {
return this->index;
}
@ -931,6 +979,10 @@ std::string docpp::HTML::Document::get(const Formatting formatting, const int ta
return this->doctype + (formatting == Formatting::Pretty ? "\n" : formatting == Formatting::Newline ? "\n" : "") + this->document.get(formatting, tabc);
}
docpp::HTML::Section docpp::HTML::Document::get_section() const {
return this->document;
}
docpp::HTML::Section& docpp::HTML::Document::get_section() {
return this->document;
}
@ -1095,6 +1147,14 @@ docpp::CSS::Property docpp::CSS::Element::at(const size_type index) const {
return this->element.second.at(index);
}
docpp::CSS::Property& docpp::CSS::Element::at(const size_type index) {
if (index < 0 || index >= this->element.second.size()) {
throw docpp::out_of_range("Index out of range");
}
return this->element.second.at(index);
}
docpp::CSS::Element::size_type docpp::CSS::Element::find(const Property& property) {
for (size_type i{0}; i < this->element.second.size(); i++) {
if (this->element.second.at(i).get() == property.get()) {
@ -1123,6 +1183,14 @@ docpp::CSS::Property docpp::CSS::Element::back() const {
return this->element.second.back();
}
docpp::CSS::Property& docpp::CSS::Element::front() {
return this->element.second.front();
}
docpp::CSS::Property& docpp::CSS::Element::back() {
return this->element.second.back();
}
docpp::CSS::Element::size_type docpp::CSS::Element::size() const {
return this->element.second.size();
}