Skip to content

Commit 25d4138

Browse files
committed
Add procedural operator content(...), to lookup elements inside template tags
As discussed internally with team.
1 parent dc119d4 commit 25d4138

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/js/contentscript-extra.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ class PSelectorVoidTask extends PSelectorTask {
6363
}
6464
}
6565

66+
class PSelectorContentTask extends PSelectorTask {
67+
constructor(task) {
68+
super();
69+
this.selector = task[1];
70+
}
71+
transpose(node, output) {
72+
const root = node.content;
73+
if ( root?.querySelectorAll !== 'function' ) { return; }
74+
const nodes = root.querySelectorAll(this.selector);
75+
output.push(...nodes);
76+
}
77+
}
78+
6679
class PSelectorHasTextTask extends PSelectorTask {
6780
constructor(task) {
6881
super();
@@ -280,6 +293,7 @@ class PSelectorShadowTask extends PSelectorTask {
280293
if ( PSelectorShadowTask.openOrClosedShadowRoot !== undefined ) {
281294
return PSelectorShadowTask.openOrClosedShadowRoot;
282295
}
296+
const { chrome } = self;
283297
if ( typeof chrome === 'object' && chrome !== null ) {
284298
if ( chrome.dom instanceof Object ) {
285299
if ( typeof chrome.dom.openOrClosedShadowRoot === 'function' ) {
@@ -476,6 +490,7 @@ class PSelector {
476490
}
477491
}
478492
PSelector.prototype.operatorToTaskMap = new Map([
493+
[ 'content', PSelectorContentTask ],
479494
[ 'has', PSelectorIfTask ],
480495
[ 'has-text', PSelectorHasTextTask ],
481496
[ 'if', PSelectorIfTask ],

src/js/html-filtering.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ class PSelectorVoidTask {
6565
transpose() {
6666
}
6767
}
68+
69+
class PSelectorContentTask {
70+
constructor(task) {
71+
this.selector = task[1];
72+
}
73+
transpose(node, output) {
74+
const root = node.content;
75+
if ( typeof root?.querySelectorAll !== 'function' ) { return; }
76+
const nodes = root.querySelectorAll(this.selector);
77+
output.push(...nodes);
78+
}
79+
}
80+
6881
class PSelectorHasTextTask {
6982
constructor(task) {
7083
this.needle = regexFromString(task[1]);
@@ -238,6 +251,7 @@ class PSelector {
238251
}
239252
}
240253
PSelector.prototype.operatorToTaskMap = new Map([
254+
[ 'content', PSelectorContentTask ],
241255
[ 'has', PSelectorIfTask ],
242256
[ 'has-text', PSelectorHasTextTask ],
243257
[ 'if', PSelectorIfTask ],
@@ -329,7 +343,7 @@ htmlFilteringEngine.compile = function(parser, writer) {
329343
// Only exception filters are allowed to be global.
330344
if ( parser.hasOptions() === false ) {
331345
if ( isException ) {
332-
writer.push([ 64, '', 1, compiled ]);
346+
writer.push([ 64, '', `-${compiled}` ]);
333347
}
334348
return;
335349
}

src/js/static-filtering-parser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3312,6 +3312,7 @@ export class ExtSelectorCompiler {
33123312
':style',
33133313
]);
33143314
this.proceduralOperatorNames = new Set([
3315+
'content',
33153316
'has-text',
33163317
'if',
33173318
'if-not',
@@ -3959,6 +3960,8 @@ export class ExtSelectorCompiler {
39593960
const arg = this.astSerialize(parts, false);
39603961
if ( arg === undefined ) { return; }
39613962
switch ( operator ) {
3963+
case 'content':
3964+
return this.compileSelector(arg);
39623965
case 'has-text':
39633966
return this.compileText(arg);
39643967
case 'if':
@@ -4194,6 +4197,7 @@ export const proceduralOperatorTokens = new Map([
41944197
[ '-abp-contains', 0b00 ],
41954198
[ '-abp-has', 0b00, ],
41964199
[ 'contains', 0b00, ],
4200+
[ 'content', 0b01, ],
41974201
[ 'has', 0b01 ],
41984202
[ 'has-text', 0b01 ],
41994203
[ 'if', 0b00 ],
@@ -4207,9 +4211,10 @@ export const proceduralOperatorTokens = new Map([
42074211
[ 'not', 0b01 ],
42084212
[ 'nth-ancestor', 0b00 ],
42094213
[ 'others', 0b11 ],
4210-
[ 'remove', 0b11 ],
4214+
[ 'remove', 0b01 ],
42114215
[ 'remove-attr', 0b11 ],
42124216
[ 'remove-class', 0b11 ],
4217+
[ 'shadow', 0b11, ],
42134218
[ 'style', 0b11 ],
42144219
[ 'upward', 0b01 ],
42154220
[ 'watch-attr', 0b11 ],

0 commit comments

Comments
 (0)