From 2102e177faed67f3ddaab8503eb8ad8dd70b07a8 Mon Sep 17 00:00:00 2001 From: boocmp Date: Mon, 8 Aug 2022 19:11:29 +0700 Subject: [PATCH] CHECK removed in favor of character escaping. --- .../speedreader/speedreader_browsertest.cc | 17 +++++++++++---- browser/speedreader/speedreader_tab_helper.cc | 21 +++++++++++++++---- browser/speedreader/speedreader_tab_helper.h | 4 ++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/browser/speedreader/speedreader_browsertest.cc b/browser/speedreader/speedreader_browsertest.cc index d09bd7606904..9583eec108f8 100644 --- a/browser/speedreader/speedreader_browsertest.cc +++ b/browser/speedreader/speedreader_browsertest.cc @@ -6,6 +6,7 @@ #include "base/bind.h" #include "base/path_service.h" #include "base/run_loop.h" +#include "base/strings/utf_string_conversions.h" #include "brave/app/brave_command_ids.h" #include "brave/browser/speedreader/speedreader_service_factory.h" #include "brave/browser/speedreader/speedreader_tab_helper.h" @@ -322,23 +323,29 @@ IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, ReloadContent) { } IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, ShowOriginalPage) { + const std::u16string title = u"\u0022\"script shouldn't fail\"\u0022"; + speedreader::test::SetShowOriginalLinkTitle(&title); + ToggleSpeedreader(); NavigateToPageSynchronously(kTestPageReadable); auto* web_contents = ActiveWebContents(); - constexpr const char kClickLink[] = + constexpr const char kClickLinkAndGetTitle[] = R"js( (function() { // element id is hardcoded in extractor.rs const link = document.getElementById('c93e2206-2f31-4ddc-9828-2bb8e8ed940e'); link.click(); + return link.text })(); )js"; - ASSERT_TRUE(content::ExecJs(web_contents, kClickLink, - content::EXECUTE_SCRIPT_DEFAULT_OPTIONS, - speedreader::kIsolatedWorldId)); + EXPECT_EQ(base::UTF16ToUTF8(title), + content::EvalJs(web_contents, kClickLinkAndGetTitle, + content::EXECUTE_SCRIPT_DEFAULT_OPTIONS, + speedreader::kIsolatedWorldId) + .ExtractString()); content::WaitForLoadStop(web_contents); auto* tab_helper = speedreader::SpeedreaderTabHelper::FromWebContents(web_contents); @@ -351,4 +358,6 @@ IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, ShowOriginalPage) { content::WaitForLoadStop(web_contents); EXPECT_EQ(speedreader::DistillState::kSpeedreaderMode, tab_helper->PageDistillState()); + + speedreader::test::SetShowOriginalLinkTitle(nullptr); } diff --git a/browser/speedreader/speedreader_tab_helper.cc b/browser/speedreader/speedreader_tab_helper.cc index cfe63d3b161b..06781dd84814 100644 --- a/browser/speedreader/speedreader_tab_helper.cc +++ b/browser/speedreader/speedreader_tab_helper.cc @@ -37,6 +37,16 @@ namespace speedreader { +namespace test { + +const std::u16string* g_show_original_link_title = nullptr; + +void SetShowOriginalLinkTitle(const std::u16string* title) { + g_show_original_link_title = title; +} + +} // namespace test + SpeedreaderTabHelper::SpeedreaderTabHelper(content::WebContents* web_contents) : content::WebContentsObserver(web_contents), content::WebContentsUserData(*web_contents) { @@ -358,18 +368,21 @@ void SpeedreaderTabHelper::DOMContentLoaded( getElementById('c93e2206-2f31-4ddc-9828-2bb8e8ed940e'); if (!link) return; - link.text = '$1'; + link.text = "$1"; link.addEventListener('click', (e) => { window.speedreader.showOriginalPage(); }) })(); )js"; - const auto link_text = brave_l10n::GetLocalizedResourceUTF16String( + auto link_text = brave_l10n::GetLocalizedResourceUTF16String( IDS_SPEEDREADER_SHOW_ORIGINAL_PAGE_LINK); + if (test::g_show_original_link_title) { + link_text = *test::g_show_original_link_title; + } + // Make sure that the link text doesn't contain js injection - CHECK_EQ(std::u16string::npos, link_text.find(u'\'')); - CHECK_EQ(std::u16string::npos, link_text.find(u'\\')); + base::ReplaceChars(link_text, u"\"", u"\\\"", &link_text); const auto script = base::ReplaceStringPlaceholders(kAddShowOriginalPageLink, link_text, nullptr); diff --git a/browser/speedreader/speedreader_tab_helper.h b/browser/speedreader/speedreader_tab_helper.h index c72c1a5728ad..44a7cd14e1ae 100644 --- a/browser/speedreader/speedreader_tab_helper.h +++ b/browser/speedreader/speedreader_tab_helper.h @@ -25,6 +25,10 @@ class WebContents; namespace speedreader { +namespace test { +void SetShowOriginalLinkTitle(const std::u16string* title); +} + class SpeedreaderBubbleView; // Determines if speedreader should be active for a given top-level navigation.