Prevent JS error when getting ownerDocument from document element

document elements have "ownerDocument" set to null. Because in some rare
cases, the document element is passed as an argument, code accessing
the ownerDocument property must check if the argument is already the
document element.

214466

Change-Id: If3fe934bb4893ee78466a1e7483761bfb64ff181
Reviewed-on: https://git.eclipse.org/r/105505
Reviewed-by: Claudio Guglielmo <claudio.guglielmo@bsiag.com>
Tested-by: Claudio Guglielmo <claudio.guglielmo@bsiag.com>
Reviewed-on: https://git.eclipse.org/r/105544
Tested-by: Hudson CI
Reviewed-by: Beat Schwarzentrub <bsh@bsiag.com>
diff --git a/org.eclipse.scout.rt.ui.html/src/main/js/jquery/jquery-scout.js b/org.eclipse.scout.rt.ui.html/src/main/js/jquery/jquery-scout.js
index 6709610..eef558a 100644
--- a/org.eclipse.scout.rt.ui.html/src/main/js/jquery/jquery-scout.js
+++ b/org.eclipse.scout.rt.ui.html/src/main/js/jquery/jquery-scout.js
@@ -22,11 +22,11 @@
  */
 function elemVisible(elem) {
   // Check if element itself is hidden by its own style attribute
-  if (isHidden(elem.style)) {
+  if (!elem || isHidden(elem.style)) {
     return false;
   }
   // Must use correct window for element / computedStyle
-  var myWindow = elem.ownerDocument.defaultView;
+  var myWindow = (elem instanceof Document ? elem : elem.ownerDocument).defaultView;
   // Check if element itself is hidden by external style-sheet
   if (isHidden(myWindow.getComputedStyle(elem))) {
     return false;
@@ -257,7 +257,7 @@
  * @param domElement (optional) if true the result is returned as DOM element, otherwise it is returned as jQuery object. The default is false.
  */
 $.fn.document = function(domElement) {
-  var myDocument = this.length ? this[0].ownerDocument : null;
+  var myDocument = this.length ? (this[0] instanceof Document ? this[0] : this[0].ownerDocument) : null;
   return domElement ? myDocument : $(myDocument);
 };
 
diff --git a/org.eclipse.scout.rt.ui.html/src/main/js/scout/focus/focusUtils.js b/org.eclipse.scout.rt.ui.html/src/main/js/scout/focus/focusUtils.js
index 57c6cda..5e6b3bb 100644
--- a/org.eclipse.scout.rt.ui.html/src/main/js/scout/focus/focusUtils.js
+++ b/org.eclipse.scout.rt.ui.html/src/main/js/scout/focus/focusUtils.js
@@ -92,7 +92,7 @@
       activeElement = element.activeElement(true);
       element = element[0];
     } else {
-      activeElement = element.ownerDocument.activeElement;
+      activeElement = (element instanceof Document ? element : element.ownerDocument).activeElement;
     }
     return activeElement === element;
   }
diff --git a/org.eclipse.scout.rt.ui.html/src/main/js/scout/form/fields/ValueField.js b/org.eclipse.scout.rt.ui.html/src/main/js/scout/form/fields/ValueField.js
index f1ec649..eb13b54 100644
--- a/org.eclipse.scout.rt.ui.html/src/main/js/scout/form/fields/ValueField.js
+++ b/org.eclipse.scout.rt.ui.html/src/main/js/scout/form/fields/ValueField.js
@@ -180,7 +180,7 @@
  * That is used in DateField.js with multiple input elements.
  */
 scout.ValueField._getActiveValueField = function(target) {
-  var $activeElement = $(target.ownerDocument.activeElement),
+  var $activeElement = $(target).activeElement(),
     valueField = $activeElement.data('valuefield') || $activeElement.parent().data('valuefield');
   return valueField && !(valueField.$field && valueField.$field.hasClass('disabled')) ? valueField : null;
 };