Opened 14 years ago

Closed 13 years ago

#415 closed enhancement (fixed)

Notification of web server requirement

Reported by: jkronika@… Owned by: gogo
Priority: high Milestone: Version 1.0
Component: Xinha Core Version: trunk
Severity: normal Keywords: xmlhttprequest status local web file server
Cc:

Description

Since Xinha cannot be run properly as a local file (using "protocol" of file:///...), I suggest an addition of the following to the postBack and getBack functions in HTMLArea.js. This will provide a more descriptive notification of the error encountered when Xinha is run locally:

else if(req.status == 0 && document.URL.search(/file:\/\/\) > -1)
{

alert('Xinha cannot be run locally (without a web server).');

}

This would be placed between the if and else portions checking the req.status value that currently provide feedback on errors.

Change History (14)

comment:1 Changed 14 years ago by jkronika@…

Here is an SVN DIFF of my changes

Index: htmlarea.js
===================================================================
--- htmlarea.js (revision 286)
+++ htmlarea.js (working copy)
@@ -5025,6 +5025,10 @@
         if(typeof handler == 'function')
         handler(req.responseText, req);
       }
+      else if(req.status == 0 && document.URL.search(/^file:\/\/\//) > -1)
+      {
+        alert('Xinha cannot be run locally (without a web server).');
+      }
       else
       {
         alert('An error has occurred: ' + req.statusText);
@@ -5064,6 +5068,10 @@
       {
         handler(req.responseText, req);
       }
+      else if(req.status == 0 && document.URL.search(/^file:\/\/\//) > -1)
+      {
+        alert('Xinha cannot be run locally (without a web server).');
+      }
       else
       {
         alert('An error has occurred: ' + req.statusText);
@@ -5491,4 +5499,4 @@
 }

comment:2 Changed 14 years ago by niko

some kind of alert would be really a good idea, however with this solution there might popup the alert several times
(for every language file that gets loaded and every where else where xmlhttprequest is used.

...wouldn't it be possible to check ONCE when inizializeing xinha if we run on file://?

comment:3 Changed 14 years ago by jkronika@…

What about using the following change to halt loading Xinha entirely if the protocol is file://, since it won't work properly anyway?

@@ -5334,6 +5334,17 @@

 HTMLArea.startEditors = function(editors)
 {
+  // if xinha is being run locally and not on a web server, XMLHTTPRequest
+  // will not function, and therefore Xinha will be rendered un-usable
+  if(document.URL.match(/^file:\/\/.*?$/))
+  {
+    // warn the user of the error
+    alert('Xinha must be installed on a web server. Locally opened '
+        + 'files (those that use the "file://" protocol) cannot '
+        + 'properly function. Xinha will not be loaded.');
+    return;
+  }
+
   for(var i in editors)
   {
     if(editors[i].generate) editors[i].generate();

comment:4 Changed 13 years ago by gogo

  • Milestone set to Version 1.0
  • Priority changed from normal to high

comment:5 Changed 13 years ago by mokhet

There is plenty way to init a Xinha instance, I for instance never use startEditor(). So i think it would be better to check the document.URL in the constructor and nowhere else. Just throwing an error which explain why the editor didnt started is enough i think, i hate all thoses alerts.

Here is the patch, can i commit it or am i missing something obvious here ?

Index: htmlarea.js
===================================================================
--- htmlarea.js	(revision 461)
+++ htmlarea.js	(working copy)
@@ -76,6 +76,15 @@
 // ID with it.
 function HTMLArea(textarea, config)
 {
+  // if Xinha is being run locally and not on a web server, XMLHTTPRequest
+  // will not function, and therefore Xinha will be rendered un-usable
+  if (document.URL.toLowerCase().match(/^file:\/\/.*?$/))
+  {
+    throw('Xinha must be installed on a web server. Locally opened '
+      + 'files (those that use the "file://" protocol) cannot '
+      + 'properly function. Xinha will not be loaded.');
+  }
+  
   if(!textarea) throw("Tried to create HTMLArea without textarea specified.");
 
   if (HTMLArea.checkSupportedBrowser()) {

comment:6 Changed 13 years ago by gogo

mokhet: Mmmmm, I think an alert is better, throwing won't popup an alert so the user won't know why it's not working unless they look at thier error log.

comment:7 Changed 13 years ago by mokhet

Yes, i can see the point of the alert. But in this case, we should have a consistant way to throw an error. I mean, sometimes it's an alert, sometimes it's throws error in console.

Something like this :

HTMLArea.ERR = function(txt)
{
  alert(txt);
  throw(txt);
};

Then, instead of doing stuff like this :

if(!textarea) throw("Tried to create ...");

we can simply transform it to :

if(!textarea) HTMLArea.ERR("Tried to create ...");

It's not much change, but this will allow any Xinha users (not the final users, ppl using Xinha in their devs) to choose the way errors are managed. I'm sorry for my english, let me try to explain better.

I, for instance, have in my project my own way to capture errors and show them to the user. alert() is absolutly not enough for me because it is breaking my UI, it is breaking the consistant way i have for all others js errors all over the application. If something like HTMLArea.ERR is introduced, it could then be updated by any user with their own wrapper when the alert boxes are not enough.

I'll try to explain better my point if it's not understable enough. But if you understand, what do you think about introducing something like HTMLArea.ERR to manage all thoses nasty errors ?

comment:8 Changed 13 years ago by gogo

Throwing an error though might not be just to put it in the console, Javascript has full exception handling you know ;-)

If anything, we should probably throw everything, but put a try-catch block around everything to catch uncaught exceptions and send them to HTMLArea.ReportError?()

comment:9 Changed 13 years ago by mokhet

to be honest, i've read and quite understood http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:throw but i've never used anything else than a string as parameter to "manage" the "error". And since Xinha is using only strings too nothing stop the creation of HTMLArea.ReportError?();

That's would be wonderfull to finally have thoses nasty alert boxes when something bad happens. Especially when the people seeing the alert boxes barely understand what is a computer.

Humm, let me sleep a few and i'll try to make a patch ;-)

comment:10 Changed 13 years ago by mokhet

Wonder why i always end up forking the original ticket. Pardon for that.

comment:11 Changed 13 years ago by mokhet

Here is a patch that will close the original ticket (web server requirement). I've tested it in the branch /branches/mokhet and it's running ok as far i can say.

it adds a boolean flag : HTMLArea.isRunLocally

If this flag is true, it alerts about the need of a web server. However it doesnt prevent Xinha to generate since it can do it with most plugins.

And then in the methods using xmlhttp, instead of testing (req.status == 200), it test (req.status == 200 *OR* HTMLArea.isRunLocally && req.status == 0)

Index: htmlarea.js
===================================================================
--- htmlarea.js	(revision 548)
+++ htmlarea.js	(working copy)
@@ -78,6 +78,11 @@
 HTMLArea.is_mac_ie = (HTMLArea.is_ie && HTMLArea.is_mac);
 HTMLArea.is_win_ie = (HTMLArea.is_ie && !HTMLArea.is_mac);
 HTMLArea.is_gecko  = (navigator.product == "Gecko");
+HTMLArea.isRunLocally = document.URL.toLowerCase().search(/^file:/) != -1;
+if ( HTMLArea.isRunLocally )
+{
+  alert('Xinha *must* be installed on a web server. Locally opened files (those that use the "file://" protocol) cannot properly function. Xinha will try to initialize but may not be correctly loaded.');
+}
 
 // Creates a new HTMLArea object.  Tries to replace the textarea with the given
 // ID with it.
@@ -6141,7 +6146,7 @@
   {
     if ( req.readyState == 4 )
     {
-      if ( req.status == 200 )
+      if ( req.status == 200 || HTMLArea.isRunLocally && req.status == 0 )
       {
         if ( typeof handler == 'function' )
         {
@@ -6179,7 +6184,7 @@
   {
     if ( req.readyState == 4 )
     {
-      if ( req.status == 200 )
+      if ( req.status == 200 || HTMLArea.isRunLocally && req.status == 0 )
       {
         handler(req.responseText, req);
       }
@@ -6210,7 +6215,7 @@
   // Synchronous!
   req.open('GET', url, false);
   req.send(null);
-  if ( req.status == 200 )
+  if ( req.status == 200 || HTMLArea.isRunLocally && req.status == 0 )
   {
     return req.responseText;
   }

Is this patch ok for everyone. Can i commit to trunk and close this ticket ?

gogo:
For the other part of this ticket (HTMLArea.ReportError?), should we open a new ticket ?

comment:12 Changed 13 years ago by gogo

Looks good, but is it even worth trying to initialize, pretty sure it would fail anyway, so why not just bail out before doing anything at all?

Yes I think another ticket for the other part, I'll leave you to open it mokhet after you commit and close this one.

comment:13 Changed 13 years ago by mokhet

Probably not all plugins will load correctly (ImageManager?, Linker, etc) but since the basics examples are working fine locally (with this patch), even the full_example.html is working fine when run locally, i though it would be better to warn about a possible fail (alert) but it is trying anyway.

I will commit this patch in a few.

comment:14 Changed 13 years ago by mokhet

  • Resolution set to fixed
  • Status changed from new to closed

patch applied in changeset:549

Closing this ticket now.

Note: See TracTickets for help on using tickets.