Add position functions for HTMLElementProperties.
This commit is contained in:
parent
292b6018d6
commit
cddf11235f
|
@ -110,14 +110,53 @@ namespace docpp {
|
||||||
* @brief Get the properties of the element
|
* @brief Get the properties of the element
|
||||||
* @return std::vector<HTMLProperty> The properties of the element
|
* @return std::vector<HTMLProperty> The properties of the element
|
||||||
*/
|
*/
|
||||||
std::vector<HTMLProperty> get() const;
|
std::vector<HTMLProperty> getProperties() 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
|
||||||
*/
|
*/
|
||||||
void set(const std::vector<HTMLProperty>& properties);
|
void set(const std::vector<HTMLProperty>& properties);
|
||||||
/**
|
/**
|
||||||
* @brief Add a property to the element
|
* @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 HTMLProperty& 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 HTMLProperty& property1, const HTMLProperty& property2);
|
||||||
|
/**
|
||||||
|
* @brief Get the size of the element
|
||||||
|
* @return int The size of the element
|
||||||
|
*/
|
||||||
|
int size() const;
|
||||||
|
/**
|
||||||
|
* @brief Prepend a property to the element
|
||||||
|
* @param property The property to add
|
||||||
|
*/
|
||||||
|
void push_front(const HTMLProperty& property);
|
||||||
|
/**
|
||||||
|
* @brief Append a property to the element
|
||||||
* @param property The property to add
|
* @param property The property to add
|
||||||
*/
|
*/
|
||||||
void push_back(const HTMLProperty& property);
|
void push_back(const HTMLProperty& property);
|
||||||
|
@ -332,12 +371,12 @@ namespace docpp {
|
||||||
* @brief Get the elements of the section
|
* @brief Get the elements of the section
|
||||||
* @return std::vector<HTMLElement> The elements of the section
|
* @return std::vector<HTMLElement> The elements of the section
|
||||||
*/
|
*/
|
||||||
std::vector<HTMLElement> getHTMLElements();
|
std::vector<HTMLElement> getHTMLElements() const;
|
||||||
/**
|
/**
|
||||||
* @brief Get the sections of the section
|
* @brief Get the sections of the section
|
||||||
* @return std::vector<HTMLSection> The sections of the section
|
* @return std::vector<HTMLSection> The sections of the section
|
||||||
*/
|
*/
|
||||||
std::vector<HTMLSection> getHTMLSections();
|
std::vector<HTMLSection> getHTMLSections() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Dump the entire section.
|
* @brief Dump the entire section.
|
||||||
|
|
|
@ -59,7 +59,7 @@ docpp::HTML::HTMLElementProperties::HTMLElementProperties(const docpp::HTML::HTM
|
||||||
this->push_back(property);
|
this->push_back(property);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<docpp::HTML::HTMLProperty> docpp::HTML::HTMLElementProperties::get() const {
|
std::vector<docpp::HTML::HTMLProperty> docpp::HTML::HTMLElementProperties::getProperties() const {
|
||||||
return this->properties;
|
return this->properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,10 +67,68 @@ void docpp::HTML::HTMLElementProperties::set(const std::vector<docpp::HTML::HTML
|
||||||
this->properties = properties;
|
this->properties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void docpp::HTML::HTMLElementProperties::erase(const int index) {
|
||||||
|
if (index < 0 || index >= this->properties.size()) {
|
||||||
|
throw std::out_of_range("Index out of range");
|
||||||
|
}
|
||||||
|
|
||||||
|
this->properties.erase(this->properties.begin() + index);
|
||||||
|
}
|
||||||
|
|
||||||
|
void docpp::HTML::HTMLElementProperties::push_front(const docpp::HTML::HTMLProperty& property) {
|
||||||
|
this->properties.insert(this->properties.begin(), property);
|
||||||
|
}
|
||||||
|
|
||||||
void docpp::HTML::HTMLElementProperties::push_back(const docpp::HTML::HTMLProperty& property) {
|
void docpp::HTML::HTMLElementProperties::push_back(const docpp::HTML::HTMLProperty& property) {
|
||||||
this->properties.push_back(property);
|
this->properties.push_back(property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int docpp::HTML::HTMLElementProperties::find(const docpp::HTML::HTMLProperty& property) {
|
||||||
|
for (int i{0}; i < this->properties.size(); i++) {
|
||||||
|
if (!this->properties.at(i).getKey().compare(property.getKey())) {
|
||||||
|
return i;
|
||||||
|
} else if (!this->properties.at(i).getValue().compare(property.getValue())) {
|
||||||
|
return i;
|
||||||
|
} else if (this->properties.at(i).getValue().find(property.getValue()) != std::string::npos) {
|
||||||
|
return i;
|
||||||
|
} else if (this->properties.at(i).getKey().find(property.getKey()) != std::string::npos) {
|
||||||
|
return i;
|
||||||
|
} else if (this->properties.at(i).get() == property.get()) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return docpp::HTML::HTMLElementProperties::npos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int docpp::HTML::HTMLElementProperties::find(const std::string& str) {
|
||||||
|
for (int i{0}; i < this->properties.size(); i++) {
|
||||||
|
if (!this->properties.at(i).getKey().compare(str) || !this->properties.at(i).getValue().compare(str)) {
|
||||||
|
return i;
|
||||||
|
} else if (this->properties.at(i).getKey().find(str) != std::string::npos || this->properties.at(i).getValue().find(str) != std::string::npos) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return docpp::HTML::HTMLElementProperties::npos;
|
||||||
|
}
|
||||||
|
|
||||||
|
int docpp::HTML::HTMLElementProperties::size() const {
|
||||||
|
return this->properties.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void docpp::HTML::HTMLElementProperties::swap(const int index1, const int index2) {
|
||||||
|
if (index1 < 0 || index1 >= this->properties.size() || index2 < 0 || index2 >= this->properties.size()) {
|
||||||
|
throw std::out_of_range("Index out of range");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::swap(this->properties[index1], this->properties[index2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void docpp::HTML::HTMLElementProperties::swap(const docpp::HTML::HTMLProperty& property1, const docpp::HTML::HTMLProperty& property2) {
|
||||||
|
this->swap(this->find(property1), this->find(property2));
|
||||||
|
}
|
||||||
|
|
||||||
docpp::HTML::HTMLElement::HTMLElement(const std::string& tag, const HTMLElementProperties& properties, const std::string& data, const int type) {
|
docpp::HTML::HTMLElement::HTMLElement(const std::string& tag, const HTMLElementProperties& properties, const std::string& data, const int type) {
|
||||||
this->set(tag, properties, data, type);
|
this->set(tag, properties, data, type);
|
||||||
}
|
}
|
||||||
|
@ -87,7 +145,7 @@ std::string docpp::HTML::HTMLElement::get(const int formatting) const {
|
||||||
|
|
||||||
ret += "<" + this->tag;
|
ret += "<" + this->tag;
|
||||||
|
|
||||||
for (const auto& it : this->properties.get()) {
|
for (const auto& it : this->properties.getProperties()) {
|
||||||
if (!it.getKey().compare("")) continue;
|
if (!it.getKey().compare("")) continue;
|
||||||
if (!it.getValue().compare("")) continue;
|
if (!it.getValue().compare("")) continue;
|
||||||
|
|
||||||
|
@ -292,7 +350,7 @@ int docpp::HTML::HTMLSection::size() const {
|
||||||
return this->index;
|
return this->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<docpp::HTML::HTMLElement> docpp::HTML::HTMLSection::getHTMLElements() {
|
std::vector<docpp::HTML::HTMLElement> docpp::HTML::HTMLSection::getHTMLElements() const {
|
||||||
std::vector<docpp::HTML::HTMLElement> ret{};
|
std::vector<docpp::HTML::HTMLElement> ret{};
|
||||||
ret.reserve(this->index);
|
ret.reserve(this->index);
|
||||||
for (int i{0}; i < this->index; i++) {
|
for (int i{0}; i < this->index; i++) {
|
||||||
|
@ -303,7 +361,7 @@ std::vector<docpp::HTML::HTMLElement> docpp::HTML::HTMLSection::getHTMLElements(
|
||||||
return std::move(ret);
|
return std::move(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<docpp::HTML::HTMLSection> docpp::HTML::HTMLSection::getHTMLSections() {
|
std::vector<docpp::HTML::HTMLSection> docpp::HTML::HTMLSection::getHTMLSections() const {
|
||||||
std::vector<docpp::HTML::HTMLSection> ret{};
|
std::vector<docpp::HTML::HTMLSection> ret{};
|
||||||
ret.reserve(this->index);
|
ret.reserve(this->index);
|
||||||
|
|
||||||
|
@ -321,7 +379,7 @@ std::string docpp::HTML::HTMLSection::get(const int formatting) const {
|
||||||
|
|
||||||
ret += "<" + this->tag;
|
ret += "<" + this->tag;
|
||||||
|
|
||||||
for (const auto& it : this->properties.get()) {
|
for (const auto& it : this->properties.getProperties()) {
|
||||||
if (!it.getKey().compare("")) continue;
|
if (!it.getKey().compare("")) continue;
|
||||||
if (!it.getValue().compare("")) continue;
|
if (!it.getValue().compare("")) continue;
|
||||||
|
|
||||||
|
|
|
@ -238,7 +238,32 @@ SCENARIO("Test HTML", "[HTML]") {
|
||||||
REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}");
|
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};
|
auto test15 = []() {
|
||||||
|
docpp::HTML::HTMLElementProperties prop{};
|
||||||
|
|
||||||
|
prop.push_back(docpp::HTML::HTMLProperty(std::pair<std::string, std::string>("id", "test_id")));
|
||||||
|
prop.push_back(docpp::HTML::HTMLProperty(std::pair<std::string, std::string>("class", "class1 class2 class3")));
|
||||||
|
|
||||||
|
REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "<p id=\"test_id\" class=\"class1 class2 class3\"></p>");
|
||||||
|
|
||||||
|
const int pos = prop.find("class");
|
||||||
|
|
||||||
|
REQUIRE(pos != docpp::HTML::HTMLElementProperties::npos);
|
||||||
|
|
||||||
|
const int pos2 = prop.find("class2");
|
||||||
|
|
||||||
|
REQUIRE(pos2 != docpp::HTML::HTMLElementProperties::npos);
|
||||||
|
|
||||||
|
const int pos3 = prop.find("class4");
|
||||||
|
|
||||||
|
REQUIRE(pos3 == docpp::HTML::HTMLElementProperties::npos);
|
||||||
|
|
||||||
|
prop.erase(pos);
|
||||||
|
|
||||||
|
REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "<p id=\"test_id\"></p>");
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<void (*)()> tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15};
|
||||||
|
|
||||||
for (const auto& test : tests) {
|
for (const auto& test : tests) {
|
||||||
test();
|
test();
|
||||||
|
|
Loading…
Reference in a new issue