Cross-domain tabbing issues with SimpleModal

I’ve been using Eric Martin’s brilliant SimpleModal jQuery plugin for a couple of weeks now and am loving it’s simplicity.

However, I’ve run into a problem whereby if I use an iframe, with a source which points to a different domain, then I cannot tab into my modal dialog. This is because SimpleModal works out the first and last input elements in the modal dialog to ensure that you cannot tab out of it (after all, you don’t want your users tabbing to controls which should not be enabled!). However, if the iframe has loaded a page from a different domain then javascript is unable to “see” the input elements inside the page (due to cross-domain policy).

Thanks to StackOverflow I’ve come up with the following solution. Add invisible inputs before and after your modal frame, and modify SimplyModal.js slightly.

So instead of

$.modal("<iframe src='http://www.bing.co.uk' />");

we use

$.modal("<input style='display:none' /><iframe src='http://www.bing.co.uk' /><input style='display:none' />");

This provides two input elements that SimpleModal can detect as they are on your domain. Anything between these two elements is tabbable. However, you also need to edit the following function in jquery.SimpleModal.js to ensure that invisible input elements are detected.

// s.inputs = $(':input:enabled:visible:first, :input:enabled:visible:last', s.d.data[0]);
s.inputs = $(':input:enabled:first, :input:enabled:last', s.d.data[0]);

Debugging a Windows Service in C#

Writing a Windows Service in .NET is as easy as selecting the…wait for it…”Windows Service” project type for your new project. However, debugging a service is more difficult. Typically you will start the service, locate the process name, and manually attach a debugger to it, which is 2.5 steps too many for me.

If you try and F5 it from Visual Studio, you will receive the following error message:

Cannot start service from the command line or a debugger. A Windows Service must first be installed (using installutil.exe) and then started with the ServerExplorer, Windows Services Administrative tool or the NET START command.

So how do you start a service from Visual Studio? The answer is actually quite simple. Simply change your Main method to check if you are debugging and, if so, jump straight into your service (providing a suitable entry point in your service of course).

static void Main()
{
	if (System.Diagnostics.Debugger.IsAttached)
	{
		// We are running with a debugger attached, so start
		// the service directly.
		Service1 service = new Service1();
		string[] args = new string[] { "arg1", "arg2" };
		service.StartFromDebugger(args);
	}
	else
	{
		// We are running as a service so start normally.
		ServiceBase[] ServicesToRun;
		ServicesToRun = new ServiceBase[] 
		{ 
			new Service1() 
		};
		ServiceBase.Run(ServicesToRun);
	}
}

// IN YOUR SERVICE
public partial class Service1 : ServiceBase
{
	public void StartFromDebugger(string[] args)
	{
		OnStart(args);
	}
	
	// Normal service code
}

Extension method for is string whitespace, null or empty.

Here’s a quick extension method which returns true if string is whitespace, null or empty. I’ve also added

using Microsoft.VisualStudio.TestTools.UnitTesting;

public static class Extensions
{
    public static bool IsWhitespaceNullOrEmpty(this string s)
    {
        if (s == null) return true;
        if (s.Trim() == string.Empty) return true;
        return false;
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestStringNull()
    {
        string s = null;
        Assert.IsTrue(s.IsWhitespaceNullOrEmpty());
        Assert.IsTrue(((string)null).IsWhitespaceNullOrEmpty());
    }

    [TestMethod]
    public void TestStringEmpty()
    {
        Assert.IsTrue(string.Empty.IsWhitespaceNullOrEmpty());
    }

    [TestMethod]
    public void TestSpaceStringEmpty()
    {
        Assert.IsTrue("    ".IsWhitespaceNullOrEmpty());
    }

    [TestMethod]
    public void TestTabStringEmpty()
    {
        Assert.IsTrue("\t".IsWhitespaceNullOrEmpty());
    }

    [TestMethod]
    public void TestStringNotEmpty()
    {
        Assert.IsTrue(!"a".IsWhitespaceNullOrEmpty());
    }
}

Automatically run unit-tests when saving in Eclipse

Following on from Automatically run unit-tests when compiling in Visual Studio 2008, I was delighted to discover that the Eclipse alternative is even better.

In Eclipse, simply install a plugin called Infinitest, from the official download site. Also, ensure ProjectBuild automatically is checked.

Now, whenever you save, it will run the relevant tests. This is much quicker than having to run all the tests (although you should do that every now and then to be safe!!). Secondly, test errors appear as build errors, and you get a nice red or green result in the status bar, so the UI is perfect. Finally, Eclipse just seems to run tests vastly quicker than VS 2008.