Skip to content

Commit 1a37f2b

Browse files
committed
Framework: Add a whenAttrChangedOnAncestor option to prevent a tweak from being applied when an attribute changes on an ancestor.
1 parent f227e68 commit 1a37f2b

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

framework/axSGreaseSkeleton.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// @namespace http://axSgrease.nvaccess.org/
44
// @description Improves the accessibility of some site.
55
// @author James Teh <jteh@mozilla.com>
6-
// @copyright 2019 Mozilla Corporation, Derek Riemer
6+
// @copyright 2019-2022 Mozilla Corporation, Derek Riemer
77
// @license Mozilla Public License version 2.0
88
// @version 2019.1
99
// @include https://some.site/*
@@ -83,13 +83,15 @@ function applyTweak(el, tweak) {
8383
}
8484
}
8585

86-
function applyTweaks(root, tweaks, checkRoot) {
86+
function applyTweaks(root, tweaks, checkRoot, forAttrChange=false) {
8787
for (let tweak of tweaks) {
88-
for (let el of root.querySelectorAll(tweak.selector)) {
89-
try {
90-
applyTweak(el, tweak);
91-
} catch (e) {
92-
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
88+
if (!forAttrChange || tweak.whenAttrChangedOnAncestor !== false) {
89+
for (let el of root.querySelectorAll(tweak.selector)) {
90+
try {
91+
applyTweak(el, tweak);
92+
} catch (e) {
93+
console.log("Exception while applying tweak for '" + tweak.selector + "': " + e);
94+
}
9395
}
9496
}
9597
if (checkRoot && root.matches(tweak.selector)) {
@@ -113,7 +115,7 @@ let observer = new MutationObserver(function(mutations) {
113115
applyTweaks(node, DYNAMIC_TWEAKS, true);
114116
}
115117
} else if (mutation.type === "attributes") {
116-
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true);
118+
applyTweaks(mutation.target, DYNAMIC_TWEAKS, true, true);
117119
}
118120
} catch (e) {
119121
// Catch exceptions for individual mutations so other mutations are still handled.

framework/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ if `DYNAMIC_TWEAK_ATTRIBS` is empty, no attributes will be observed.
2626
In the `LOAD_TWEAKS` and `DYNAMIC_TWEAKS` arrays, each tweak is an object with these keys:
2727

2828
- `selector`: A CSS selector for the element(s) you want to tweak.
29+
- whenAttrChangedOnAncestor: Whether to apply this tweak when an attribute change occurs on an ancestor.
30+
By default, the tweak will apply for attribute changes on both the node itself, as well as for an attribute change on any ancestor.
31+
This can be problematic if, for example, you're using the style attribute to make a decision about focus, but the style changes on an ancestor.
32+
In that case, you can set this to false.
2933
- `tweak`: Either:
3034
1. A function which is passed a single element to tweak.
3135
For example:

0 commit comments

Comments
 (0)