Skip to content

Commit 802b80f

Browse files
committed
Bug 1801936 - use moz-toggle in the ETP panel r=mstriemer,tgiles,desktop-theme-reviewers,flod,dao
This patch replaces both toggles in the different sub views of the ETP panel with `moz-toggle`. Previously there was a small discrepancy where we were setting an `aria-label` on one toggle but not the other. I added a method to handle updating both toggles at once to ensure they stay in sync/always receive the same updates. Differential Revision: https://phabricator.services.mozilla.com/D176700
1 parent 503938c commit 802b80f

File tree

7 files changed

+101
-146
lines changed

7 files changed

+101
-146
lines changed

browser/base/content/browser-siteProtections.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,8 @@ var gProtectionsHandler = {
15941594

15951595
// Add an observer to observe that the history has been cleared.
15961596
Services.obs.addObserver(this, "browser:purge-session-history");
1597+
1598+
window.ensureCustomElements("moz-button-group", "moz-toggle");
15971599
},
15981600

15991601
uninit() {
@@ -1729,11 +1731,14 @@ var gProtectionsHandler = {
17291731

17301732
onPopupShown(event) {
17311733
if (event.target == this._protectionsPopup) {
1732-
window.ensureCustomElements("moz-button-group");
1733-
17341734
PopupNotifications.suppressWhileOpen(this._protectionsPopup);
17351735

17361736
window.addEventListener("focus", this, true);
1737+
this._protectionsPopupTPSwitch.addEventListener("toggle", this);
1738+
this._protectionsPopupSiteNotWorkingTPSwitch.addEventListener(
1739+
"toggle",
1740+
this
1741+
);
17371742

17381743
// Insert the info message if needed. This will be shown once and then
17391744
// remain collapsed.
@@ -1752,6 +1757,11 @@ var gProtectionsHandler = {
17521757
onPopupHidden(event) {
17531758
if (event.target == this._protectionsPopup) {
17541759
window.removeEventListener("focus", this, true);
1760+
this._protectionsPopupTPSwitch.removeEventListener("toggle", this);
1761+
this._protectionsPopupSiteNotWorkingTPSwitch.removeEventListener(
1762+
"toggle",
1763+
this
1764+
);
17551765
}
17561766
},
17571767

@@ -2025,20 +2035,30 @@ var gProtectionsHandler = {
20252035

20262036
// We handle focus here when the panel is shown.
20272037
handleEvent(event) {
2028-
let elem = document.activeElement;
2029-
let position = elem.compareDocumentPosition(this._protectionsPopup);
2038+
switch (event.type) {
2039+
case "focus": {
2040+
let elem = document.activeElement;
2041+
let position = elem.compareDocumentPosition(this._protectionsPopup);
20302042

2031-
if (
2032-
!(
2033-
position &
2034-
(Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_CONTAINED_BY)
2035-
) &&
2036-
!this._protectionsPopup.hasAttribute("noautohide")
2037-
) {
2038-
// Hide the panel when focusing an element that is
2039-
// neither an ancestor nor descendant unless the panel has
2040-
// @noautohide (e.g. for a tour).
2041-
PanelMultiView.hidePopup(this._protectionsPopup);
2043+
if (
2044+
!(
2045+
position &
2046+
(Node.DOCUMENT_POSITION_CONTAINS |
2047+
Node.DOCUMENT_POSITION_CONTAINED_BY)
2048+
) &&
2049+
!this._protectionsPopup.hasAttribute("noautohide")
2050+
) {
2051+
// Hide the panel when focusing an element that is
2052+
// neither an ancestor nor descendant unless the panel has
2053+
// @noautohide (e.g. for a tour).
2054+
PanelMultiView.hidePopup(this._protectionsPopup);
2055+
}
2056+
break;
2057+
}
2058+
case "toggle": {
2059+
this.onTPSwitchCommand(event);
2060+
break;
2061+
}
20422062
}
20432063
},
20442064

@@ -2067,12 +2087,7 @@ var gProtectionsHandler = {
20672087

20682088
let currentlyEnabled = !this.hasException;
20692089

2070-
for (let tpSwitch of [
2071-
this._protectionsPopupTPSwitch,
2072-
this._protectionsPopupSiteNotWorkingTPSwitch,
2073-
]) {
2074-
tpSwitch.toggleAttribute("enabled", currentlyEnabled);
2075-
}
2090+
this.updateProtectionsToggles(currentlyEnabled);
20762091

20772092
this._notBlockingWhyLink.setAttribute(
20782093
"tooltip",
@@ -2084,13 +2099,6 @@ var gProtectionsHandler = {
20842099
// Toggle the breakage link according to the current enable state.
20852100
this.toggleBreakageLink();
20862101

2087-
// Give the button an accessible label for screen readers.
2088-
document.l10n.setAttributes(
2089-
this._protectionsPopupTPSwitch,
2090-
currentlyEnabled ? "protections-disable" : "protections-enable",
2091-
{ host }
2092-
);
2093-
20942102
// Update the tooltip of the blocked tracker counter.
20952103
this.maybeUpdateEarliestRecordedDateTooltip();
20962104

@@ -2111,6 +2119,31 @@ var gProtectionsHandler = {
21112119
this._protectionsPopup.toggleAttribute("hasException", this.hasException);
21122120
},
21132121

2122+
/**
2123+
* Updates the "pressed" state and labels for both toggles in the different
2124+
* panel subviews.
2125+
*
2126+
* @param {boolean} isPressed - Whether or not the toggles should be pressed.
2127+
* True if ETP is enabled for a given site.
2128+
*/
2129+
updateProtectionsToggles(isPressed) {
2130+
let host = gIdentityHandler.getHostForDisplay();
2131+
for (let toggle of [
2132+
this._protectionsPopupTPSwitch,
2133+
this._protectionsPopupSiteNotWorkingTPSwitch,
2134+
]) {
2135+
toggle.toggleAttribute("pressed", isPressed);
2136+
toggle.toggleAttribute("disabled", !!this._TPSwitchCommanding);
2137+
document.l10n.setAttributes(
2138+
toggle,
2139+
isPressed
2140+
? "protections-panel-etp-on-toggle"
2141+
: "protections-panel-etp-off-toggle",
2142+
{ host }
2143+
);
2144+
}
2145+
},
2146+
21142147
/*
21152148
* This function sorts the category items into the Blocked/Allowed/None Detected
21162149
* sections. It's called immediately in onContentBlockingEvent if the popup
@@ -2203,12 +2236,8 @@ var gProtectionsHandler = {
22032236
// styling after toggling the TP switch.
22042237
let newExceptionState =
22052238
this._protectionsPopup.toggleAttribute("hasException");
2206-
for (let tpSwitch of [
2207-
this._protectionsPopupTPSwitch,
2208-
this._protectionsPopupSiteNotWorkingTPSwitch,
2209-
]) {
2210-
tpSwitch.toggleAttribute("enabled", !newExceptionState);
2211-
}
2239+
2240+
this.updateProtectionsToggles(!newExceptionState);
22122241

22132242
// Toggle the breakage link if needed.
22142243
this.toggleBreakageLink();
@@ -2452,11 +2481,11 @@ var gProtectionsHandler = {
24522481
this._protectionsPopupTPSwitchBreakageLink.hidden =
24532482
ContentBlockingAllowList.includes(gBrowser.selectedBrowser) ||
24542483
!this.anyBlocking ||
2455-
!this._protectionsPopupTPSwitch.hasAttribute("enabled");
2484+
!this._protectionsPopupTPSwitch.hasAttribute("pressed");
24562485
// The "Site Fixed?" link behaves similarly but for the opposite state.
24572486
this._protectionsPopupTPSwitchBreakageFixedLink.hidden =
24582487
!ContentBlockingAllowList.includes(gBrowser.selectedBrowser) ||
2459-
this._protectionsPopupTPSwitch.hasAttribute("enabled");
2488+
this._protectionsPopupTPSwitch.hasAttribute("pressed");
24602489
},
24612490

24622491
submitBreakageReport(uri) {

browser/base/content/test/protectionsUI/browser_protectionsUI.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ add_setup(async function () {
4141
});
4242
});
4343

44+
async function clickToggle(toggle) {
45+
let changed = BrowserTestUtils.waitForEvent(toggle, "toggle");
46+
await EventUtils.synthesizeMouseAtCenter(toggle.buttonEl, {});
47+
await changed;
48+
}
49+
4450
add_task(async function testToggleSwitch() {
4551
let tab = await BrowserTestUtils.openNewForegroundTab(
4652
gBrowser,
@@ -117,15 +123,15 @@ add_task(async function testToggleSwitch() {
117123
await viewShown;
118124

119125
ok(
120-
gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("enabled"),
121-
"TP Switch should be enabled"
126+
gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("pressed"),
127+
"TP Switch should be on"
122128
);
123129
let popuphiddenPromise = BrowserTestUtils.waitForEvent(
124130
gProtectionsHandler._protectionsPopup,
125131
"popuphidden"
126132
);
127133
let browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
128-
gProtectionsHandler._protectionsPopupTPSwitch.click();
134+
await clickToggle(gProtectionsHandler._protectionsPopupTPSwitch);
129135

130136
// The 'Site not working?' link should be hidden after clicking the TP switch.
131137
ok(
@@ -165,8 +171,8 @@ add_task(async function testToggleSwitch() {
165171

166172
await openProtectionsPanel();
167173
ok(
168-
!gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("enabled"),
169-
"TP Switch should be disabled"
174+
!gProtectionsHandler._protectionsPopupTPSwitch.hasAttribute("pressed"),
175+
"TP Switch should be off"
170176
);
171177

172178
// The 'Site not working?' link should be hidden if the TP is off.
@@ -199,7 +205,7 @@ add_task(async function testToggleSwitch() {
199205
// Click the TP switch again and check the visibility of the 'Site not
200206
// Working?'. It should be hidden after toggling the TP switch.
201207
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
202-
gProtectionsHandler._protectionsPopupTPSwitch.click();
208+
await clickToggle(gProtectionsHandler._protectionsPopupTPSwitch);
203209

204210
ok(
205211
BrowserTestUtils.is_hidden(
@@ -398,7 +404,7 @@ add_task(async function testToggleSwitchFlow() {
398404
let browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
399405

400406
// Click the TP switch, from On -> Off.
401-
gProtectionsHandler._protectionsPopupTPSwitch.click();
407+
await clickToggle(gProtectionsHandler._protectionsPopupTPSwitch);
402408

403409
// Check that the icon state has been changed.
404410
ok(
@@ -444,7 +450,7 @@ add_task(async function testToggleSwitchFlow() {
444450
"popupshown"
445451
);
446452
browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
447-
gProtectionsHandler._protectionsPopupTPSwitch.click();
453+
await clickToggle(gProtectionsHandler._protectionsPopupTPSwitch);
448454

449455
// Check that the icon state has been changed.
450456
ok(
@@ -686,7 +692,7 @@ add_task(async function testQuickSwitchTabAfterTogglingTPSwitch() {
686692
);
687693

688694
// Toggle the TP state and switch tab without waiting it to be finished.
689-
gProtectionsHandler._protectionsPopupTPSwitch.click();
695+
await clickToggle(gProtectionsHandler._protectionsPopupTPSwitch);
690696
gBrowser.selectedTab = tabOne;
691697

692698
// Wait for the second tab to be reloaded.

browser/components/controlcenter/content/protectionsPanel.inc.xhtml

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,8 @@
4545
<vbox id="protections-popup-main-body" class="panel-subview-body">
4646
<vbox id="protections-popup-tp-switch-section" class="protections-popup-section protections-popup-switch-section">
4747
<hbox id="protections-popup-tp-switch-section-header" class="protections-popup-switch-section-header">
48-
<vbox class="protections-popup-tp-switch-label-box" flex="1" align="start">
49-
<label class="protections-popup-switch-header protections-popup-tp-switch-on-header"
50-
role="heading"
51-
aria-level="2" data-l10n-id="protections-panel-etp-on-header"></label>
52-
<label class="protections-popup-switch-header protections-popup-tp-switch-off-header"
53-
role="heading"
54-
aria-level="2" data-l10n-id="protections-panel-etp-off-header"></label>
55-
</vbox>
56-
<vbox class="protections-popup-tp-switch-box">
57-
<toolbarbutton id="protections-popup-tp-switch"
58-
class="protections-popup-tp-switch"
59-
enabled="false"
60-
oncommand="gProtectionsHandler.onTPSwitchCommand();" />
48+
<vbox class="protections-popup-tp-switch-box" flex="1" align="stretch">
49+
<html:moz-toggle id="protections-popup-tp-switch" data-l10n-attrs="label, description"></html:moz-toggle>
6150
</vbox>
6251
</hbox>
6352
<hbox id="protections-popup-tp-switch-section-footer">
@@ -193,19 +182,8 @@
193182
role="document"
194183
data-l10n-id="protections-panel-site-not-working-view">
195184
<hbox id="protections-popup-siteNotWorkingView-header">
196-
<vbox class="protections-popup-tp-switch-label-box" flex="1">
197-
<label class="protections-popup-switch-header protections-popup-tp-switch-on-header"
198-
role="heading"
199-
aria-level="1" data-l10n-id="protections-panel-etp-on-header"></label>
200-
<label class="protections-popup-switch-header protections-popup-tp-switch-off-header"
201-
role="heading"
202-
aria-level="1" data-l10n-id="protections-panel-etp-off-header"></label>
203-
</vbox>
204-
<vbox class="protections-popup-tp-switch-box">
205-
<toolbarbutton id="protections-popup-siteNotWorking-tp-switch"
206-
class="protections-popup-tp-switch"
207-
enabled="false"
208-
oncommand="gProtectionsHandler.onTPSwitchCommand();" />
185+
<vbox class="protections-popup-tp-switch-box" flex="1" align="stretch">
186+
<html:moz-toggle id="protections-popup-siteNotWorking-tp-switch" data-l10n-attrs="label, description"></html:moz-toggle>
209187
</vbox>
210188
</hbox>
211189
<toolbarseparator></toolbarseparator>

browser/locales/en-US/browser/protectionsPanel.ftl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ protections-panel-etp-more-info =
2626
protections-panel-etp-on-header = Enhanced Tracking Protection is ON for this site
2727
protections-panel-etp-off-header = Enhanced Tracking Protection is OFF for this site
2828
29+
## Text for the toggles shown when ETP is enabled/disabled for a given site.
30+
## .description is transferred into a separate paragraph by the moz-toggle
31+
## custom element code.
32+
## $host (String): the hostname of the site that is being displayed.
33+
34+
protections-panel-etp-on-toggle =
35+
.label = Enhanced Tracking Protection
36+
.description = On for this site
37+
.aria-label = Disable protections for { $host }
38+
protections-panel-etp-off-toggle =
39+
.label = Enhanced Tracking Protection
40+
.description = Off for this site
41+
.aria-label = Enable protections for { $host }
42+
2943
# The link to be clicked to open the sub-panel view
3044
protections-panel-site-not-working = Site not working?
3145

browser/locales/en-US/browser/siteProtections.ftl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ tracking-protection-icon-no-trackers-detected-container =
4242

4343
# Header of the Protections Panel.
4444
protections-header = Protections for { $host }
45-
# Text that gets spoken by a screen reader if the button will disable protections.
46-
protections-disable =
47-
.aria-label = Disable protections for { $host }
48-
# Text that gets spoken by a screen reader if the button will enable protections.
49-
protections-enable =
50-
.aria-label = Enable protections for { $host }
5145
5246
## Blocking and Not Blocking sub-views in the Protections Panel
5347

0 commit comments

Comments
 (0)