[Pkg-mozext-commits] [itsalltext] 260/459: * fixed bug where the file was deleted too early; on page load instead of on edit. * added option to recover previous files. * added test cases

David Prévot taffit at moszumanska.debian.org
Tue Feb 24 23:26:28 UTC 2015


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

taffit pushed a commit to branch master
in repository itsalltext.

commit 967386313a0d67d4ce5f98df58baa2a5345804fe
Author: docwhat at gerf.org <docwhat at gerf.org>
Date:   Fri Jun 22 10:09:31 2007 -0400

    * fixed bug where the file was deleted too early; on page load instead
    of on edit.
    * added option to recover previous files.
    * added test cases
---
 src/chrome/content/cacheobj.js                | 91 ++++++++++++++++-----------
 src/chrome/content/itsalltext.js              | 57 ++++++++++-------
 src/chrome/content/itsalltext.xul             |  4 +-
 src/chrome/locale/en-US/itsalltext.properties |  2 +
 tests/index.html                              |  1 +
 tests/revisit.html                            | 45 +++++++++++++
 6 files changed, 141 insertions(+), 59 deletions(-)

diff --git a/src/chrome/content/cacheobj.js b/src/chrome/content/cacheobj.js
index 87de5c3..8f6563b 100644
--- a/src/chrome/content/cacheobj.js
+++ b/src/chrome/content/cacheobj.js
@@ -76,6 +76,11 @@ function CacheObj(node) {
      */
     that.file = null;
 
+    /* The number of edits done on this object.
+     * @type number
+     */
+    that.edit_count = 0;
+
     /* Set the default extension and create the nsIFile object. */
     var extension = node.getAttribute('itsalltext-extension');
     if (typeof(extension) != 'string' || !extension.match(/^[.a-z0-9]+$/i)) {
@@ -134,7 +139,7 @@ CacheObj.prototype.destroy = function() {
  * @param {String} ext The extension.  Must include the dot.  Example: .txt
  */
 CacheObj.prototype.setExtension = function(ext) {
-    if (ext == this.extension) {
+    if (ext == this.extension && this.file) {
         return; /* It's already set.  No problem. */
     }
 
@@ -145,6 +150,10 @@ CacheObj.prototype.setExtension = function(ext) {
 
     this.extension = ext;
     this.file = file;
+    if (file.exists()) {
+        this.timestamp = file.lastModifiedTime;
+        this.size      = file.fileSize;
+    }
 };
 
 /**
@@ -166,6 +175,7 @@ CacheObj.prototype.initFromExistingFile = function() {
             // startswith
             if (ext === null && !entry.leafName.match(tmpfiles)) {
                 ext = entry.leafName.slice(base.length);
+                continue;
             }
             try{
                 entry.remove(false);
@@ -217,35 +227,43 @@ CacheObj.prototype.toString = function() {
 
 /**
  * Write out the contents of the node.
+ * 
+ * @param {boolean} clobber Should an existing file be clobbered?
  */
-CacheObj.prototype.write = function() {
-    var foStream = Components.
-        classes["@mozilla.org/network/file-output-stream;1"].
-        createInstance(Components.interfaces.nsIFileOutputStream);
-             
-    /* write, create, truncate */
-    foStream.init(this.file, 0x02 | 0x08 | 0x20, 
-                  parseInt('0600',8), 0); 
-             
-    /* We convert to charset */
-    var conv = Components.
-        classes["@mozilla.org/intl/scriptableunicodeconverter"].
-        createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
-    conv.charset = ItsAllText.getCharset();
-             
-    var text = conv.ConvertFromUnicode(this.node.value);
-    foStream.write(text, text.length);
-    foStream.close();
-             
-    /* Reset Timestamp and filesize, to prevent a spurious refresh */
-    this.timestamp = this.file.lastModifiedTime;
-    this.size      = this.file.fileSize;
+CacheObj.prototype.write = function(clobber) {
+    clobber = typeof(clobber) === 'boolean'?clobber:true;
+    var foStream, conv, text;
+
+    if (clobber) {
+        foStream = Components.
+            classes["@mozilla.org/network/file-output-stream;1"].
+            createInstance(Components.interfaces.nsIFileOutputStream);
+        
+        /* write, create, truncate */
+        foStream.init(this.file, 0x02 | 0x08 | 0x20, 
+                      parseInt('0600',8), 0); 
+        
+        /* We convert to charset */
+        conv = Components.
+            classes["@mozilla.org/intl/scriptableunicodeconverter"].
+            createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
+        conv.charset = ItsAllText.getCharset();
+        
+        text = conv.ConvertFromUnicode(this.node.value);
+        foStream.write(text, text.length);
+        foStream.close();
 
+        /* Reset Timestamp and filesize, to prevent a spurious refresh */
+        this.timestamp = this.file.lastModifiedTime;
+        this.size      = this.file.fileSize;
+    } else {
+        this.timestamp = this.size = null; // force refresh of textarea
+    }
+    
     /* Register the file to be deleted on app exit. */
     Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"].
         getService(Components.interfaces.nsPIExternalAppLauncher).
         deleteTemporaryFileOnExit(this.file);
-             
     return this.file.path;
 };
 
@@ -267,15 +285,19 @@ CacheObj.prototype.getStyle = function(node, attr) {
 /**
  * Edit a textarea as a file.
  * @param {String} extension The extension of the file to edit.
+ * @param {boolean} clobber Should an existing file be clobbered?
  */
-CacheObj.prototype.edit = function(extension) {
-    if (typeof(extension) == 'string') {
-        this.setExtension(extension);
-    }
-    var filename = this.write();
+CacheObj.prototype.edit = function(extension, clobber) {
+    ItsAllText.debug('edit(',extension,', ',clobber,')');
+    extension = typeof(extension) === 'string'?extension:this.extension;
+    this.setExtension(extension);
+
+    var filename = this.write(clobber);
     this.initial_background = this.node.style.backgroundColor;
     this.initial_color      = this.node.style.color;
     var program = null; 
+    const procutil = Components.classes["@mozilla.org/process/util;1"];
+
     var process;
     var args, result, ec, e, params;
              
@@ -290,9 +312,7 @@ CacheObj.prototype.edit = function(extension) {
             throw {name:"NS_ERROR_FILE_ACCESS_DENIED"}; }
 
         // create an nsIProcess
-        process = Components.
-            classes["@mozilla.org/process/util;1"].
-            createInstance(Components.interfaces.nsIProcess);
+        process = procutil.createInstance(Components.interfaces.nsIProcess);
         process.init(program);
              
         // Run the process.
@@ -304,6 +324,7 @@ CacheObj.prototype.edit = function(extension) {
         result = {};
         ec = process.run(false, args, args.length, result);
         this.private_is_watching = true;
+        this.edit_count++;
     } catch(e) {        
         params = {out:null,
                       exists: program ? program.exists() : false,
@@ -345,14 +366,12 @@ CacheObj.prototype.read = function() {
     var fis, istream, str, e;
          
     try {
-        fis = Components.
-            classes["@mozilla.org/network/file-input-stream;1"].
+        fis = Components.classes["@mozilla.org/network/file-input-stream;1"].
             createInstance(Components.interfaces.nsIFileInputStream);
         fis.init(this.file, 0x01, parseInt('00400',8), 0); 
         // MODE_RDONLY | PERM_IRUSR
              
-        istream = Components.
-            classes["@mozilla.org/intl/converter-input-stream;1"].
+        istream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].
             createInstance(Components.interfaces.nsIConverterInputStream);
         istream.init(fis, ItsAllText.getCharset(), 4096, DEFAULT_REPLACEMENT_CHARACTER);
              
diff --git a/src/chrome/content/itsalltext.js b/src/chrome/content/itsalltext.js
index 0609bb7..8fa31aa 100644
--- a/src/chrome/content/itsalltext.js
+++ b/src/chrome/content/itsalltext.js
@@ -112,12 +112,11 @@ var ItsAllText = function() {
     that.log = function() {
         var message = that.logString.apply(that, arguments);
         var e;
-        const consoleService = Components.
-            classes["@mozilla.org/consoleservice;1"].
-            getService(Components.interfaces.nsIConsoleService);
+        const consoleService = Components.classes["@mozilla.org/consoleservice;1"];
+        var obj = consoleService.getService(Components.interfaces.nsIConsoleService);
         try {
             // idiom: Convert arguments to an array for easy handling.
-            consoleService.logStringMessage(message);
+            obj.logStringMessage(message);
         } catch(e) {
             Components.utils.reportError(message);
         }
@@ -140,7 +139,7 @@ var ItsAllText = function() {
      * @param {Object} message One or more objects can be passed in to display.
      */
     that.debug = function() {
-        if (that.preferences.debug) {
+        if (that.preferences && that.preferences.debug) {
             try { Firebug.Console.logFormatted(arguments); } 
             catch(e) {
                 that.log.apply(that,arguments);
@@ -154,7 +153,7 @@ var ItsAllText = function() {
      * @returns {nsILocalFile}
      */
     that.factoryFile = function(path) {
-        const file = Components.
+        var file = Components.
             classes["@mozilla.org/file/local;1"].
             createInstance(Components.interfaces.nsILocalFile);
         if (typeof(path) == 'string' && path !== '') {
@@ -169,7 +168,7 @@ var ItsAllText = function() {
      */
     that.getEditDir = function() {
         /* Where is the directory that we use. */
-        const fobj = Components.classes["@mozilla.org/file/directory_service;1"].
+        var fobj = Components.classes["@mozilla.org/file/directory_service;1"].
             getService(Components.interfaces.nsIProperties).
             get("ProfD", Components.interfaces.nsIFile);
         fobj.append(that.MYSTRING);
@@ -188,7 +187,7 @@ var ItsAllText = function() {
 
     var loadthings = function() {
         /* Load the various bits needed to make this work. */
-        const loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
+        var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
         loader.loadSubScript('chrome://itsalltext/content/Color.js', that);
         loader.loadSubScript('chrome://itsalltext/content/cacheobj.js', that);
     };
@@ -245,7 +244,7 @@ var ItsAllText = function() {
          * Register the observer.
          */
         register: function() {
-            const prefService = Components.
+            var prefService = Components.
                 classes["@mozilla.org/preferences-service;1"].
                 getService(Components.interfaces.nsIPrefService);
             this.private_branch = prefService.getBranch("extensions."+that.MYSTRING+".");
@@ -391,7 +390,8 @@ var ItsAllText = function() {
         var instantApply = getBoolPref("browser.preferences.instantApply", false) && !wait;
         var features = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal");
 
-        const wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
+        const xpcom_wm = Components.classes["@mozilla.org/appshell/window-mediator;1"];
+        var wm = xpcom_wm.getService(Components.interfaces.nsIWindowMediator);
         var win = wm.getMostRecentWindow("Browser:Preferences");
         var pane;
         if (win) {
@@ -719,7 +719,7 @@ ItsAllText.prototype.cleanEditDir = function(force) {
             try{
                 entry.remove(false);
             } catch(e) {
-                this.debug('unable to remove',entry,'because:',e);
+                this.log('unable to remove',entry,'because:',e);
             }
         }
     }
@@ -751,15 +751,19 @@ ItsAllText.prototype.menuNewExtEdit = function(event) {
 /**
  * The command that is called when selecting an existing extension.
  * @param {Event} event
+ * @param {string} ext
+ * @param {boolean} clobber
  */
-ItsAllText.prototype.menuExtEdit = function(event) {
+ItsAllText.prototype.menuExtEdit = function(event, ext, clobber) {
     var that = this;
     var uid = that.private_current_uid;
-    ItsAllText.debug('menuExtEdit:',uid);
-    var ext = event.target.getAttribute('label');
+    if (ext !== null) {
+        ext = typeof(ext) === 'string'?ext:event.target.getAttribute('label');
+    }
+    ItsAllText.debug('menuExtEdit:',uid, ext, clobber);
     var cobj = that.getCacheObj(uid);
     that.monitor.watch(cobj.node.ownerDocument);
-    cobj.edit(ext);
+    cobj.edit(ext, clobber);
 };
 
 /**
@@ -781,6 +785,7 @@ ItsAllText.prototype.rebuildMenu = function(uid, menu_id, is_disabled) {
     var magic_stop_node = null;
     var magic_start = null;
     var magic_stop = null;
+    var cobj = that.getCacheObj(uid);
 
     // Find the beginning and end of the magic replacement parts.
     for(i=0; i<items_length; i++) {
@@ -801,15 +806,25 @@ ItsAllText.prototype.rebuildMenu = function(uid, menu_id, is_disabled) {
     for(i = magic_stop - 1; i > magic_start; i--) {
         menu.removeChild(items[i]);
     }
+
+    if (cobj.edit_count <= 0 && cobj.file && cobj.file.exists()) {
+        node = document.createElementNS(that.XULNS, 'menuitem');
+        node.setAttribute('label', that.localeFormat('edit_existing', [cobj.extension]));
+        node.addEventListener('command', function(event){return that.menuExtEdit(event, null, false);}, false);
+        node.setAttribute('disabled', is_disabled?'true':'false');
+        menu.insertBefore(node, magic_stop_node);
+    }        
    
     // Insert the new magic bits
     for(i=0; i<exts.length; i++) {
         node = document.createElementNS(that.XULNS, 'menuitem');
-        node.setAttribute('label', exts[i]);
-        node.addEventListener('command', function(event){return that.menuExtEdit(event);}, false);
+        node.setAttribute('label', that.localeFormat('edit_ext', [exts[i]]));
+        (function() {
+            var ext=exts[i];
+            node.addEventListener('command', function(event){return that.menuExtEdit(event, ext);}, false);
+        })();
         node.setAttribute('disabled', is_disabled?'true':'false');
         menu.insertBefore(node, magic_stop_node);
-
     }
     return menu;
 };
@@ -818,13 +833,13 @@ ItsAllText.prototype.rebuildMenu = function(uid, menu_id, is_disabled) {
  * Returns the locale object for translation.
  */
 ItsAllText.prototype.getLocale = function() {
-    const string_bundle = Components.classes["@mozilla.org/intl/stringbundle;1"].
-        getService(Components.interfaces.nsIStringBundleService);
+    const string_bundle = Components.classes["@mozilla.org/intl/stringbundle;1"];
+    var obj = string_bundle.getService(Components.interfaces.nsIStringBundleService);
     /**
      * A localization bundle.  Use it like so:
      * ItsAllText.locale.getStringFromName('blah');
      */
-    return string_bundle.createBundle("chrome://itsalltext/locale/itsalltext.properties");
+    return obj.createBundle("chrome://itsalltext/locale/itsalltext.properties");
 };
 
 /**
diff --git a/src/chrome/content/itsalltext.xul b/src/chrome/content/itsalltext.xul
index cd8de19..f7c821b 100644
--- a/src/chrome/content/itsalltext.xul
+++ b/src/chrome/content/itsalltext.xul
@@ -17,7 +17,7 @@
                   accesskey="&newext.key;"
                   oncommand="ItsAllText.menuNewExtEdit(event);" />
         <menuseparator/>
-        <menuitem label=".txt" oncommand="ItsAllText.menuExtEdit(event);"/>
+        <menuitem label=".txt" oncommand="ItsAllText.menuExtEdit(event, '.txt');"/>
         <menuseparator/>
         <menuitem label="&readme.label;"
                   oncommand="ItsAllText.openReadme();"/>
@@ -36,7 +36,7 @@
                   accesskey="&newext.key;"
                   oncommand="ItsAllText.menuNewExtEdit(event);" />
         <menuseparator/>
-        <menuitem label=".txt" oncommand="ItsAllText.menuExtEdit(event);"/>
+        <menuitem label=".txt" oncommand="ItsAllText.menuExtEdit(event,'.txt');"/>
         <menuseparator/>
         <menuitem label="&readme.label;"
                   oncommand="ItsAllText.openReadme();"/>
diff --git a/src/chrome/locale/en-US/itsalltext.properties b/src/chrome/locale/en-US/itsalltext.properties
index 8eb3e49..543fcee 100644
--- a/src/chrome/locale/en-US/itsalltext.properties
+++ b/src/chrome/locale/en-US/itsalltext.properties
@@ -4,3 +4,5 @@ no_editor_pref=Preferences: Please pick an editor.
 problem_making_directory=I'm having a problem finding or creating the directory: %1$S
 gumdrop.width=28 
 gumdrop.height=14
+edit_ext=Edit as '%1$S'
+edit_existing=Edit using previous '%1$S' content
diff --git a/tests/index.html b/tests/index.html
index 24d15c4..fd005db 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -32,6 +32,7 @@
       <li><a href="hidden.html">Hidden Textareas</a></li>
       <li><a href="style.html">Styles</a></li>
       <li><a href="frames.html">Frames</a></li>
+      <li><a href="revisit.html">Revisit</a></li>
     </ol>
 
     <hr>
diff --git a/tests/revisit.html b/tests/revisit.html
new file mode 100644
index 0000000..977edaf
--- /dev/null
+++ b/tests/revisit.html
@@ -0,0 +1,45 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+    <title>Revisit Test</title>
+    <link rel="stylesheet" href="style.css" type="text/css">
+  </head>
+
+  <body>
+    <h1>Revisit Test</h1>
+
+    <div class="back"><a href=".">back</a></div>
+
+    <p>
+      Purpose: to verify IAT! will reuse the same file instead of deleting it.
+    </p>
+
+    <form action="" method="get">
+      <p>
+        Use this textarea for the tests below:
+        <textarea id="identity">testing area</textarea>
+      </p>
+    </form>
+    
+    <ol>
+      <li>
+        Click on the edit button.  Add the phrase "visit 1" to the text. Save and quit your editor.
+      </li>
+      <li>
+        Click on <a href="?">this link</a> to come back to this page.
+      </li>
+      <li>
+        Verify that the phrase "visit 1" is not in the textarea above.
+      </li>
+      <li>
+        Right-Click on the edit button. Choose the option “Edit using previous '.txt' file”
+        <br/>
+        The phrase "visit 1" should be in your editor.
+      </li>
+    </ol>
+
+    <div class="back"><a href=".">back</a></div>
+  </body>
+</html>
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/itsalltext.git



More information about the Pkg-mozext-commits mailing list