Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix data model aliases inside select #510

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Source/Core/DataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ bool DataModel::EraseAliases(Element* element)
return aliases.erase(element) == 1;
}

void DataModel::CopyAliases(Element* from_element, Element* to_element)
{
if (from_element == to_element)
return;
auto existing_map = aliases.find(from_element);

if (existing_map != aliases.end())
{
// Need to create a copy to prevent errors during concurrent modification for 3rd party containers
auto copy = existing_map->second;
for (auto const& it : copy)
aliases[to_element][it.first] = std::move(it.second);
}
}

DataAddress DataModel::ResolveAddress(const String& address_str, Element* element) const
{
DataAddress address = ParseAddress(address_str);
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DataModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class DataModel : NonCopyMoveable {

bool InsertAlias(Element* element, const String& alias_name, DataAddress replace_with_address);
bool EraseAliases(Element* element);
void CopyAliases(Element* source_element, Element* target_element);

DataAddress ResolveAddress(const String& address_str, Element* element) const;
const DataEventFunc* GetEventCallback(const String& name);
Expand Down
9 changes: 9 additions & 0 deletions Source/Core/Elements/WidgetDropDown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "../../../Include/RmlUi/Core/Math.h"
#include "../../../Include/RmlUi/Core/Profiling.h"
#include "../../../Include/RmlUi/Core/Property.h"
#include "../DataModel.h"

namespace Rml {

Expand Down Expand Up @@ -129,9 +130,17 @@ void WidgetDropDown::OnUpdate()
const int selection = GetSelection();

if (Element* option = selection_element->GetChild(selection))
{
option->GetInnerRML(value_rml);
if (auto model = value_element->GetDataModel())
model->CopyAliases(option, value_element);
}
else
{
if (auto model = value_element->GetDataModel())
model->EraseAliases(value_element);
value_rml = parent_element->GetValue();
}

value_element->SetInnerRML(value_rml);

Expand Down
8 changes: 8 additions & 0 deletions Tests/Source/UnitTests/ElementFormControlSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ TEST_CASE("form.select.databinding")
</select>
)",
"2", "C"},

{
R"(
<select data-value="selected_index">
<option data-for="s : subjects" data-attr-value="it_index"><p data-rml="s | to_upper"></p></option>
</select>
)",
"2", "<p data-rml=\"s | to_upper\">C</p>"},
};

DataModelConstructor constructor = context->CreateDataModel("select-test");
Expand Down