From 6fe86fbda1b5159a930ac76fc159bb63485de933 Mon Sep 17 00:00:00 2001 From: Stephen Heaps Date: Mon, 9 Sep 2024 19:05:29 -0400 Subject: [PATCH 1/2] Check for significant text before checking for 3rd party resource --- .../Scripts/Sandboxed/SelectorsPollerScript.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js index 4a79fb6cf25b..6516a8720ba1 100644 --- a/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js +++ b/ios/brave-ios/Sources/Brave/Frontend/UserContent/UserScripts/Scripts_Dynamic/Scripts/Sandboxed/SelectorsPollerScript.js @@ -564,6 +564,10 @@ window.__firefox__.execute(function($) { return false } else if (queryResults.foundFirstPartyResource) { return true + } else if (showsSignificantText(element)) { + // If the subtree HAS a significant amount of text (e.g., it doesn't just says "Advertisement") + // it should be unhidden. + return true } else if (queryResults.foundThirdPartyResource || queryResults.pendingSrcAttributes.size > 0) { if (pendingSrcAttributes !== undefined) { queryResults.pendingSrcAttributes.forEach((src) => { @@ -572,12 +576,8 @@ window.__firefox__.execute(function($) { } return false - } else { - // If the subtree doesn't have a significant amount of text (e.g., it - // just says "Advertisement"), then no need to change anything; it should - // stay hidden. - return showsSignificantText(element) } + return false } const shouldUnhideElementAsync = async (element) => { From 8cdf68a196cc178ab5325dbbd08bf75e6a664b24 Mon Sep 17 00:00:00 2001 From: Stephen Heaps Date: Mon, 9 Sep 2024 20:51:46 -0400 Subject: [PATCH 2/2] Add unit test for significant text fix. --- ios/brave-ios/Tests/ClientTests/Resources/html/index.html | 7 ++++++- .../ClientTests/User Scripts/ScriptExecutionTests.swift | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ios/brave-ios/Tests/ClientTests/Resources/html/index.html b/ios/brave-ios/Tests/ClientTests/Resources/html/index.html index 9850369aa42a..39f812877bb3 100644 --- a/ios/brave-ios/Tests/ClientTests/Resources/html/index.html +++ b/ios/brave-ios/Tests/ClientTests/Resources/html/index.html @@ -27,7 +27,12 @@

Test Ads

- This element should be always hidden (standard or aggressive mode) because it's a 3rd party ad. +
+ +
+ + This element should be not be hidden in standard mode because it contains significant amount of text. + It should still be hidden in aggressive mode.
diff --git a/ios/brave-ios/Tests/ClientTests/User Scripts/ScriptExecutionTests.swift b/ios/brave-ios/Tests/ClientTests/User Scripts/ScriptExecutionTests.swift index a0fab7241dd0..b0c1b0f037e8 100644 --- a/ios/brave-ios/Tests/ClientTests/User Scripts/ScriptExecutionTests.swift +++ b/ios/brave-ios/Tests/ClientTests/User Scripts/ScriptExecutionTests.swift @@ -224,7 +224,9 @@ final class ScriptExecutionTests: XCTestCase { let initialStandardSelectors = Set([".test-ads-primary-standard div"]) let initialAggressiveSelectors = Set([".test-ads-primary-aggressive div"]) let polledAggressiveIds = ["test-ad-aggressive"] - let polledStandardIds = ["test-ad-1st-party", "test-ad-3rd-party", "test-ad-simple"] + let polledStandardIds = [ + "test-ad-1st-party", "test-ad-3rd-party", "test-ad-3rd-party-sig-text", "test-ad-simple", + ] let nestedIds = [ "test-ad-primary-standard-1st-party", "test-ad-primary-standard-3rd-party", "test-ad-primary-aggressive-1st-party", "test-ad-primary-aggressive-3rd-party", @@ -378,7 +380,10 @@ final class ScriptExecutionTests: XCTestCase { ) XCTAssertEqual(resultsAfterPump?.unhiddenIds.contains("test-ad-1st-party"), true) XCTAssertEqual(resultsAfterPump?.hiddenIds.contains("test-ad-aggressive"), true) + // hidden because of 3rd party src XCTAssertEqual(resultsAfterPump?.hiddenIds.contains("test-ad-3rd-party"), true) + // unhidden because it contains significant amount of text + XCTAssertEqual(resultsAfterPump?.unhiddenIds.contains("test-ad-3rd-party-sig-text"), true) XCTAssertEqual(resultsAfterPump?.hiddenIds.contains("test-ad-simple"), true) XCTAssertEqual( resultsAfterPump?.hiddenIds.contains("test-ad-primary-aggressive-1st-party"),