Phil Jackson <[email protected]> writes:

> This patch adds the domain to the filename created by
> `edit_field_in_external_editor'. It means one can choose a mode to edit,
> specific text from specific domains.

Slightly modified to remove the protocol and tidy up the file name a
little. Now has tests.

Cheers,
Phil
-- 
 Philip Jackson
 web:    http://www.shellarchive.co.uk
diff --git a/modules/content-buffer-input.js b/modules/content-buffer-input.js
index e3c0ff0..06a5c00 100644
--- a/modules/content-buffer-input.js
+++ b/modules/content-buffer-input.js
@@ -306,6 +306,25 @@ interactive("browser-focus-previous-form-field",
                     I.buffer, -I.p, browser_form_field_xpath_expression);
             });
 
+
+var edit_field_in_external_editor_file_ext = ".txt";
+
+function get_filename_for_current_textfield(doc, elem) {
+    var name = doc.URL
+        + "-"
+        + ( elem.getAttribute("name")
+            || elem.getAttribute("id")
+            || "textarea" );
+
+    // get rid filesystem unfriendly chars
+    name = name.replace(doc.location.protocol, "")
+        .replace(/[^a-zA-Z0-9]+/g, "-")
+        .replace(/(^-+|-+$)/g, "")
+        + edit_field_in_external_editor_file_ext;
+
+    return name;
+}
+
 function edit_field_in_external_editor(buffer, elem) {
     if (elem instanceof Ci.nsIDOMHTMLInputElement) {
         var type = elem.getAttribute("type");
@@ -316,15 +335,7 @@ function edit_field_in_external_editor(buffer, elem) {
     } else if (!(elem instanceof Ci.nsIDOMHTMLTextAreaElement))
         throw interactive_error("Element is not a text field.");
 
-    var name = elem.getAttribute("name");
-    if (!name || name.length == 0)
-        name = elem.getAttribute("id");
-    if (!name)
-        name = "";
-    name = name.replace(/[^a-zA-Z0-9\-_]/g, "");
-    if (name.length == 0)
-        name = "text";
-    name += ".txt";
+    var name = get_filename_for_current_textfield(buffer.document, elem);
     var file = get_temporary_file(name);
 
     // Write to file
@@ -349,6 +360,7 @@ function edit_field_in_external_editor(buffer, elem) {
         file.remove(false);
     }
 }
+
 interactive("edit-current-field-in-external-editor",
             "Edit the contents of the currently-focused text field in an external editor.",
             function (I) {
diff --git a/tests/simple/external-filename.js b/tests/simple/external-filename.js
new file mode 100644
index 0000000..cf0883b
--- /dev/null
+++ b/tests/simple/external-filename.js
@@ -0,0 +1,46 @@
+require('walnut.js');
+
+function mock_textfield(onlyid) {
+    this.getAttribute = function (accessor) {
+        if (! accessor || accessor != onlyid)
+            return null;
+
+        return onlyid + "!text    box%--"
+    }
+}
+
+walnut_run({
+    suite_setup: function () {
+        this.ext = edit_field_in_external_editor_file_ext;
+    },
+    suite_teardown: function () {
+        edit_field_in_external_editor_file_ext = this.ext;
+    },
+    test_elem_mock: function () {
+        var elem = new mock_textfield("name");
+        assert_equals(elem.getAttribute("name"), "name!text    box%--");
+
+        var elem = new mock_textfield("id");
+        assert_equals(elem.getAttribute("id"), "id!text    box%--");
+    },
+    test_filenames: function () {
+        var document = {
+            URL: "http://www.bbc.co.uk/";,
+            location: {
+                protocol: "http:"
+            }
+        };
+
+        var elem = new mock_textfield();
+        assert_equals(get_filename_for_current_textfield(document, elem),
+                      "www-bbc-co-uk-textarea.txt");
+
+        var elem = new mock_textfield("id");
+        assert_equals(get_filename_for_current_textfield(document, elem),
+                      "www-bbc-co-uk-id-text-box.txt");
+
+        var elem = new mock_textfield("name");
+        assert_equals(get_filename_for_current_textfield(document, elem),
+                      "www-bbc-co-uk-name-text-box.txt");
+    }
+});
_______________________________________________
Conkeror mailing list
[email protected]
https://www.mozdev.org/mailman/listinfo/conkeror

Reply via email to