For the moment, MouseLock only works if the element being locked is in fullscreen mode. However, the w3c spec says that any element, in fullscreen or not could have the mouse locked into.
It was decided on  the Bug 6333602 discussion, that the first implementation of MouseLock would only work in fullscreen mode, but probably future versions will support non fullscreen mode as weel. With that in mind, I went back revising all the features that MouseLock was relying on the mozFullScreen API.

One of those, was the unlocking of the mouse.
If the locked element is removed from the DOM Tree, because the mouse can only be locked if the element is in fullscreen mode, and when the element is removed from the DOM Tree it automatically loses fullscreen, that would trigger the unlocking of the mouse.
So in the future, if an element that is not in fullscreen gets locked, and then removed from the DOM Tree, the mouse wouldn’t unlock it.

The Fix:

     
 nsCOMPtr node = do_QueryInterface(aOldChild);  
 if (node) {  
 nsCOMPtr domDoc;  
 node->GetOwnerDocument(getter_AddRefs(domDoc));  
 if (domDoc) {  
 nsCOMPtr lockedElement;  
 domDoc->GetMozFullScreenElement(getter_AddRefs(lockedElement));

 nsCOMPtr lockedNode = do_QueryInterface(lockedElement);  
 // If the element being removed is the same element with the mouse locked  
 // Then unlock the element  
 if (node == lockedNode) {  
 // Get Window  
 nsCOMPtr window;  
 domDoc->GetDefaultView(getter_AddRefs(window));  
 //Get Navigator  
 nsCOMPtr navigator;  
 window->GetNavigator(getter_AddRefs(navigator));  
 if (navigator) {  
 // Get Pointer  
 nsCOMPtr pointer;  
 navigator->GetPointer(getter_AddRefs(pointer));  
 if (pointer) {  
 // Unlock the mouse  
 pointer->Unlock();  
 }  
 }  
 }  
 }  
 }  

So now, if the locked element is removed from the DOM Tree, it will be unlocked in the nsINode::RemoveChild method not in the nsDocument::CancelFullScreen.
Not relying on the mozFullScreen API anymore

The second feature that MouseLock was relying on mozFullScreen, was that only elements in the DOM Tree could have the mouse locked. However, the element can only be locked if in fullscreen mode, and to get fullscreen mode, the element must be in the DOM Tree. So MouseLock wasn’t validating that, because mozFullScreen was filtering.

The Fix:

    
 nsCOMPtr targetNode(do_QueryInterface(aTarget));  
 nsCOMPtr parentNode;  
 targetNode->GetParentNode(getter_AddRefs(parentNode));  
 if (!parentNode) {  
 return false;  
 }  

We were also considering validating if the element being locked was a DOM element. However, it seems that only a nsIDOMElement can be locked, and if anything else gets passed as an argument an exception is thrown.

p.lock()
p.lock(true)
p.lock(“text”)
p.lock(12345)
p.lock(canvas, 1, 2)
p.lock(document)
p.lock(window)

[02:03:22.566] [Exception… “Could not convert JavaScript argument arg 0 [nsIDOMMouseLockable.lock]” nsresult: “0x80570009 (NSERRORXPCBADCONVERT_JS)” location: “JS frame :: Web Console :: :: line 1″ data: no]

So I guess we get for free the validation to check if the element being locked is a DOM element, since when trying to lock anything but a DOM element the browser throws an exception

I saved a few printf’s showing the flow of MouseLock when an element gets locked

Locking an element that is not in the DOM Tree

nsDOMMouseLockable::Lock
nsMouseLockableRequest::nsMouseLockableRequest
Calling ShouldLock..
nsDOMMouseLockable::ShouldLock

Element is not in the DOM Tree, returning false….

Dispatching failure callback to main thread
nsRequestMouseLockEvent::nsRequestMouseLockEvent
Failure request dispatched
Returning from Lock
RequestAllowMouseLockEvent::Run
nsMouseLockableRequest::SendFailure

Locking an element that is not in fullscreen mode

nsDOMMouseLockable::Lock
nsMouseLockableRequest::nsMouseLockableRequest
Calling ShouldLock..
nsDOMMouseLockable::ShouldLock
Element is in the DOM Tree
Element is not in fullscreen mode, returning false…
Dispatching failure callback to main thread
nsRequestMouseLockEvent::nsRequestMouseLockEvent
Failure request dispatched
Returning from lock

Locking an element that it’s in the DOM tree and in fullscren mode

nsDOMMouseLockable::Lock
nsMouseLockableRequest::nsMouseLockableRequest
Calling ShouldLock..
nsDOMMouseLockable::ShouldLock

Element is in the DOM Tree
Element is in fullscreen mode

returning true…
ShouldLock returned true
Dispatching success callback to main thread
nsRequestMouseLockEvent::nsRequestMouseLockEvent
Success request dispatched
Returning from lock
RequestAllowMouseLockEvent::Run
nsMouseLockableRequest::SendSuccess
 

I pushed the changes here