Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHECK removed in favor of character escaping. (uplift to 1.42.x) #14544

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions browser/speedreader/speedreader_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
21 changes: 17 additions & 4 deletions browser/speedreader/speedreader_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpeedreaderTabHelper>(*web_contents) {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions browser/speedreader/speedreader_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down