Skip to content

Commit 8d136ec

Browse files
committed
Ignore pseudo-elements when querying selectors in element picker
Related issue: - #2515
1 parent aaee898 commit 8d136ec

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/js/scriptlets/element-picker.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ const filtersFrom = function(x, y) {
698698
const filterToDOMInterface = (( ) => {
699699
const reHnAnchorPrefix = '^[\\w-]+://(?:[^/?#]+\\.)?';
700700
const reCaret = '(?:[^%.0-9a-z_-]|$)';
701+
const rePseudoElements = /::?(?:after|before)$/;
701702

702703
// Net filters: we need to lookup manually -- translating into a foolproof
703704
// CSS selector is just not possible.
@@ -795,11 +796,16 @@ const filterToDOMInterface = (( ) => {
795796
// https://github.com/uBlockOrigin/uBlock-issues/issues/389
796797
// Test filter using comma-separated list to better detect invalid CSS
797798
// selectors.
799+
//
800+
// https://github.com/gorhill/uBlock/issues/2515
801+
// Remove trailing pseudo-element when querying.
798802
const fromPlainCosmeticFilter = function(raw) {
799803
let elems;
800804
try {
801805
document.documentElement.matches(`${raw},\na`);
802-
elems = document.querySelectorAll(raw);
806+
elems = document.querySelectorAll(
807+
raw.replace(rePseudoElements, '')
808+
);
803809
}
804810
catch (e) {
805811
return;
@@ -814,21 +820,25 @@ const filterToDOMInterface = (( ) => {
814820

815821
// https://github.com/gorhill/uBlock/issues/1772
816822
// Handle procedural cosmetic filters.
823+
//
824+
// https://github.com/gorhill/uBlock/issues/2515
825+
// Remove trailing pseudo-element when querying.
817826
const fromCompiledCosmeticFilter = function(raw) {
818827
if ( typeof raw !== 'string' ) { return; }
819-
let o;
828+
let elems;
820829
try {
821-
o = JSON.parse(raw);
830+
const o = JSON.parse(raw);
831+
if ( o.style ) {
832+
elems = document.querySelectorAll(
833+
o.style[0].replace(rePseudoElements, '')
834+
);
835+
lastAction = o.style[0] + ' {' + o.style[1] + '}';
836+
} else if ( o.tasks ) {
837+
elems = vAPI.domFilterer.createProceduralFilter(o).exec();
838+
}
822839
} catch(ex) {
823840
return;
824841
}
825-
let elems;
826-
if ( o.style ) {
827-
elems = document.querySelectorAll(o.style[0]);
828-
lastAction = o.style[0] + ' {' + o.style[1] + '}';
829-
} else if ( o.tasks ) {
830-
elems = vAPI.domFilterer.createProceduralFilter(o).exec();
831-
}
832842
if ( !elems ) { return; }
833843
const out = [];
834844
for ( const elem of elems ) {

0 commit comments

Comments
 (0)