Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions features/element-hiding.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,13 +684,7 @@
"rules": [
{
"selector": ".navigation",
"type": "modify-style",
"values": [
{
"property": "top",
"value": "0px"
}
]
"type": "modify-style"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing Values Property in Modify-Style Rule

The modify-style rule for abcnews.go.com's .navigation selector is missing its required values property. Per the element-hiding.ts schema, modify-style rules need this array to define style modifications, making the current rule invalid and non-functional.

Fix in Cursor Fix in Web

}
]
},
Expand Down
2 changes: 2 additions & 0 deletions schema/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { CustomUserAgentFeature } from './features/custom-user-agent';
import { BurnFeature } from './features/burn';
import { Taskbar } from './features/taskbar';
import { AppHealth } from './features/appHealth';
import { ElementHidingFeature } from './features/element-hiding';

export { WebCompatSettings } from './features/webcompat';
export { DuckPlayerSettings } from './features/duckplayer';
Expand Down Expand Up @@ -65,6 +66,7 @@ export type ConfigV5<VersionType> = {
scriptlets?: ScriptletsFeature<VersionType>;
windowsWebviewFailures?: WindowsWebViewFailures<VersionType>;
customUserAgent?: CustomUserAgentFeature<VersionType>;
elementHiding?: ElementHidingFeature<VersionType>;
};
unprotectedTemporary: SiteException[];
};
Expand Down
55 changes: 55 additions & 0 deletions schema/features/element-hiding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Feature } from '../feature';

type ElementHidingRuleType = 'hide-empty' | 'hide' | 'closest-empty' | 'override' | 'modify-style' | 'modify-attr' | 'disable-default';

type StyleValue = {
property: string;
value: string;
};

type AttributeValue = {
attribute: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value: string;
};

type BaseElementHidingRule = {
selector: string;
type: ElementHidingRuleType;
};

type StyleRule = BaseElementHidingRule & {
type: 'modify-style';
values: StyleValue[];
};

type AttributeRule = BaseElementHidingRule & {
type: 'modify-attr';
values: AttributeValue[];
};

type DisableDefaultRule = {
type: 'disable-default';
};

type OverrideRule = BaseElementHidingRule & {
type: 'override';
};

type ElementHidingRule = BaseElementHidingRule | StyleRule | AttributeRule | DisableDefaultRule | OverrideRule;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type ElementHidingRule = BaseElementHidingRule | StyleRule | AttributeRule | DisableDefaultRule | OverrideRule;
type ElementHidingRule = StyleRule | AttributeRule | DisableDefaultRule | OverrideRule;

The base isn't needed here I don't think?


type GlobalRules = ElementHidingRule[];

type DomainRules = {
domain: string | string[];
rules: ElementHidingRule[];
};

type ElementHidingSettings = {
rules: GlobalRules;
domains: DomainRules[];
[key: string]: any;
};

export type ElementHidingFeature<VersionType> = Feature<ElementHidingSettings, VersionType>;

export type ElementHidingSettingsType = ElementHidingSettings;
6 changes: 3 additions & 3 deletions tests/config-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from 'chai';
import fs from 'fs';
import path from 'path';
import { createValidator, formatErrors } from './schema-validation.js';
import { createValidator, createHybridValidator, formatErrors } from './schema-validation.js';
import platforms from './../platforms.js';
import { immutableJSONPatch } from 'immutable-json-patch';
import { getBaseFeatureConfigs } from '../util.js';
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Config schema tests', () => {
});

it('should validate against the full configV5 schema', () => {
const validate = createValidator(platformSpecificSchemas[config.name] || 'CurrentGenericConfig');
const validate = createHybridValidator(platformSpecificSchemas[config.name] || 'CurrentGenericConfig');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a red flag there's a change needed here. or is this just temporary?

expect(validate(config.body)).to.be.equal(true, formatErrors(validate.errors));
});

Expand Down Expand Up @@ -128,7 +128,7 @@ describe('Config schema tests', () => {
});

