Ignore pseudo-elements when querying selectors in element picker

Related issue:
- https://github.com/gorhill/uBlock/issues/2515
This commit is contained in:
Raymond Hill 2019-07-26 09:24:34 -04:00
parent aaee898d95
commit 8d136ec2d5
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 20 additions and 10 deletions

View File

@ -698,6 +698,7 @@ const filtersFrom = function(x, y) {
const filterToDOMInterface = (( ) => { const filterToDOMInterface = (( ) => {
const reHnAnchorPrefix = '^[\\w-]+://(?:[^/?#]+\\.)?'; const reHnAnchorPrefix = '^[\\w-]+://(?:[^/?#]+\\.)?';
const reCaret = '(?:[^%.0-9a-z_-]|$)'; const reCaret = '(?:[^%.0-9a-z_-]|$)';
const rePseudoElements = /::?(?:after|before)$/;
// Net filters: we need to lookup manually -- translating into a foolproof // Net filters: we need to lookup manually -- translating into a foolproof
// CSS selector is just not possible. // CSS selector is just not possible.
@ -795,11 +796,16 @@ const filterToDOMInterface = (( ) => {
// https://github.com/uBlockOrigin/uBlock-issues/issues/389 // https://github.com/uBlockOrigin/uBlock-issues/issues/389
// Test filter using comma-separated list to better detect invalid CSS // Test filter using comma-separated list to better detect invalid CSS
// selectors. // selectors.
//
// https://github.com/gorhill/uBlock/issues/2515
// Remove trailing pseudo-element when querying.
const fromPlainCosmeticFilter = function(raw) { const fromPlainCosmeticFilter = function(raw) {
let elems; let elems;
try { try {
document.documentElement.matches(`${raw},\na`); document.documentElement.matches(`${raw},\na`);
elems = document.querySelectorAll(raw); elems = document.querySelectorAll(
raw.replace(rePseudoElements, '')
);
} }
catch (e) { catch (e) {
return; return;
@ -814,21 +820,25 @@ const filterToDOMInterface = (( ) => {
// https://github.com/gorhill/uBlock/issues/1772 // https://github.com/gorhill/uBlock/issues/1772
// Handle procedural cosmetic filters. // Handle procedural cosmetic filters.
//
// https://github.com/gorhill/uBlock/issues/2515
// Remove trailing pseudo-element when querying.
const fromCompiledCosmeticFilter = function(raw) { const fromCompiledCosmeticFilter = function(raw) {
if ( typeof raw !== 'string' ) { return; } if ( typeof raw !== 'string' ) { return; }
let o;
try {
o = JSON.parse(raw);
} catch(ex) {
return;
}
let elems; let elems;
try {
const o = JSON.parse(raw);
if ( o.style ) { if ( o.style ) {
elems = document.querySelectorAll(o.style[0]); elems = document.querySelectorAll(
o.style[0].replace(rePseudoElements, '')
);
lastAction = o.style[0] + ' {' + o.style[1] + '}'; lastAction = o.style[0] + ' {' + o.style[1] + '}';
} else if ( o.tasks ) { } else if ( o.tasks ) {
elems = vAPI.domFilterer.createProceduralFilter(o).exec(); elems = vAPI.domFilterer.createProceduralFilter(o).exec();
} }
} catch(ex) {
return;
}
if ( !elems ) { return; } if ( !elems ) { return; }
const out = []; const out = [];
for ( const elem of elems ) { for ( const elem of elems ) {