Skip to content

Commit

Permalink
Merge pull request #105 from NuiCpp/devel
Browse files Browse the repository at this point in the history
Fixed const correctness in static range.
  • Loading branch information
5cript authored Jan 23, 2024
2 parents 9e7e3f0 + bf1ae0f commit 4bca60e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
16 changes: 10 additions & 6 deletions nui/include/nui/frontend/elements/impl/html_element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,22 @@ namespace Nui
requires(!InvocableReturns<GeneratorT, std::string>)
auto operator()(GeneratorT&& elementRenderer) &&
{
return [self = this->clone(),
elementRenderer = std::forward<GeneratorT>(elementRenderer)](auto& parentElement, Renderer const&) {
return elementRenderer()(parentElement, Renderer{.type = RendererType::Append});
return [self = this->clone(), elementRenderer = std::forward<GeneratorT>(elementRenderer)](
auto& parentElement, Renderer const& gen) {
auto materialized = renderElement(gen, parentElement, self);
elementRenderer()(*materialized, Renderer{.type = RendererType::Append});
return materialized;
};
}
template <typename T, std::invocable<T&, Renderer const&> GeneratorT>
requires InvocableReturns<GeneratorT, std::string>
auto operator()(GeneratorT&& elementRenderer) &&
{
return [self = this->clone(),
elementRenderer = std::forward<GeneratorT>(elementRenderer)](auto& parentElement, Renderer const&) {
return elementRenderer(parentElement, Renderer{.type = RendererType::Append});
return [self = this->clone(), elementRenderer = std::forward<GeneratorT>(elementRenderer)](
auto& parentElement, Renderer const& gen) {
auto materialized = renderElement(gen, parentElement, self);
materialized->setTextContent(elementRenderer(*materialized, Renderer{.type = RendererType::Append}));
return materialized;
};
}

Expand Down
6 changes: 3 additions & 3 deletions nui/include/nui/frontend/event_system/range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ namespace Nui
}

template <typename ContainerT>
UnoptimizedRange<IteratorAccessor<ContainerT>> range(ContainerT const& container)
UnoptimizedRange<IteratorAccessor<ContainerT const>> range(ContainerT const& container)
{
return UnoptimizedRange<IteratorAccessor<ContainerT>>{
IteratorAccessor<ContainerT>{container},
return UnoptimizedRange<IteratorAccessor<ContainerT const>>{
IteratorAccessor<ContainerT const>{container},
};
}

Expand Down
8 changes: 8 additions & 0 deletions nui/include/nui/utility/iterator_accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ namespace Nui
private:
ContainerT* container_;
};

// Deduction guide for const ContainerT
template <typename ContainerT>
IteratorAccessor(ContainerT const&) -> IteratorAccessor<const ContainerT>;

// Deduction guide for non-const ContainerT
template <typename ContainerT>
IteratorAccessor(ContainerT&) -> IteratorAccessor<ContainerT>;
}
54 changes: 53 additions & 1 deletion nui/test/nui/test_render.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ namespace Nui::Tests
else
{
const auto s = stabilize(
stable,
stable,
span{id = once}(button{class_ = onceClass}())
);
once = "X";
Expand Down Expand Up @@ -733,4 +733,56 @@ namespace Nui::Tests
EXPECT_EQ(
Nui::val::global("document")["body"]["children"][0]["children"][0]["attributes"]["cx"].as<long long>(), 10);
}

TEST_F(TestRender, CanUseUnconditionalRendererFunction)
{
using namespace Nui::Elements;
using namespace Nui::Attributes;
using div = Nui::Elements::div;

render(body{id = "body"}([]() -> Nui::ElementRenderer {
return div{
id = "inner",
}();
}));

EXPECT_EQ(Nui::val::global("document")["body"]["children"]["length"].as<long long>(), 1);
EXPECT_EQ(Nui::val::global("document")["body"]["children"][0]["attributes"]["id"].as<std::string>(), "inner");
}

TEST_F(TestRender, AttributesArePresentOnRangeChildWithRendererFunctionChildren)
{
using namespace Nui::Elements;
using namespace Nui::Attributes;
using div = Nui::Elements::div;
using span = Nui::Elements::span;

Nui::Observed<std::vector<std::string>> range{std::vector<std::string>{"A", "B", "C", "D"}};

render(body{
class_ = "range-parent",
}(range.map([](long long, auto const& element) {
return div{
class_ = "range-child",
id = element,
}([]() -> Nui::ElementRenderer {
return span{
class_ = "range-child-child",
}();
});
})));

auto children = Nui::val::global("document")["body"]["children"].as<Nui::Tests::Engine::Array>();
ASSERT_EQ(children.size(), range.value().size());

for (size_t i = 0; i < children.size(); ++i)
{
auto& child = children[i];
std::string expectedId = range.value()[i];
std::string expectedClass = "range-child";

EXPECT_EQ(child["attributes"]["id"].as<std::string>(), expectedId);
EXPECT_EQ(child["attributes"]["class"].as<std::string>(), expectedClass);
}
}
}

0 comments on commit 4bca60e

Please sign in to comment.