Skip to content

Commit

Permalink
Fix usage of data variables in selected options (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakror authored Sep 17, 2023
1 parent cda1db7 commit a9bb4ac
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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

0 comments on commit a9bb4ac

Please sign in to comment.