1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-07 11:22:38 +01:00

Fix word-based selection in filter list editor/viewer

This commit fixes mouse double-click-and-drag operations,
which was broken due to the implementation of a custom
word selection in the filter list editor/viewer.
This commit is contained in:
Raymond Hill 2020-12-27 09:32:50 -05:00
parent f80371e844
commit 6d3ad553b4
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 49 additions and 46 deletions

View File

@ -336,12 +336,20 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
return 'comment'; return 'comment';
} }
if ( parser.category === parser.CATStaticExtFilter ) { if ( parser.category === parser.CATStaticExtFilter ) {
const style = colorExtSpan(stream); const style = colorExtSpan(stream) || '';
return style ? `ext ${style}` : 'ext'; let flavor = '';
if ( (parser.flavorBits & parser.BITFlavorExtCosmetic) !== 0 ) {
flavor = 'line-cm-ext-dom';
} else if ( (parser.flavorBits & parser.BITFlavorExtScriptlet) !== 0 ) {
flavor = 'line-cm-ext-js';
} else if ( (parser.flavorBits & parser.BITFlavorExtHTML) !== 0 ) {
flavor = 'line-cm-ext-html';
}
return `${flavor} ${style}`.trim();
} }
if ( parser.category === parser.CATStaticNetFilter ) { if ( parser.category === parser.CATStaticNetFilter ) {
const style = colorNetSpan(stream); const style = colorNetSpan(stream);
return style ? `net ${style}` : 'net'; return style ? `line-cm-net ${style}` : 'line-cm-net';
} }
stream.skipToEnd(); stream.skipToEnd();
return null; return null;
@ -679,25 +687,10 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
// Enhanced word selection // Enhanced word selection
{ {
const Pass = CodeMirror.Pass;
const selectWordAt = function(cm, pos) { const selectWordAt = function(cm, pos) {
const { line, ch } = pos; const { line, ch } = pos;
// Leave current selection alone
if ( cm.somethingSelected() ) {
const from = cm.getCursor('from');
const to = cm.getCursor('to');
if (
(line > from.line || line === from.line && ch > from.ch) &&
(line < to.line || line === to.line && ch < to.ch)
) {
return Pass;
}
}
const s = cm.getLine(line); const s = cm.getLine(line);
const token = cm.getTokenTypeAt(pos); const { type: token } = cm.getTokenAt(pos);
let beg, end; let beg, end;
// Select URL in comments // Select URL in comments
@ -712,30 +705,34 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
} }
} }
// Better word selection for cosmetic filters // Better word selection for extended filters: prefix
else if ( /\bext\b/.test(token) ) { else if (
if ( /\bvalue\b/.test(token) ) { /\bline-cm-ext-(?:dom|html|js)\b/.test(token) &&
const l = /[^,.]*$/i.exec(s.slice(0, ch)); /\bvalue\b/.test(token)
const r = /^[^#,]*/i.exec(s.slice(ch)); ) {
if ( l && r ) { const l = /[^,.]*$/i.exec(s.slice(0, ch));
beg = l.index; const r = /^[^#,]*/i.exec(s.slice(ch));
end = ch + r[0].length; if ( l && r ) {
} beg = l.index;
} else if ( /\bvariable\b/.test(token) ) { end = ch + r[0].length;
const l = /[#.][a-z0-9_-]+$/i.exec(s.slice(0, ch)); }
const r = /^[a-z0-9_-]+/i.exec(s.slice(ch)); }
if ( l && r ) {
beg = l.index; // Better word selection for cosmetic and HTML filters: suffix
end = ch + r[0].length; else if ( /\bline-cm-ext-(?:dom|html)\b/.test(token) ) {
if ( /\bdef\b/.test(cm.getTokenTypeAt({ line, ch: beg + 1 })) ) { const l = /[#.]?[a-z0-9_-]+$/i.exec(s.slice(0, ch));
beg += 1; const r = /^[a-z0-9_-]+/i.exec(s.slice(ch));
} if ( l && r ) {
beg = l.index;
end = ch + r[0].length;
if ( /\bdef\b/.test(cm.getTokenTypeAt({ line, ch: beg + 1 })) ) {
beg += 1;
} }
} }
} }
// Better word selection for network filters // Better word selection for network filters
else if ( /\bnet\b/.test(token) ) { else if ( /\bline-cm-net\b/.test(token) ) {
if ( /\bvalue\b/.test(token) ) { if ( /\bvalue\b/.test(token) ) {
const l = /[^ ,.=|]*$/i.exec(s.slice(0, ch)); const l = /[^ ,.=|]*$/i.exec(s.slice(0, ch));
const r = /^[^ #,|]*/i.exec(s.slice(ch)); const r = /^[^ #,|]*/i.exec(s.slice(ch));
@ -753,16 +750,22 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
} }
} }
if ( beg === undefined ) { return Pass; } if ( beg === undefined ) {
cm.setSelection( const { anchor, head } = cm.findWordAt(pos);
{ line, ch: beg }, return { from: anchor, to: head };
{ line, ch: end } }
);
return {
from: { line, ch: beg },
to: { line, ch: end },
};
}; };
CodeMirror.defineInitHook(cm => { CodeMirror.defineInitHook(cm => {
cm.addKeyMap({ cm.setOption('configureMouse', function(cm, repeat) {
'LeftDoubleClick': selectWordAt, return {
unit: repeat === 'double' ? selectWordAt : null,
};
}); });
}); });
} }

View File

@ -2782,7 +2782,7 @@ Parser.tokenizableStrFromRegex = (( ) => {
} }
return s; return s;
} }
case 2: /* T_ALTERNATION,'Alternation' */ case 2: /* T_ALTERNATION, 'Alternation' */
case 8: /* T_CHARGROUP, 'CharacterGroup' */ { case 8: /* T_CHARGROUP, 'CharacterGroup' */ {
let firstChar = 0; let firstChar = 0;
let lastChar = 0; let lastChar = 0;