1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-04 01:59:38 +02: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';
}
if ( parser.category === parser.CATStaticExtFilter ) {
const style = colorExtSpan(stream);
return style ? `ext ${style}` : 'ext';
const style = colorExtSpan(stream) || '';
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 ) {
const style = colorNetSpan(stream);
return style ? `net ${style}` : 'net';
return style ? `line-cm-net ${style}` : 'line-cm-net';
}
stream.skipToEnd();
return null;
@ -679,25 +687,10 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
// Enhanced word selection
{
const Pass = CodeMirror.Pass;
const selectWordAt = function(cm, 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 token = cm.getTokenTypeAt(pos);
const { type: token } = cm.getTokenAt(pos);
let beg, end;
// Select URL in comments
@ -712,30 +705,34 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
}
}
// Better word selection for cosmetic filters
else if ( /\bext\b/.test(token) ) {
if ( /\bvalue\b/.test(token) ) {
const l = /[^,.]*$/i.exec(s.slice(0, ch));
const r = /^[^#,]*/i.exec(s.slice(ch));
if ( l && r ) {
beg = l.index;
end = ch + r[0].length;
}
} else if ( /\bvariable\b/.test(token) ) {
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;
end = ch + r[0].length;
if ( /\bdef\b/.test(cm.getTokenTypeAt({ line, ch: beg + 1 })) ) {
beg += 1;
}
// Better word selection for extended filters: prefix
else if (
/\bline-cm-ext-(?:dom|html|js)\b/.test(token) &&
/\bvalue\b/.test(token)
) {
const l = /[^,.]*$/i.exec(s.slice(0, ch));
const r = /^[^#,]*/i.exec(s.slice(ch));
if ( l && r ) {
beg = l.index;
end = ch + r[0].length;
}
}
// Better word selection for cosmetic and HTML filters: suffix
else if ( /\bline-cm-ext-(?:dom|html)\b/.test(token) ) {
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;
end = ch + r[0].length;
if ( /\bdef\b/.test(cm.getTokenTypeAt({ line, ch: beg + 1 })) ) {
beg += 1;
}
}
}
// 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) ) {
const l = /[^ ,.=|]*$/i.exec(s.slice(0, ch));
const r = /^[^ #,|]*/i.exec(s.slice(ch));
@ -753,16 +750,22 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
}
}
if ( beg === undefined ) { return Pass; }
cm.setSelection(
{ line, ch: beg },
{ line, ch: end }
);
if ( beg === undefined ) {
const { anchor, head } = cm.findWordAt(pos);
return { from: anchor, to: head };
}
return {
from: { line, ch: beg },
to: { line, ch: end },
};
};
CodeMirror.defineInitHook(cm => {
cm.addKeyMap({
'LeftDoubleClick': selectWordAt,
cm.setOption('configureMouse', function(cm, repeat) {
return {
unit: repeat === 'double' ? selectWordAt : null,
};
});
});
}

View File

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