[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor-browser] 05/179: Bug 1772906, switch to use validateFileNameForSaving to verify the filename when opening view source in an external editor, r=mak a=pascalc



This is an automated email from the git hooks/post-receive script.

pierov pushed a commit to branch tor-browser-102.2.0esr-12.0-1
in repository tor-browser.

commit fbeb3dab5e52740641c4a7b6d1eadad67dcf7833
Author: Neil Deakin <neil@xxxxxxxxxxx>
AuthorDate: Wed Jun 15 14:33:28 2022 +0000

    Bug 1772906, switch to use validateFileNameForSaving to verify the filename when opening view source in an external editor, r=mak  a=pascalc
    
    Differential Revision: https://phabricator.services.mozilla.com/D148535
---
 .../viewsource/content/viewSourceUtils.js          | 19 +++---
 .../components/viewsource/test/browser/browser.ini |  1 +
 .../test/browser/browser_validatefilename.js       | 68 ++++++++++++++++++++++
 3 files changed, 79 insertions(+), 9 deletions(-)

diff --git a/toolkit/components/viewsource/content/viewSourceUtils.js b/toolkit/components/viewsource/content/viewSourceUtils.js
index 093a1b7381fe4..ce77555c28fb3 100644
--- a/toolkit/components/viewsource/content/viewSourceUtils.js
+++ b/toolkit/components/viewsource/content/viewSourceUtils.js
@@ -8,8 +8,7 @@
  * To keep the global namespace safe, don't define global variables and
  * functions in this file.
  *
- * This file silently depends on contentAreaUtils.js for
- * getDefaultFileName, getNormalizedLeafName and getDefaultExtension
+ * This file silently depends on contentAreaUtils.js for getDefaultFileName
  */
 
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
@@ -403,20 +402,22 @@ var gViewSourceUtils = {
       );
     }
 
-    var tempFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
     var fileName = this._caUtils.getDefaultFileName(
       null,
       aURI,
       aDocument,
-      aContentType
+      null
     );
-    var extension = this._caUtils.getDefaultExtension(
+
+    const mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
+    fileName = mimeService.validateFileNameForSaving(
       fileName,
-      aURI,
-      aContentType
+      aContentType,
+      mimeService.VALIDATE_DEFAULT
     );
-    var leafName = this._caUtils.getNormalizedLeafName(fileName, extension);
-    tempFile.append(leafName);
+
+    var tempFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
+    tempFile.append(fileName);
     return tempFile;
   },
 };
diff --git a/toolkit/components/viewsource/test/browser/browser.ini b/toolkit/components/viewsource/test/browser/browser.ini
index c22b0dc63cbfb..4ac477c7c1d3d 100644
--- a/toolkit/components/viewsource/test/browser/browser.ini
+++ b/toolkit/components/viewsource/test/browser/browser.ini
@@ -17,5 +17,6 @@ skip-if =
 skip-if =
   fission && os == "mac" && !debug  # Bug 1713913 - new Fission platform triage
 [browser_srcdoc.js]
+[browser_validatefilename.js]
 [browser_viewsourceprefs.js]
 skip-if = socketprocess_networking && os == "linux" && !debug
diff --git a/toolkit/components/viewsource/test/browser/browser_validatefilename.js b/toolkit/components/viewsource/test/browser/browser_validatefilename.js
new file mode 100644
index 0000000000000..cd99f1c57eced
--- /dev/null
+++ b/toolkit/components/viewsource/test/browser/browser_validatefilename.js
@@ -0,0 +1,68 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+add_task(async function() {
+  let tests = [
+    {
+      uri: "data:text/html,Test",
+      basename: "index",
+    },
+    {
+      uri: "data:text/html,<title>Hello There</title>Test",
+      basename: "Hello There",
+    },
+  ];
+
+  for (let test of tests) {
+    await BrowserTestUtils.withNewTab(test.uri, async browser => {
+      let doc = {
+        characterSet: browser.characterSet,
+        contentType: browser.documentContentType,
+        title: browser.contentTitle,
+      };
+
+      let fl = gViewSourceUtils.getTemporaryFile(
+        browser.currentURI,
+        doc,
+        "text/html"
+      );
+      // Some versions of Windows will crop the extension to three characters so allow both forms.
+      ok(
+        fl.leafName == test.basename + ".htm" ||
+          fl.leafName == test.basename + ".html",
+        "filename title for " + test.basename + " html"
+      );
+
+      doc.contentType = "application/xhtml+xml";
+      fl = gViewSourceUtils.getTemporaryFile(
+        browser.currentURI,
+        doc,
+        "application/xhtml+xml"
+      );
+      ok(
+        fl.leafName == test.basename + ".xht" ||
+          fl.leafName == test.basename + ".xhtml",
+        "filename title for " + test.basename + " xhtml"
+      );
+    });
+  }
+
+  let fl = gViewSourceUtils.getTemporaryFile(
+    Services.io.newURI("http://www.example.com/simple";),
+    null,
+    "text/html"
+  );
+  ok(
+    fl.leafName == "simple.htm" || fl.leafName == "simple.html",
+    "filename title for simple"
+  );
+
+  fl = gViewSourceUtils.getTemporaryFile(
+    Services.io.newURI("http://www.example.com/samplefile.txt";),
+    null,
+    "text/html"
+  );
+  is(fl.leafName, "samplefile.txt", "filename title for samplefile");
+});

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits