From 4b7cfe67f77783e95206ae30a940b8029d713805 Mon Sep 17 00:00:00 2001 From: saddamr3e Date: Tue, 16 Jun 2026 23:16:38 +0530 Subject: [PATCH] Fix WMenu selecting item on partial path component matches (non-segment boundaries) --- src/Wt/WMenu.C | 22 +++++++++++----------- test/widgets/WMenuTest.C | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Wt/WMenu.C b/src/Wt/WMenu.C index ac3c576aa..84aaffb77 100644 --- a/src/Wt/WMenu.C +++ b/src/Wt/WMenu.C @@ -29,21 +29,21 @@ namespace { * ("a", "a") -> 1 */ int match(const std::string& path, const std::string& component) { - if (component.length() > path.length()) - return -1; - - int length = std::min(component.length(), path.length()); + if (component.empty()) { + return 0; + } - int current = -1; + if (component.length() > path.length()) { + return -1; + } - for (int i = 0; i < length; ++i) { - if (component[i] != path[i]) { - return current; - } else if (component[i] == '/') - current = i; + if (path.compare(0, component.length(), component) == 0) { + if (path.length() == component.length() || path[component.length()] == '/') { + return static_cast(component.length()); + } } - return length; + return -1; } } diff --git a/test/widgets/WMenuTest.C b/test/widgets/WMenuTest.C index 84c1b947c..01cffd956 100644 --- a/test/widgets/WMenuTest.C +++ b/test/widgets/WMenuTest.C @@ -39,3 +39,22 @@ BOOST_AUTO_TEST_CASE(WMenu_addItem_change_index_for_internal_path_match) BOOST_TEST(menu->currentIndex() == previousIndex); } +BOOST_AUTO_TEST_CASE(WMenu_internal_path_matching_segment_boundary) +{ + Wt::Test::WTestEnvironment testEnv; + testEnv.setInternalPath("/contact-us"); + Wt::WApplication app(testEnv); + + Wt::WStackedWidget *stack = app.root()->addNew(); + Wt::WMenu *menu = app.root()->addNew(stack); + menu->setInternalPathEnabled("/"); + + menu->addItem("contact", std::make_unique("Contact Page")); + + BOOST_TEST(menu->currentIndex() == -1); + + app.setInternalPath("/contact/us"); + + BOOST_TEST(menu->currentIndex() == 0); +} +