From 2ab39aee235e90104450f3f8ca564fe22e21444a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sun, 31 Oct 2021 13:18:31 -0400 Subject: [PATCH] Fix not highlighting cases of invalid syntax Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1791 The following case of invalid syntax was not reported as invalid by the syntax highlighter: ... example.com image ... With dynamic filtering, there can't be a specific hostname when a specific type is used, or a specific type when a specific hostname is used, one or the other must be `*`. --- src/js/codemirror/ubo-dynamic-filtering.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/js/codemirror/ubo-dynamic-filtering.js b/src/js/codemirror/ubo-dynamic-filtering.js index 4e670d5f7..8ce760c8d 100644 --- a/src/js/codemirror/ubo-dynamic-filtering.js +++ b/src/js/codemirror/ubo-dynamic-filtering.js @@ -123,6 +123,7 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { const { sortType } = opts; const reNotToken = /^\s+/; const reToken = /^\S+/; + const tokens = []; // leading whitespaces let match = reNotToken.exec(string); if ( match !== null ) { @@ -131,6 +132,7 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { // first token match = reToken.exec(string); if ( match === null ) { return; } + tokens.push(match[0]); // hostname or switch const isSwitchRule = validSwitches.has(match[0]); if ( isSwitchRule ) { @@ -151,6 +153,7 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { // second token match = reToken.exec(string); if ( match === null ) { return; } + tokens.push(match[0]); // hostname or url const isURLRule = isSwitchRule === false && match[0].indexOf('://') > 0; if ( isURLRule ) { @@ -169,6 +172,7 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { // third token match = reToken.exec(string); if ( match === null ) { return; } + tokens.push(match[0]); // rule type or switch state if ( isSwitchRule ) { string = validSwitcheStates.has(match[0]) @@ -178,10 +182,14 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { string = invalidURLRuleTypes.has(match[0]) ? addMatchSlice(match, 'error') : addMatchSlice(match); - } else { + } else if ( tokens[1] === '*' ) { string = validHnRuleTypes.has(match[0]) ? addMatchSlice(match) : addMatchSlice(match, 'error'); + } else { + string = match[0] === '*' + ? addMatchSlice(match) + : addMatchSlice(match, 'error'); } // whitespaces before fourth token match = reNotToken.exec(string); @@ -190,6 +198,7 @@ CodeMirror.defineMode('ubo-dynamic-filtering', ( ) => { // fourth token match = reToken.exec(string); if ( match === null ) { return; } + tokens.push(match[0]); string = isSwitchRule || validActions.has(match[0]) === false ? addMatchSlice(match, 'error') : addMatchSlice(match, `${match[0]}rule`);