Bug 672474 - add nsIDOMWindowUtils.getScrollbarWidth(aFlushLayout); r=roc

This commit is contained in:
Tim Taubert 2013-02-13 21:32:35 +01:00
parent 8a85444625
commit 67433fbff1
5 changed files with 96 additions and 1 deletions

View File

@ -1439,6 +1439,37 @@ nsDOMWindowUtils::GetScrollXY(bool aFlushLayout, int32_t* aScrollX, int32_t* aSc
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowUtils::GetScrollbarWidth(bool aFlushLayout, int32_t* aResult)
{
if (!nsContentUtils::IsCallerChrome()) {
return NS_ERROR_DOM_SECURITY_ERR;
}
*aResult = 0;
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
NS_ENSURE_STATE(window);
nsCOMPtr<nsIDocument> doc(do_QueryInterface(window->GetExtantDocument()));
NS_ENSURE_STATE(doc);
if (aFlushLayout) {
doc->FlushPendingNotifications(Flush_Layout);
}
nsIPresShell* presShell = doc->GetShell();
NS_ENSURE_TRUE(presShell, NS_ERROR_NOT_AVAILABLE);
nsIScrollableFrame* scrollFrame = presShell->GetRootScrollFrameAsScrollable();
NS_ENSURE_TRUE(scrollFrame, NS_OK);
nsMargin sizes = scrollFrame->GetActualScrollbarSizes();
*aResult = nsPresContext::AppUnitsToIntCSSPixels(sizes.LeftRight());
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowUtils::GetRootBounds(nsIDOMClientRect** aResult)
{

View File

@ -41,7 +41,7 @@ interface nsIDOMClientRect;
interface nsIURI;
interface nsIDOMEventTarget;
[scriptable, uuid(020deb5a-cba6-41dd-8551-72a880d01970)]
[scriptable, uuid(16b3bdcc-75d4-11e2-8a20-aaff78957a39)]
interface nsIDOMWindowUtils : nsISupports {
/**
@ -647,6 +647,13 @@ interface nsIDOMWindowUtils : nsISupports {
*/
void getScrollXY(in boolean aFlushLayout, out long aScrollX, out long aScrollY);
/**
* Returns the scrollbar width of the window's scroll frame.
*
* @param aFlushLayout flushes layout if true. Otherwise, no flush occurs.
*/
long getScrollbarWidth(in boolean aFlushLayout);
/**
* Returns the bounds of the window's currently loaded document. This will
* generally be (0, 0, pageWidth, pageHeight) but in some cases (e.g. RTL

View File

@ -29,6 +29,8 @@ MOCHITEST_FILES = \
test_consoleAPI.html \
test_domWindowUtils.html \
test_domWindowUtils_scrollXY.html \
test_domWindowUtils_scrollbarWidth.html \
file_domWindowUtils_scrollbarWidth.html \
test_offsets.html \
test_offsets.js \
test_windowProperties.html \

View File

@ -0,0 +1,7 @@
<!DOCTYPE HTML>
<html>
<body style='width: 100000px; overflow: hidden;'></body>
<div id="float" style="float: left; overflow: scroll;">
<div style="width: 200px;"></div>
</div>
</html>

View File

@ -0,0 +1,48 @@
<!DOCTYPE HTML>
<html>
<head>
<title>nsIDOMWindowUtils::getScrollbarWidth test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body id="body">
<script type="application/javascript;version=1.8">
function doTests() {
let iframe = document.getElementById("iframe");
let cwindow = iframe.contentWindow;
let utils = SpecialPowers.getDOMWindowUtils(cwindow);
let doc = cwindow.document;
function haveNonFloatingScrollbars() {
return doc.getElementById("float").offsetWidth > 200;
}
is(utils.getScrollbarWidth(true), 0,
"getScrollbarWidth returns zero without a scrollbar");
// Some platforms (esp. mobile) may have floating scrollbars that don't
// affect layout. Thus getScrollbarWidth() would always return 0.
if (haveNonFloatingScrollbars()) {
let body = doc.querySelector("body");
body.style.overflowY = "scroll";
is(utils.getScrollbarWidth(false), 0,
"getScrollbarWidth returns zero with a vertical scrollbar w/o flushing");
ok(utils.getScrollbarWidth(true) > 0,
"getScrollbarWidth returns non-zero with a vertical scrollbar with flushing");
}
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
</script>
<iframe src="http://mochi.test:8888/tests/dom/tests/mochitest/general/file_domWindowUtils_scrollbarWidth.html"
id="iframe" onload="doTests();">
</iframe>
</body>
</html>