Skip to content

Commit 5931c7e

Browse files
Rename matchDomainFeatureSetting to matchConditionalFeatureSetting
Additionally ensure all checks match
1 parent e7e0ac9 commit 5931c7e

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

injected/src/config-feature.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ export default class ConfigFeature {
4242
}
4343

4444
/**
45-
* Given a config key, interpret the value as a list of domain overrides, and return the elements that match the current page
46-
* Consider using patchSettings instead as per `getFeatureSetting`.
45+
* Given a config key, interpret the value as a list of conditionals objects, and return the elements that match the current page
46+
* Consider in your feature using patchSettings instead as per `getFeatureSetting`.
4747
* @param {string} featureKeyName
4848
* @return {any[]}
4949
* @protected
5050
*/
51-
matchDomainFeatureSetting(featureKeyName) {
51+
matchConditionalFeatureSetting(featureKeyName) {
5252
const domains = this._getFeatureSettings()?.[featureKeyName] || [];
5353
return domains.filter((rule) => {
54-
return this.matchConditionalChanges(rule);
54+
// Support shorthand for domain matching for backwards compatibility
55+
return this.matchConditionalChanges(rule.condition || { domain: rule.domain });
5556
});
5657
}
5758

@@ -69,16 +70,25 @@ export default class ConfigFeature {
6970
* @returns {boolean}
7071
*/
7172
matchConditionalChanges(conditionBlock) {
72-
// Check domain condition
73-
if (conditionBlock.domain && !this._matchDomainConditional(conditionBlock)) {
74-
return false;
75-
}
76-
77-
// Check URL pattern condition
78-
if (conditionBlock.urlPattern) {
79-
const pattern = new URLPattern(conditionBlock.urlPattern);
80-
if (!this.args?.site.url || !pattern.test(this.args?.site.url)) {
81-
return false;
73+
for (const key in conditionBlock) {
74+
switch (key) {
75+
case 'domain': {
76+
if (!this._matchDomainConditional(conditionBlock)) {
77+
return false;
78+
}
79+
break;
80+
}
81+
case 'urlPattern': {
82+
const pattern = new URLPattern(conditionBlock.urlPattern);
83+
if (!this.args?.site.url || !pattern.test(this.args?.site.url)) {
84+
return false;
85+
}
86+
break;
87+
}
88+
// For unknown keys we should assume the condition is not met
89+
default: {
90+
return false;
91+
}
8292
}
8393
}
8494

@@ -172,14 +182,12 @@ export default class ConfigFeature {
172182
throw new Error(`${featureKeyName} is a reserved feature setting key name`);
173183
}
174184
// We only support one of these keys at a time, where conditionalChanges takes precedence
175-
// TODO should we rename these?
176-
// TODO should we only support conditionalChanges to support other types of settings?
177185
let conditionalMatches = [];
178186
// Presence check using result to avoid the [] default response
179187
if (result?.conditionalChanges) {
180-
conditionalMatches = this.matchDomainFeatureSetting('conditionalChanges');
188+
conditionalMatches = this.matchConditionalFeatureSetting('conditionalChanges');
181189
} else {
182-
conditionalMatches = this.matchDomainFeatureSetting('domains');
190+
conditionalMatches = this.matchConditionalFeatureSetting('domains');
183191
}
184192
for (const match of conditionalMatches) {
185193
if (match.patchSettings === undefined) {

injected/src/features/element-hiding.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ export default class ElementHiding extends ContentFeature {
320320

321321
// determine whether strict hide rules should be injected as a style tag
322322
if (shouldInjectStyleTag) {
323-
shouldInjectStyleTag = this.matchDomainFeatureSetting('styleTagExceptions').length === 0;
323+
shouldInjectStyleTag = this.matchConditionalFeatureSetting('styleTagExceptions').length === 0;
324324
}
325325

326326
// collect all matching rules for domain
327-
const activeDomainRules = this.matchDomainFeatureSetting('domains').flatMap((item) => item.rules);
327+
const activeDomainRules = this.matchConditionalFeatureSetting('domains').flatMap((item) => item.rules);
328328

329329
const overrideRules = activeDomainRules.filter((rule) => {
330330
return rule.type === 'override';

injected/src/features/navigator-interface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createPageWorldBridge } from './message-bridge/create-page-world-bridge
44

55
export default class NavigatorInterface extends ContentFeature {
66
load(args) {
7-
if (this.matchDomainFeatureSetting('privilegedDomains').length) {
7+
if (this.matchConditionalFeatureSetting('privilegedDomains').length) {
88
this.injectNavigatorInterface(args);
99
}
1010
}

injected/unit-test/content-feature.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ describe('ContentFeature class', () => {
6464
expect(this.getFeatureSetting('pathTestPlaceholder')).toBe('place');
6565
expect(this.getFeatureSetting('domainWildcard')).toBe('wildwest');
6666
expect(this.getFeatureSetting('domainWildcardNope')).toBe('nope');
67+
expect(this.getFeatureSetting('invalidCheck')).toBe('nope');
6768
didRun = true;
6869
}
6970
}
@@ -86,6 +87,7 @@ describe('ContentFeature class', () => {
8687
pathTestPlaceholder: 'nope',
8788
domainWildcard: 'nope',
8889
domainWildcardNope: 'nope',
90+
invalidCheck: 'nope',
8991
conditionalChanges: [
9092
{
9193
domain: 'example.com',
@@ -103,38 +105,59 @@ describe('ContentFeature class', () => {
103105
patchSettings: [{ op: 'replace', path: '/arrayTest', value: 'enabledArray' }],
104106
},
105107
{
106-
urlPattern: {
107-
path: '/path/path/me',
108+
condition: {
109+
urlPattern: {
110+
path: '/path/path/me',
111+
},
108112
},
109113
patchSettings: [{ op: 'replace', path: '/pathTest', value: 'beep' }],
110114
},
111115
{
112-
urlPattern: {
113-
hostname: 'beep.nope.com',
114-
path: '/path/path/me',
116+
condition: {
117+
urlPattern: {
118+
hostname: 'beep.nope.com',
119+
path: '/path/path/me',
120+
},
115121
},
116122
patchSettings: [{ op: 'replace', path: '/pathTestNotApply', value: 'yep' }],
117123
},
118124
{
119-
urlPattern: 'http://beep.example.com/path/path/me',
125+
condition: {
126+
urlPattern: 'http://beep.example.com/path/path/me',
127+
},
120128
patchSettings: [{ op: 'replace', path: '/pathTestShort', value: 'beep' }],
121129
},
122130
{
123-
urlPattern: 'http://beep.example.com/*/path/me',
131+
condition: {
132+
urlPattern: 'http://beep.example.com/*/path/me',
133+
},
124134
patchSettings: [{ op: 'replace', path: '/pathTestAsterix', value: 'comic' }],
125135
},
126136
{
127-
urlPattern: 'http://beep.example.com/:something/path/me',
137+
condition: {
138+
urlPattern: 'http://beep.example.com/:something/path/me',
139+
},
128140
patchSettings: [{ op: 'replace', path: '/pathTestPlaceholder', value: 'place' }],
129141
},
130142
{
131-
urlPattern: 'http://beep.*.com/*/path/me',
143+
condition: {
144+
urlPattern: 'http://beep.*.com/*/path/me',
145+
},
132146
patchSettings: [{ op: 'replace', path: '/domainWildcard', value: 'wildwest' }],
133147
},
134148
{
135-
urlPattern: 'http://nope.*.com/*/path/me',
149+
condition: {
150+
urlPattern: 'http://nope.*.com/*/path/me',
151+
},
136152
patchSettings: [{ op: 'replace', path: '/domainWildcardNope', value: 'wildwest' }],
137153
},
154+
{
155+
condition: {
156+
somethingInvalid: true,
157+
urlPattern: 'http://beep.example.com/*/path/me',
158+
},
159+
patchSettings: [{ op: 'replace', path: '/invalidCheck', value: 'neverhappened' }],
160+
}
138161
],
139162
},
140163
},

0 commit comments

Comments
 (0)