Bug 635993 - Attachment reminder doesn't recognize file names with matching file-type extensions. r=bwinton
authorJason Yeo <jasonyeo88@gmail.com>
Tue, 13 Dec 2011 13:39:32 +0000
changeset 9981 d2d1125046e9fbde6f88996fab68461c6dd0b079
parent 9980 b1363ce4fd80c3c62fd4a0841d196c8153da95c6
child 9982 a2cd7e37802fc1a4b75695eef5d2e4f22655d554
push id336
push userbugzilla@standard8.plus.com
push dateTue, 31 Jan 2012 22:15:45 +0000
treeherdercomm-beta@54945f5d278d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbwinton
bugs635993
Bug 635993 - Attachment reminder doesn't recognize file names with matching file-type extensions. r=bwinton
mail/base/modules/attachmentChecker.js
mail/base/test/unit/test_attachmentChecker.js
--- a/mail/base/modules/attachmentChecker.js
+++ b/mail/base/modules/attachmentChecker.js
@@ -83,25 +83,42 @@ function GetAttachmentKeywords(mailData,
   const NOT_W = "[^\\u0041-\\u005a\\u0061-\\u007a\\u00aa\\u00b5\\u00ba\\u00c0-\\u00d6\\u00d8-\\u00f6\\u00f8-\\u01ba\\u01bc-\\u01bf\\u01c4-\\u02ad\\u0386\\u0388-\\u0481\\u048c-\\u0556\\u0561-\\u0587\\u10a0-\\u10c5\\u1e00-\\u1fbc\\u1fbe\\u1fc2-\\u1fcc\\u1fd0-\\u1fdb\\u1fe0-\\u1fec\\u1ff2-\\u1ffc\\u207f\\u2102\\u2107\\u210a-\\u2113\\u2115\\u2119-\\u211d\\u2124\\u2126\\u2128\\u212a-\\u212d\\u212f-\\u2131\\u2133\\u2134\\u2139\\ufb00-\\ufb17\\uff21-\\uff3a\\uff41-\\uff5a]";
 
   var keywordsFound = [];
   for (var i = 0; i < keywordsArray.length; i++) {
     var kw = escapeRegxpSpecials(keywordsArray[i]);
     // If the keyword starts (ends) with a CJK character, we don't care
     // what the previous (next) character is, because the words aren't
     // space delimited.
-    var start = IsCJK(kw.charCodeAt(0)) ? "" : ("(^|" + NOT_W + ")");
-    var end = IsCJK(kw.charCodeAt(kw.length - 1)) ? "" : ("(" + NOT_W + "|$)");
-    var re = new RegExp(start + kw + end, "i");
-    var matching = re.exec(mailData);
-    // Ignore the match if it was a URL.
-    if (matching && !(/^http|^ftp/i.test(matching[0])))
-      // We're not worried about matching too much because we only add the
-      // keyword to the list of found keywords.
-      keywordsFound.push(keywordsArray[i]);
+    var re;
+    var matching;
+    var isFileType = kw.charAt(1) == ".";
+    var start;
+    var end;
+    if (isFileType) {
+      start = "(([^\\s]*)\\b)";
+      end = IsCJK(kw.charCodeAt(kw.length - 1)) ? "" : "(\\s|$)";
+      re = new RegExp(start + kw + end, "ig");
+      matching = mailData.match(re);
+      if (matching) {
+        var j, len;
+        for (j = 0, len = matching.length; j < len; j++) {
+          // Ignore the match if it was a URL.
+          if (!(/^(http|ftp|https):\/\//i.test(matching[j])))
+            keywordsFound.push(matching[j].trim());
+        }
+      }
+    } else {
+      start = IsCJK(kw.charCodeAt(0)) ? "" : ("(^|" + NOT_W + ")");
+      end = IsCJK(kw.charCodeAt(kw.length - 1)) ? "" : ("(" + NOT_W + "|$)");
+      re = new RegExp(start + kw + end, "i");
+      matching = re.exec(mailData);
+      if (matching)
+        keywordsFound.push(keywordsArray[i]);
+    }
   }
   return keywordsFound;
 }
 
 onmessage = function(event)
 {
   var keywordsFound = GetAttachmentKeywords(event.data[0], event.data[1]);
   postMessage(keywordsFound);
--- a/mail/base/test/unit/test_attachmentChecker.js
+++ b/mail/base/test/unit/test_attachmentChecker.js
@@ -67,52 +67,50 @@ function utf8_decode(s)
 {
   return decodeURIComponent(escape(s));
 };
 
 /*
  * TESTS
  */
 
-/**
- * Test the GetAttachmentKeywords method with a simple word.
- */
-function test_GetAttachmentKeywords_simple()
-{
-  let mailData = utf8_decode("latte.ca");
-  let keywords = utf8_decode("latte");
-  let result = GetAttachmentKeywords(mailData, keywords);
-  assert_equal(result, "latte", "Simple keyword not equal!");
-}
-
 function test_GetAttachmentKeywords(desc, mailData, keywords, expected)
 {
   mailData = utf8_decode(mailData);
   keywords = utf8_decode(keywords);
   expected = utf8_decode(expected);
   let result = GetAttachmentKeywords(mailData, keywords);
   assert_equal(result, expected, desc + " not equal!");
 }
 
 var tests = [
-  // This is a function to demonstrate that we can put functions here.
-  test_GetAttachmentKeywords_simple,
-
   // Desc, mail body Data, keywords to search for, expected keywords found.
+  ["Simple keyword", "latte.ca", "latte", "latte"],
+  ["Extension", "testing document.pdf", ".pdf", "document.pdf"],
+  ["Two Extensions", "testing document.pdf and test.pdf", ".pdf",
+    "document.pdf,test.pdf"],
+  ["Url", "testing http://document.pdf", ".pdf", ""],
+  ["Both", "testing http://document.pdf test.pdf", ".pdf", "test.pdf"],
   ["Greek", "This is a Θεωρία test", "Θεωρία,is", "Θεωρία,is"],
   ["Greek missing", "This a Θεωρίαω test", "Θεωρία", ""],
   ["Greek and punctuation", "This a:Θεωρία-test", "Θεωρία", "Θεωρία"],
   ["Greek and Japanese", "This a 添Θεωρία付 test", "Θεωρία", "Θεωρία"],
   ["Japanese", "This is 添付! test", "Θεωρία,添付", "添付"],
   ["More Japanese", "添付mailを送る", "添付,cv", "添付"],
   ["Japanese and English", "添付mailを送る", "添付,mail", "添付,mail"],
   ["Japanese and English Mixed", "添付mailを送る", "添付mail", "添付mail"],
   ["Japanese and English Mixed missing", "添付mailing", "添付mail", ""],
   ["Japanese trailers", "This is 添添付付! test", "Θεωρία,添付", "添付"],
   ["Multi-lang", "cv添付Θεωρία", "Θεωρία,添付,cv", "Θεωρία,添付,cv"],
+  ["Should match", "I've attached the http/test.pdf file", ".pdf",
+    "http/test.pdf"],
+  ["Should still fail", "a https://test.pdf a", ".pdf", ""],
+  ["Should match Japanese", "a test.添付 a", ".添付", "test.添付"],
+  ["Should match Greek", "a test.Θεωρία a", ".Θεωρία", "test.Θεωρία"],
+  ["Should match once", "a test.pdf.doc a", ".pdf,.doc", "test.pdf.doc"],
 ];
 
 function run_test()
 {
   do_test_pending();
 
   for (var i in tests)
     if (typeof(tests[i]) == "function")