it('All patchSettings should also be valid', () => {
const validate = createValidator(platformSpecificSchemas[config.name] || 'CurrentGenericConfig');
const validate = createHybridValidator(platformSpecificSchemas[config.name] || 'CurrentGenericConfig');
function applyPatchAndValidate(featureName, feature, conditionalChange, config) {
for (const change of conditionalChange) {
if (!change.patchSettings) {
Expand Down
182 changes: 182 additions & 0 deletions tests/schema-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,185 @@ export function formatErrors(errors) {
})
.join(', ');
}

function validateElementHidingRulesOptimized(rules) {
if (!Array.isArray(rules)) {
return { valid: true };
}

const simpleRules = [];
const complexRules = [];

for (let i = 0; i < rules.length; i++) {
const rule = rules[i];

if (!rule.type) {
return {
valid: false,
error: `Rule ${i}: Missing required 'type' property`,
};
}

if (
[
'modify-style',
'modify-attr',
].includes(rule.type)
) {
complexRules.push({ rule, index: i });
} else {
simpleRules.push({ rule, index: i });
}
}

const simpleValidation = validateSimpleRules(simpleRules);
if (!simpleValidation.valid) return simpleValidation;

const complexValidation = validateComplexRules(complexRules);
if (!complexValidation.valid) return complexValidation;

return { valid: true };
}

function validateSimpleRules(simpleRules) {
for (const { rule, index } of simpleRules) {
if (
[
'hide-empty',
'hide',
'closest-empty',
'override',
].includes(rule.type)
) {
if (!rule.selector) {
return {
valid: false,
error: `Rule ${index}: ${rule.type} rules must have 'selector' property`,
};
}
}
}
return { valid: true };
}

function validateComplexRules(complexRules) {
for (const { rule, index } of complexRules) {
if (rule.type === 'modify-style') {
if (!rule.values || !Array.isArray(rule.values)) {
return {
valid: false,
error: `Rule ${index}: modify-style rules must have 'values' array property`,
};
}
for (let j = 0; j < rule.values.length; j++) {
const value = rule.values[j];
if (!value.property || !value.value) {
return {
valid: false,
error: `Rule ${index}, value ${j}: Style values must have 'property' and 'value' properties`,
};
}
}
}

if (rule.type === 'modify-attr') {
if (!rule.values || !Array.isArray(rule.values)) {
return {
valid: false,
error: `Rule ${index}: modify-attr rules must have 'values' array property`,
};
}
for (let j = 0; j < rule.values.length; j++) {
const value = rule.values[j];
if (!value.attribute || !value.value) {
return {
valid: false,
error: `Rule ${index}, value ${j}: Attribute values must have 'attribute' and 'value' properties`,
};
}
}
}
}
return { valid: true };
}

function validateElementHidingDomains(domains) {
if (!Array.isArray(domains)) {
return { valid: true };
}

for (let i = 0; i < domains.length; i++) {
const domain = domains[i];

if (!domain.domain) {
return {
valid: false,
error: `Domain ${i}: Missing required 'domain' property`,
};
}

if (typeof domain.domain !== 'string' && !Array.isArray(domain.domain)) {
return {
valid: false,
error: `Domain ${i}: 'domain' must be string or array of strings`,
};
}

if (domain.rules) {
const rulesValidation = validateElementHidingRulesOptimized(domain.rules);
if (!rulesValidation.valid) {
return {
valid: false,
error: `Domain ${i}: ${rulesValidation.error}`,
};
}
}
}

return { valid: true };
}

export function createHybridValidator(schemaName) {
const basicValidator = createValidator(schemaName);

return function validate(config) {
const basicResult = basicValidator(config);
if (!basicResult) {
return false;
}

if (config.features?.elementHiding?.settings) {
const settings = config.features.elementHiding.settings;

if (settings.rules) {
const rulesValidation = validateElementHidingRulesOptimized(settings.rules);
if (!rulesValidation.valid) {
validate.errors = [
{
instancePath: '/features/elementHiding/settings/rules',
message: rulesValidation.error,
params: {},
},
];
return false;
}
}

if (settings.domains) {
const domainsValidation = validateElementHidingDomains(settings.domains);
if (!domainsValidation.valid) {
validate.errors = [
{
instancePath: '/features/elementHiding/settings/domains',
message: domainsValidation.error,
params: {},
},
];
return false;
}
}
}

return true;
};
}
Loading