From 4966f4155dcfff93fb43cff791a4fa4e01f1943d Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Thu, 10 Nov 2022 17:23:10 +0400 Subject: [PATCH] Fixed handling of backslashes in suggestions --- src/tools/otherTools.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/tools/otherTools.cpp b/src/tools/otherTools.cpp index 3d9fd78bd..37345e7d1 100644 --- a/src/tools/otherTools.cpp +++ b/src/tools/otherTools.cpp @@ -333,6 +333,19 @@ std::string kiwix::render_template(const std::string& template_str, kainjow::mus namespace { +std::string escapeBackslashes(const std::string& s) +{ + std::string es; + es.reserve(s.size()); + for (char c : s) { + if ( c == '\\' ) { + es.push_back('\\'); + } + es.push_back(c); + } + return es; +} + std::string makeFulltextSearchSuggestion(const std::string& lang, const std::string& queryString) { @@ -358,10 +371,10 @@ void kiwix::Suggestions::add(const zim::SuggestionItem& suggestion) ? suggestion.getSnippet() : suggestion.getTitle(); - result.set("label", label); - result.set("value", suggestion.getTitle()); + result.set("label", escapeBackslashes(label)); + result.set("value", escapeBackslashes(suggestion.getTitle())); result.set("kind", "path"); - result.set("path", suggestion.getPath()); + result.set("path", escapeBackslashes(suggestion.getPath())); result.set("first", m_data.is_empty_list()); m_data.push_back(result); } @@ -370,8 +383,9 @@ void kiwix::Suggestions::addFTSearchSuggestion(const std::string& uiLang, const std::string& queryString) { kainjow::mustache::data result; - result.set("label", makeFulltextSearchSuggestion(uiLang, queryString)); - result.set("value", queryString + " "); + const std::string label = makeFulltextSearchSuggestion(uiLang, queryString); + result.set("label", escapeBackslashes(label)); + result.set("value", escapeBackslashes(queryString + " ")); result.set("kind", "pattern"); result.set("first", m_data.is_empty_list()); m_data.push_back(result);