Просмотр исходного кода

fix: case-sensitivity adjustments for url matching

* @include/exclude: case-insensitive like GM4/TM
* @match/exclude-match: only host part is case-insensitive
tophf 5 лет назад
Родитель
Сommit
1a684ffec4
2 измененных файлов с 66 добавлено и 5 удалено
  1. 11 5
      src/background/utils/tester.js
  2. 55 0
      test/background/tester.test.js

+ 11 - 5
src/background/utils/tester.js

@@ -94,9 +94,11 @@ function bindRE(re) {
 
 function autoReg(str) {
   if (str.length > 1 && str[0] === '/' && str[str.length - 1] === '/') {
-    const re = new RegExp(str.slice(1, -1));
+    const re = new RegExp(str.slice(1, -1), 'i');
     return { test: bindRE(re) };
   }
+  // glob mode: case-insensitive to match GM4 & Tampermonkey bugged behavior
+  str = str.toLowerCase();
   const reStr = str2RE(str);
   if (tld.isReady() && str.includes('.tld/')) {
     const reTldStr = reStr.replace('\\.tld/', '((?:\\.\\w+)+)/');
@@ -104,14 +106,14 @@ function autoReg(str) {
       test: (tstr) => {
         const matches = tstr.match(reTldStr);
         if (matches) {
-          const suffix = matches[1].slice(1);
+          const suffix = matches[1].slice(1).toLowerCase();
           if (tld.getPublicSuffix(suffix) === suffix) return true;
         }
         return false;
       },
     };
   }
-  const re = new RegExp(`^${reStr}$`); // String with wildcards
+  const re = new RegExp(`^${reStr}$`, 'i'); // String with wildcards
   return { test: bindRE(re) };
 }
 
@@ -144,17 +146,21 @@ function hostMatcher(rule) {
     base = base.slice(0, -4);
     suffix = RE_STR_TLD;
   }
-  const re = new RegExp(`^${prefix}${str2RE(base)}${suffix}$`);
+  const re = new RegExp(`^${prefix}${str2RE(base)}${suffix}$`, 'i');
+  let ruleLC;
   return (data) => {
     // * matches all
     if (rule === '*') return 1;
     // exact match
     if (rule === data) return 1;
+    // host matching is case-insensitive
+    if (!ruleLC) ruleLC = rule.toLowerCase();
+    if (ruleLC === data.toLowerCase()) return 1;
     const matches = data.match(re);
     if (matches) {
       const [, tldStr] = matches;
       if (!tldStr) return 1;
-      const tldSuffix = tldStr.slice(1);
+      const tldSuffix = tldStr.slice(1).toLowerCase();
       return tld.getPublicSuffix(tldSuffix) === tldSuffix;
     }
     return 0;

+ 55 - 0
test/background/tester.test.js

@@ -114,6 +114,18 @@ test('host', (t) => {
     q.end();
   });
 
+  t.test('should ignore case', (q) => {
+    const script = buildScript({
+      meta: {
+        match: [
+          '*://GOOGLE.com/',
+        ],
+      },
+    });
+    q.ok(testScript('https://google.COM/', script), 'should ignore case');
+    q.end();
+  });
+
   t.end();
 });
 
@@ -179,6 +191,22 @@ test('path', (t) => {
     q.end();
   });
 
+  t.test('should be case-sensitive', (q) => {
+    const script = buildScript({
+      meta: {
+        match: [
+          'https://www.google.com/a?Query',
+          'https://www.google.com/b#Hash',
+        ],
+      },
+    });
+    q.ok(testScript('https://www.google.com/a?Query', script), 'query should be case-sensitive');
+    q.notOk(testScript('https://www.google.com/a?query', script), 'query should be case-sensitive');
+    q.ok(testScript('https://www.google.com/b#Hash', script), 'hash should be case-sensitive');
+    q.notOk(testScript('https://www.google.com/b#hash', script), 'hash should be case-sensitive');
+    q.end();
+  });
+
   t.end();
 });
 
@@ -224,6 +252,20 @@ test('include', (t) => {
     q.notOk(testScript('https://www.google.example.com/', script), 'should not match subdomains');
     q.end();
   });
+
+  t.test('should ignore case', (q) => {
+    const script = buildScript({
+      meta: {
+        include: [
+          'https://www.google.*',
+          '/regexp/',
+        ],
+      },
+    });
+    q.ok(testScript('https://www.GOOGLE.com/', script), 'should ignore case');
+    q.ok(testScript('https://www.REGEXP.com/', script), 'should ignore case');
+    q.end();
+  });
 });
 
 test('exclude', (t) => {
@@ -308,6 +350,19 @@ test('exclude-match', (t) => {
     q.ok(testScript('https://www.hello.com/', script), 'not exclude by prefix');
     q.end();
   });
+
+  t.test('should ignore case only in host', (q) => {
+    const script = buildScript({
+      meta: {
+        match: [
+          '*://GOOGLE.com/FOO?BAR#HASH',
+        ],
+      },
+    });
+    q.ok(testScript('https://google.COM/FOO?BAR#HASH', script), 'should ignore case in host');
+    q.notOk(testScript('https://google.com/foo?bar#hash', script), 'should ignore case in host only');
+    q.end();
+  });
 });
 
 test('custom', (t) => {