Support html translate=no on iOS
Bug: 414508113 Change-Id: Ie0b462fbd202ce6537e6c475a3f7262701b741b8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6513508 Reviewed-by: Trevor Perrier <perrier@chromium.org> Commit-Queue: Olivier Robin <olivierrobin@chromium.org> Auto-Submit: Olivier Robin <olivierrobin@chromium.org> Cr-Commit-Position: refs/heads/main@{#1456978}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
b4b19378dc
commit
cfc38c0f57
components/language/ios/browser
@ -209,6 +209,43 @@ TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithNoTranslateMeta) {
|
||||
[handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
|
||||
}
|
||||
|
||||
// Tests if `__gCrWeb.languageDetection.detectLanguage` correctly informs the
|
||||
// native side when the notranslate html attribute is specified
|
||||
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithHTMLNoTranslate) {
|
||||
// A simple page using the notranslate meta tag.
|
||||
NSString* html = @"<html translate='no'><head>"
|
||||
@"<meta http-equiv='content-language' content='foo'>"
|
||||
@"</head></html>";
|
||||
LoadHtml(html);
|
||||
ASSERT_TRUE(TriggerLanguageDetection());
|
||||
|
||||
ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
|
||||
EXPECT_NSEQ(@"foo",
|
||||
handler().lastReceivedMessage.body[@"httpContentLanguage"]);
|
||||
ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
|
||||
EXPECT_TRUE(
|
||||
[handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
|
||||
}
|
||||
|
||||
// Tests if `__gCrWeb.languageDetection.detectLanguage` does not confuse a body
|
||||
// or div language='no' for a page wide translate disabling.
|
||||
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithDIVNoTranslate) {
|
||||
// A simple page using the notranslate meta tag.
|
||||
NSString* html = @"<html><head>"
|
||||
@"<meta http-equiv='content-language' content='foo'>"
|
||||
@"</head><body translate='no'>"
|
||||
@"<div translate='no'>test</div></html>";
|
||||
LoadHtml(html);
|
||||
ASSERT_TRUE(TriggerLanguageDetection());
|
||||
|
||||
ASSERT_TRUE(handler().lastReceivedMessage.body[@"httpContentLanguage"]);
|
||||
EXPECT_NSEQ(@"foo",
|
||||
handler().lastReceivedMessage.body[@"httpContentLanguage"]);
|
||||
ASSERT_TRUE(handler().lastReceivedMessage.body[@"hasNoTranslate"]);
|
||||
EXPECT_FALSE(
|
||||
[handler().lastReceivedMessage.body[@"hasNoTranslate"] boolValue]);
|
||||
}
|
||||
|
||||
// Tests if `__gCrWeb.languageDetection.detectLanguage` correctly informs the
|
||||
// native side when no notranslate meta tag is specified.
|
||||
TEST_F(LanguageDetectionJavascriptTest, DetectLanguageWithoutNoTranslateMeta) {
|
||||
|
@ -22,9 +22,16 @@ let activeRequests = 0;
|
||||
|
||||
/**
|
||||
* Searches page elements for "notranslate" meta tag.
|
||||
* @return true if "notranslate" meta tag is defined.
|
||||
* @return true if "notranslate" meta tag is defined or the translate attribute
|
||||
* equal to no on html document.
|
||||
*/
|
||||
function hasNoTranslate(): boolean {
|
||||
if (document.documentElement.hasAttribute('translate')) {
|
||||
if (document.documentElement.getAttribute('translate')!.toLowerCase() ===
|
||||
'no') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (const metaTag of document.getElementsByTagName('meta')) {
|
||||
if (metaTag.name === 'google') {
|
||||
if (metaTag.content === 'notranslate' ||
|
||||
|
Reference in New Issue
Block a user