Bug 764240 - Clamp window.sizeToContent() the same way we clamp other resizing methods. r=bz

This commit is contained in:
Mounir Lamouri 2012-11-19 23:18:21 +00:00
parent 095dc9e962
commit f3e9f0a56b
7 changed files with 117 additions and 21 deletions

View File

@ -23,7 +23,7 @@ interface nsIMarkupDocumentViewer;
[ref] native nsIMarkupDocumentViewerTArray(nsTArray<nsCOMPtr<nsIMarkupDocumentViewer> >);
[scriptable, uuid(1016d5e8-690f-4d97-8ac5-d50ffa341c46)]
[scriptable, uuid(02d37b31-e654-4b74-9bc3-14dfe0020bb3)]
interface nsIMarkupDocumentViewer : nsISupports
{
@ -68,10 +68,10 @@ interface nsIMarkupDocumentViewer : nsISupports
//void GetCharacterSetHint(in wstring hintCharset, in int32_t charsetSource);
/**
* Tell the container to shrink-to-fit or grow-to-fit its contents
*/
void sizeToContent();
/**
* Requests the size of the content to the container.
*/
void getContentSize(out long width, out long height);
/**
* Options for Bidi presentation.

View File

@ -5474,12 +5474,32 @@ nsGlobalWindow::SizeToContent()
// The content viewer does a check to make sure that it's a content
// viewer for a toplevel docshell.
nsCOMPtr<nsIContentViewer> cv;
mDocShell->GetContentViewer(getter_AddRefs(cv));
nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
NS_ENSURE_TRUE(markupViewer, NS_ERROR_FAILURE);
NS_ENSURE_SUCCESS(markupViewer->SizeToContent(), NS_ERROR_FAILURE);
int32_t width, height;
NS_ENSURE_SUCCESS(markupViewer->GetContentSize(&width, &height),
NS_ERROR_FAILURE);
// Make sure the new size is following the CheckSecurityWidthAndHeight
// rules.
nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
GetTreeOwner(getter_AddRefs(treeOwner));
NS_ENSURE_TRUE(treeOwner, NS_ERROR_FAILURE);
nsIntSize cssSize(DevToCSSIntPixels(nsIntSize(width, height)));
NS_ENSURE_SUCCESS(CheckSecurityWidthAndHeight(&cssSize.width,
&cssSize.height),
NS_ERROR_FAILURE);
nsIntSize newDevSize(CSSToDevIntPixels(cssSize));
nsCOMPtr<nsIDocShellTreeItem> docShellAsItem = do_QueryInterface(mDocShell);
NS_ENSURE_SUCCESS(treeOwner->SizeShellTo(docShellAsItem,
newDevSize.width, newDevSize.height),
NS_ERROR_FAILURE);
return NS_OK;
}

View File

@ -135,6 +135,7 @@ MOCHITEST_FILES = \
file_bug809290_b2.html \
file_bug809290_c.html \
file_empty.html \
test_sizetocontent_clamp.html \
$(NULL)
ifneq (Linux,$(OS_ARCH))

View File

@ -0,0 +1,73 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=764240
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 764240</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=764240">Mozilla Bug 764240</a>
<p id="display"></p>
<div id="content">
<button onclick="test();">run test</button>
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 764240 **/
SimpleTest.waitForExplicitFinish();
// Error margin allowed for the window's size.
// The window size being set by the platform, we can't guarantee it will be respected.
// In platforms other than Windows, 5 is a good epsilon. On windows, it seems
// that most of the time, the width is 14/15 higher than expected so we need
// to have an epsilon that matches this difference.
// Actually, on platforms other than Windows, epsilon of 0 might work.
// Only innerWidth on Windows has a 14/15 pixels difference to what is
// requested.
var epsilon = navigator.platform.indexOf("Win") == -1 ? 5 : 20;
function test() {
var w = window.open('data:text/html,null', null, 'width=300,height=300');
var nbResize = 0;
SimpleTest.waitForFocus(function() {
w.onresize = function() {
nbResize++;
if (nbResize == 1) {
return;
}
ok(w.innerWidth + epsilon >= 100 && w.innerWidth - epsilon <= 100,
"innerWidth should be around 100");
ok(w.innerHeight + epsilon >= 100 && w.innerHeight - epsilon <= 100,
"innerHeight should be around 100");
// It's not clear why 2 events are coming...
is(nbResize, 2, "We should get 2 events.");
w.close();
SimpleTest.waitForFocus(function() {
SimpleTest.finish();
});
};
w.sizeToContent();
}, w);
}
SimpleTest.waitForFocus(function() {
synthesizeMouseAtCenter(document.getElementsByTagName('button')[0], {});
});
</script>
</pre>
</body>
</html>

View File

@ -3260,7 +3260,8 @@ NS_IMETHODIMP DocumentViewerImpl::ChangeMaxLineBoxWidth(int32_t aMaxLineBoxWidth
return NS_OK;
}
NS_IMETHODIMP DocumentViewerImpl::SizeToContent()
NS_IMETHODIMP
DocumentViewerImpl::GetContentSize(int32_t* aWidth, int32_t* aHeight)
{
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
@ -3301,23 +3302,15 @@ NS_IMETHODIMP DocumentViewerImpl::SizeToContent()
GetPresContext(getter_AddRefs(presContext));
NS_ENSURE_TRUE(presContext, NS_ERROR_FAILURE);
int32_t width, height;
// so how big is it?
nsRect shellArea = presContext->GetVisibleArea();
// Protect against bogus returns here
NS_ENSURE_TRUE(shellArea.width != NS_UNCONSTRAINEDSIZE &&
shellArea.height != NS_UNCONSTRAINEDSIZE,
NS_ERROR_FAILURE);
width = presContext->AppUnitsToDevPixels(shellArea.width);
height = presContext->AppUnitsToDevPixels(shellArea.height);
nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
docShellAsItem->GetTreeOwner(getter_AddRefs(treeOwner));
NS_ENSURE_TRUE(treeOwner, NS_ERROR_FAILURE);
NS_ENSURE_SUCCESS(treeOwner->SizeShellTo(docShellAsItem, width, height),
NS_ERROR_FAILURE);
*aWidth = presContext->AppUnitsToDevPixels(shellArea.width);
*aHeight = presContext->AppUnitsToDevPixels(shellArea.height);
return NS_OK;
}

View File

@ -180,6 +180,7 @@
"dom/tests/mochitest/bugs/test_bug641552.html": "",
"dom/tests/mochitest/bugs/test_devicemotion_multiple_listeners.html": "bug 775227",
"dom/tests/mochitest/bugs/test_resize_move_windows.html": "TIMED_OUT",
"dom/tests/mochitest/bugs/test_sizetocontent_clamp.html": "TIMED_OUT",
"dom/tests/mochitest/bugs/test_window_bar.html": "",
"dom/tests/mochitest/dom-level2-core/test_documentimportnode03.html": "",
"dom/tests/mochitest/dom-level2-core/test_documentimportnode04.html": "",

View File

@ -975,9 +975,17 @@ void nsXULWindow::OnChromeLoaded()
// (if LoadSizeFromXUL set the size, mIntrinsicallySized will be false)
nsCOMPtr<nsIContentViewer> cv;
mDocShell->GetContentViewer(getter_AddRefs(cv));
nsCOMPtr<nsIMarkupDocumentViewer> markupViewer(do_QueryInterface(cv));
if (markupViewer)
markupViewer->SizeToContent();
nsCOMPtr<nsIMarkupDocumentViewer> markupViewer = do_QueryInterface(cv);
if (markupViewer) {
nsCOMPtr<nsIDocShellTreeItem> docShellAsItem = do_QueryInterface(mDocShell);
nsCOMPtr<nsIDocShellTreeOwner> treeOwner;
docShellAsItem->GetTreeOwner(getter_AddRefs(treeOwner));
if (treeOwner) {
int32_t width, height;
markupViewer->GetContentSize(&width, &height);
treeOwner->SizeShellTo(docShellAsItem, width, height);
}
}
}
bool positionSet = !mIgnoreXULPosition;