Commit a5f9821f authored by Raine Makelainen's avatar Raine Makelainen

Sync Utils.js from Metro code base that are related to selection handling

Bug 905808 - Reuse browser shift code from SelectionHandler to center findbar searches. r=mbrubeck
parent 0c793e91
......@@ -6,11 +6,14 @@ let Util = {
/*
* General purpose utilities
*/
getWindowUtils: function getWindowUtils(aWindow) {
return aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
},
fuzzyEquals: function fuzzyEquals(a, b) {
return (Math.abs(a - b) < 0.999);
},
// Recursively find all documents, including root document.
getAllDocuments: function getAllDocuments(doc, resultSoFar) {
resultSoFar = resultSoFar || [doc];
......@@ -185,6 +188,46 @@ let Util = {
aElement instanceof Ci.nsIDOMHTMLTextAreaElement);
},
/**
* Checks whether aElement's content can be edited either if it(or any of its
* parents) has "contenteditable" attribute set to "true" or aElement's
* ownerDocument is in design mode.
*/
isEditableContent: function isEditableContent(aElement) {
return !!aElement && (aElement.isContentEditable ||
this.isOwnerDocumentInDesignMode(aElement));
},
isEditable: function isEditable(aElement) {
if (!aElement) {
return false;
}
if (this.isTextInput(aElement) || this.isEditableContent(aElement)) {
return true;
}
// If a body element is editable and the body is the child of an
// iframe or div we can assume this is an advanced HTML editor
if ((aElement instanceof Ci.nsIDOMHTMLIFrameElement ||
aElement instanceof Ci.nsIDOMHTMLDivElement) &&
aElement.contentDocument &&
this.isEditableContent(aElement.contentDocument.body)) {
return true;
}
return false;
},
/**
* Checks whether aElement's owner document has design mode turned on.
*/
isOwnerDocumentInDesignMode: function(aElement) {
return !!aElement && !!aElement.ownerDocument &&
aElement.ownerDocument.designMode == "on";
},
isMultilineInput: function isMultilineInput(aElement) {
return (aElement instanceof Ci.nsIDOMHTMLTextAreaElement);
},
......@@ -406,6 +449,44 @@ let Util = {
//Services.obs.notifyObservers(download, "dl-start", null);
},
/*
* aViewHeight - the height of the viewable area in the browser
* aRect - a bounding rectangle of a selection or element.
*
* return - number of pixels for the browser to be shifted up by such
* that aRect is centered vertically within aViewHeight.
*/
centerElementInView: function centerElementInView(aViewHeight, aRect) {
// If the bottom of the target bounds is higher than the new height,
// there's no need to adjust. It will be above the keyboard.
if (aRect.bottom <= aViewHeight) {
return 0;
}
// height of the target element
let targetHeight = aRect.bottom - aRect.top;
// height of the browser view.
let viewBottom = content.innerHeight;
// If the target is shorter than the new content height, we can go ahead
// and center it.
if (targetHeight <= aViewHeight) {
// Try to center the element vertically in the new content area, but
// don't position such that the bottom of the browser view moves above
// the top of the chrome. We purposely do not resize the browser window
// by making it taller when trying to center elements that are near the
// lower bounds. This would trigger reflow which can cause content to
// shift around.
let splitMargin = Math.round((aViewHeight - targetHeight) * .5);
let distanceToPageBounds = viewBottom - aRect.bottom;
let distanceFromChromeTop = aRect.bottom - aViewHeight;
let distanceToCenter =
distanceFromChromeTop + Math.min(distanceToPageBounds, splitMargin);
return distanceToCenter;
}
},
/*
* Local system utilities
*/
......
......@@ -36,10 +36,6 @@ var gScreenHeight = 0;
const kEmbedStateActive = 0x00000001; // :active pseudoclass for elements
function fuzzyEquals(a, b) {
return (Math.abs(a - b) < 0.999);
}
function EmbedHelper() {
this.contentDocumentIsDisplayed = true;
// Reasonable default. Will be read from preferences.
......@@ -502,7 +498,7 @@ EmbedHelper.prototype = {
rect.h = fixedCurrentViewport.height;
// Are we really zooming.
aAllowZoom = !fuzzyEquals(rect.w, this._viewportData.cssCompositedRect.width)
aAllowZoom = !Util.fuzzyEquals(rect.w, this._viewportData.cssCompositedRect.width)
if (aAllowZoom) {
var winid = Services.embedlite.getIDByWindow(content);
......@@ -675,7 +671,7 @@ EmbedHelper.prototype = {
oldVpHeight = oldVpHeight / scaleFactor;
vpHeight = vpHeight / scaleFactor;
this.vkbOpen = fuzzyEquals(oldVpHeight - vpHeight, this.vkbOpenCompositionMetrics.bottomMargin / 2);
this.vkbOpen = Util.fuzzyEquals(oldVpHeight - vpHeight, this.vkbOpenCompositionMetrics.bottomMargin / 2);
return this.vkbOpen;
}
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment