Commit e473e2b9 authored by Yue Li's avatar Yue Li Committed by Commit Bot

Quick Answers: Unescape ampersand character codes

The Cloud translation API response might contains ampersand character
codes such as '
Unescape such character codes for translation response.

Bug: b/173569400
Test: Run TranslationResponseParserTest.* in chromeos_components_unittests
Change-Id: I99774a6ca3fc14650c032dec89884e5c4187a585
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2546472
Commit-Queue: Yue Li <updowndota@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarJeroen Dhollander <jeroendh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828895}
parent cf80448b
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/values.h"
#include "chromeos/components/quick_answers/search_result_parsers/result_parser.h"
#include "chromeos/components/quick_answers/utils/quick_answers_utils.h"
namespace chromeos {
namespace quick_answers {
......@@ -51,21 +52,22 @@ void TranslationResponseParser::OnJsonParsed(
DCHECK(translations->GetList().size() == 1);
const std::string* translated_text =
const std::string* translated_text_ptr =
translations->GetList().front().FindStringPath("translatedText");
if (!translated_text) {
if (!translated_text_ptr) {
LOG(ERROR) << "Can't find a translated text.";
std::move(complete_callback_).Run(nullptr);
return;
}
auto translated_text = UnescapeStringForHTML(*translated_text_ptr);
auto quick_answer = std::make_unique<QuickAnswer>();
quick_answer->result_type = ResultType::kTranslationResult;
quick_answer->primary_answer = *translated_text;
quick_answer->primary_answer = translated_text;
quick_answer->secondary_answer = title_text;
quick_answer->title.push_back(std::make_unique<QuickAnswerText>(title_text));
quick_answer->first_answer_row.push_back(
std::make_unique<QuickAnswerResultText>(*translated_text));
std::make_unique<QuickAnswerResultText>(translated_text));
std::move(complete_callback_).Run(std::move(quick_answer));
}
......
......@@ -74,6 +74,30 @@ TEST_F(TranslationResponseParserTest, ProcessResponseSuccess) {
EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type);
}
TEST_F(TranslationResponseParserTest,
ProcessResponseWithAmpersandCharacterCodes) {
constexpr char kTranslationResponse[] = R"(
{
"data": {
"translations": [
{
"translatedText": "don&#39;t mess with me"
}
]
}
}
)";
constexpr char kTranslationTitle[] = "non scherzare con me · Italian";
translation_response_parser_->ProcessResponse(
std::make_unique<std::string>(kTranslationResponse), kTranslationTitle);
WaitForResponse();
EXPECT_TRUE(quick_answer_);
// Should correctly unescape ampersand character codes.
EXPECT_EQ("don't mess with me", quick_answer_->primary_answer);
EXPECT_EQ(kTranslationTitle, quick_answer_->secondary_answer);
EXPECT_EQ(ResultType::kTranslationResult, quick_answer_->result_type);
}
TEST_F(TranslationResponseParserTest, ProcessResponseNoResults) {
constexpr char kTranslationResponse[] = R"(
{}
......
......@@ -8,6 +8,7 @@
#include "base/strings/stringprintf.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "net/base/escape.h"
#include "ui/base/l10n/l10n_util.h"
namespace chromeos {
......@@ -75,5 +76,9 @@ std::string BuildTranslationTitleText(const std::string& query_text,
base::UTF8ToUTF16(locale_name));
}
std::string UnescapeStringForHTML(const std::string& string) {
return base::UTF16ToUTF8(net::UnescapeForHTML(base::UTF8ToUTF16(string)));
}
} // namespace quick_answers
} // namespace chromeos
......@@ -27,6 +27,10 @@ std::string BuildTranslationTitleText(const IntentInfo& intent_info);
std::string BuildTranslationTitleText(const std::string& query_text,
const std::string& locale_name);
// Unescapes the following ampersand character codes from |string|:
// &lt; &gt; &amp; &quot; &#39;
std::string UnescapeStringForHTML(const std::string& string);
} // namespace quick_answers
} // namespace chromeos
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment