Skip to content

Commit 969d667

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Ignore AI Assistance selectors
Fixed: 405049967 Change-Id: I4c254f2787a367d4dba8d96ae07c462a98f6f622 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6375523 Reviewed-by: Ergün Erdoğmuş <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]>
1 parent be289f6 commit 969d667

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

front_end/models/ai_assistance/ExtensionScope.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,33 @@ describe('ExtensionScope', () => {
212212
assert.strictEqual(selector, 'div.container > .header');
213213
});
214214

215+
it('should skip selector with ai assistance prefix', async () => {
216+
// Order is reversed we know that specificity order will
217+
// be returned correctly
218+
// front_end/core/sdk/CSSMatchedStyles.ts:373
219+
const matchedPayload = [
220+
ruleMatch('.test', MOCK_STYLE),
221+
ruleMatch(`.${Injected.AI_ASSISTANCE_CSS_CLASS_NAME}-1`, MOCK_STYLE),
222+
];
223+
const selector = await getSelector({matchedPayload});
224+
assert.strictEqual(selector, '.test');
225+
});
226+
it('should skip selector with ai assistance prefix in complex selector', async () => {
227+
// Order is reversed we know that specificity order will
228+
// be returned correctly
229+
// front_end/core/sdk/CSSMatchedStyles.ts:373
230+
const matchedPayload = [
231+
ruleMatch(
232+
{
233+
selectors: [{text: `.${Injected.AI_ASSISTANCE_CSS_CLASS_NAME}-1`}, {text: '.test'}],
234+
text: `.${Injected.AI_ASSISTANCE_CSS_CLASS_NAME}-1, .test`
235+
},
236+
MOCK_STYLE),
237+
];
238+
const selector = await getSelector({matchedPayload});
239+
assert.strictEqual(selector, '.test');
240+
});
241+
215242
it('should skip nested selector with ai assistance prefix', async () => {
216243
// Order is reversed we know that specificity order will
217244
// be returned correctly

front_end/models/ai_assistance/ExtensionScope.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ export class ExtensionScope {
162162
break;
163163
}
164164
if (rule instanceof SDK.CSSRule.CSSStyleRule) {
165-
if (rule.nestingSelectors?.at(0)?.includes(AI_ASSISTANCE_CSS_CLASS_NAME)) {
165+
if (rule.nestingSelectors?.at(0)?.includes(AI_ASSISTANCE_CSS_CLASS_NAME) ||
166+
rule.selectors.every(selector => selector.text.includes(AI_ASSISTANCE_CSS_CLASS_NAME))) {
166167
// If the rule we created was our continue to get the correct location
167168
continue;
168169
}
@@ -174,28 +175,36 @@ export class ExtensionScope {
174175
}
175176

176177
static getSelectorsFromStyleRule(
177-
styleRule: SDK.CSSRule.CSSStyleRule, matchedStyles: SDK.CSSMatchedStyles.CSSMatchedStyles): string {
178+
styleRule: SDK.CSSRule.CSSStyleRule,
179+
matchedStyles: SDK.CSSMatchedStyles.CSSMatchedStyles,
180+
): string {
178181
const selectorIndexes = matchedStyles.getMatchingSelectors(styleRule);
179182
// TODO: Compute the selector when nested selector is present
180-
const selectors = styleRule.selectors.filter((_, index) => selectorIndexes.includes(index)).sort((a, b) => {
181-
if (!a.specificity) {
182-
return -1;
183-
}
184-
185-
if (!b.specificity) {
186-
return 1;
187-
}
188-
189-
if (b.specificity.a !== a.specificity.a) {
190-
return b.specificity.a - a.specificity.a;
191-
}
192-
193-
if (b.specificity.b !== a.specificity.b) {
194-
return b.specificity.b - a.specificity.b;
195-
}
196-
197-
return b.specificity.b - a.specificity.b;
198-
});
183+
const selectors = styleRule
184+
.selectors
185+
// Filter out only selector that apply rules
186+
.filter((_, index) => selectorIndexes.includes(index))
187+
// Ignore selector that include AI selector name
188+
.filter(value => !value.text.includes(AI_ASSISTANCE_CSS_CLASS_NAME))
189+
.sort((a, b) => {
190+
if (!a.specificity) {
191+
return -1;
192+
}
193+
194+
if (!b.specificity) {
195+
return 1;
196+
}
197+
198+
if (b.specificity.a !== a.specificity.a) {
199+
return b.specificity.a - a.specificity.a;
200+
}
201+
202+
if (b.specificity.b !== a.specificity.b) {
203+
return b.specificity.b - a.specificity.b;
204+
}
205+
206+
return b.specificity.b - a.specificity.b;
207+
});
199208

200209
const selector = selectors.at(0);
201210
if (!selector) {

0 commit comments

Comments
 (0)