hemplateSimple XML template engine |
git clone git://git.dimitrijedobrota.com/hemplate.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
commit | a2af38695e4b6be946b33ba20d09ab0d1cb80834 |
parent | 14c196fa229e9b4baf62db69d4730d64367b769b |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Thu, 24 Apr 2025 08:47:38 +0200 |
Rework attribute_list operations
M | example/html.cpp | | | ++ -- |
M | include/hemplate/attribute.hpp | | | +++ ----- |
M | include/hemplate/element.hpp | | | +++++++++++++ -- |
M | include/hemplate/rss.hpp | | | ++ - |
M | source/attribute.cpp | | | +++++++++++++++++ ------------------ |
M | test/source/attribute_list.cpp | | | +++ ------ |
6 files changed, 40 insertions(+), 34 deletions(-)
diff --git a/ example/html.cpp b/ example/html.cpp
@@ -20,11 +20,11 @@
int main()
html::ul {
ul_attrs,
html::li {
li_attrs.add({"class", "item1"}),
{li_attrs, {{"class", "item1"}}},
"Item 1",
},
html::li {
li_attrs.add({"class", "item2"}),
{li_attrs, {{"class", "item2"}}},
"Item 2",
},
},
diff --git a/ include/hemplate/attribute.hpp b/ include/hemplate/attribute.hpp
@@ -51,12 +51,10 @@
public:
attribute_list(std::initializer_list<attribute> list);
attribute_list(attribute attr); // NOLINT *-explicit-constructor
attribute_list(attribute_list attrs, std::initializer_list<attribute> list);
attribute_list& set(const attribute_list& list);
attribute_list& set(attribute attr);
attribute_list add(const attribute_list& list) const;
attribute_list add(attribute attr) const;
void set(const attribute_list& list);
void set(attribute attr);
explicit operator std::string() const
{
diff --git a/ include/hemplate/element.hpp b/ include/hemplate/element.hpp
@@ -140,15 +140,26 @@
public:
template<based::string_literal Tag, element::Type MyType>
class HEMPLATE_EXPORT element_builder : public element
{
static bool m_state; // NOLINT
static bool m_state;
public:
// NOLINTBEGIN *-no-array-decay
template<typename... Args>
explicit element_builder(Args&&... args)
// NOLINTNEXTLINE *-no-array-decay
: element(m_state, MyType, Tag.data(), std::forward<Args>(args)...)
{
}
template<typename... Args>
explicit element_builder(attribute_list list, Args&&... args)
: element(m_state,
MyType,
Tag.data(),
std::move(list),
std::forward<Args>(args)...)
{
}
// NOLINTEND *-no-array-decay
};
template<based::string_literal Tag, element::Type Type>
diff --git a/ include/hemplate/rss.hpp b/ include/hemplate/rss.hpp
@@ -56,10 +56,11 @@
class HEMPLATE_EXPORT atomLink // NOLINT *-identifier-naming
std::string_view rel,
std::string_view type)
{
return list.set({
list.set({
{"rel", rel},
{"type", type},
});
return list;
}
public:
diff --git a/ source/attribute.cpp b/ source/attribute.cpp
@@ -26,7 +26,16 @@
attribute_list::attribute_list(attribute attr)
set(std::move(attr));
}
attribute_list& attribute_list::set(const attribute_list& list)
attribute_list::attribute_list(attribute_list attrs,
std::initializer_list<attribute> list)
: attribute_list(std::move(attrs))
{
for (const auto& attr : list) {
set(attr);
}
}
void attribute_list::set(const attribute_list& list)
{
for (const auto& attr : list.m_attributes) {
set(attr);
@@ -34,31 +43,21 @@
attribute_list& attribute_list::set(const attribute_list& list)
set(list.m_class);
set(list.m_style);
return (*this);
}
attribute_list& attribute_list::set(attribute attr)
void attribute_list::set(attribute attr)
{
if (attr.name() == "class") {
m_class.append(" ", attr.value());
} else if (attr.name() == "style") {
m_style.append("; ", attr.value());
} else {
m_attributes.emplace_back(std::move(attr));
return;
}
return *this;
}
attribute_list attribute_list::add(const attribute_list& list) const
{
return attribute_list(*this).set(list);
}
if (attr.name() == "style") {
m_style.append("; ", attr.value());
return;
}
attribute_list attribute_list::add(attribute attr) const
{
return attribute_list(*this).set(std::move(attr));
m_attributes.emplace_back(std::move(attr));
}
} // namespace hemplate
diff --git a/ test/source/attribute_list.cpp b/ test/source/attribute_list.cpp
@@ -79,12 +79,9 @@
TEST_CASE("add", "[attribute_list]")
{
using namespace std::literals::string_view_literals;
const auto attrs = hemplate::attribute_list {}
.add({"class", "first"})
.add({
{"class"sv, "second"sv},
{"class"sv, "third"sv},
});
const auto tmp = hemplate::attribute_list {{"class", "first"}};
const auto attrs = hemplate::attribute_list {
tmp, {{"class"sv, "second"sv}, {"class"sv, "third"sv}}};
REQUIRE(std::string(attrs) == R"(class="first second third")");
}