diff --git a/include/capi/cef_dom_capi.h b/include/capi/cef_dom_capi.h index 97300ec38..7e28e8992 100644 --- a/include/capi/cef_dom_capi.h +++ b/include/capi/cef_dom_capi.h @@ -33,7 +33,7 @@ // by hand. See the translator.README.txt file in the tools directory for // more information. // -// $hash=c6de3fb4d64a2b2ad06a4b9c5e9d7625d40b5bb6$ +// $hash=ddff4ad975fc26d0abfe05799aedb597b8274ffd$ // #ifndef CEF_INCLUDE_CAPI_CEF_DOM_CAPI_H_ @@ -335,7 +335,8 @@ typedef struct _cef_domnode_t { struct _cef_domnode_t* self); /// - // Returns the bounds of the element. + // Returns the bounds of the element in device pixels. Use + // "window.devicePixelRatio" to convert to/from CSS pixels. /// cef_rect_t(CEF_CALLBACK* get_element_bounds)(struct _cef_domnode_t* self); } cef_domnode_t; diff --git a/include/cef_dom.h b/include/cef_dom.h index 382b19305..d45647bd8 100644 --- a/include/cef_dom.h +++ b/include/cef_dom.h @@ -323,7 +323,8 @@ class CefDOMNode : public virtual CefBaseRefCounted { virtual CefString GetElementInnerText() = 0; /// - // Returns the bounds of the element. + // Returns the bounds of the element in device pixels. Use + // "window.devicePixelRatio" to convert to/from CSS pixels. /// /*--cef()--*/ virtual CefRect GetElementBounds() = 0; diff --git a/tests/ceftests/dom_unittest.cc b/tests/ceftests/dom_unittest.cc index 1b262524b..d07628d07 100644 --- a/tests/ceftests/dom_unittest.cc +++ b/tests/ceftests/dom_unittest.cc @@ -59,7 +59,8 @@ class TestDOMVisitor : public CefDOMVisitor { EXPECT_FALSE(textNode->HasChildren()); } - void TestBodyNodeStructure(CefRefPtr bodyNode) { + void TestBodyNodeStructure(CefRefPtr bodyNode, + float devicePixelRatio) { EXPECT_TRUE(bodyNode.get()); EXPECT_TRUE(bodyNode->IsElement()); EXPECT_FALSE(bodyNode->IsText()); @@ -133,11 +134,14 @@ class TestDOMVisitor : public CefDOMVisitor { EXPECT_TRUE(divNode.get()); EXPECT_TRUE(divNode->IsElement()); EXPECT_FALSE(divNode->IsText()); + + // Returned bounds are in device pixels. CefRect divRect = divNode->GetElementBounds(); - EXPECT_EQ(divRect.width, 50); - EXPECT_EQ(divRect.height, 25); - EXPECT_EQ(divRect.x, 150); - EXPECT_EQ(divRect.y, 100); + EXPECT_EQ(divRect.width, 50.0 * devicePixelRatio); + EXPECT_EQ(divRect.height, 25.0 * devicePixelRatio); + EXPECT_EQ(divRect.x, 150.0 * devicePixelRatio); + EXPECT_EQ(divRect.y, 100.0 * devicePixelRatio); + EXPECT_FALSE(divNode->GetNextSibling().get()); } @@ -166,8 +170,10 @@ class TestDOMVisitor : public CefDOMVisitor { CefRefPtr headNode = htmlNode->GetFirstChild(); TestHeadNodeStructure(headNode); + const float devicePixelRatio = GetDevicePixelRatio(); + CefRefPtr bodyNode = headNode->GetNextSibling(); - TestBodyNodeStructure(bodyNode); + TestBodyNodeStructure(bodyNode, devicePixelRatio); // Retrieve the head node directly. headNode = document->GetHead(); @@ -175,7 +181,7 @@ class TestDOMVisitor : public CefDOMVisitor { // Retrieve the body node directly. bodyNode = document->GetBody(); - TestBodyNodeStructure(bodyNode); + TestBodyNodeStructure(bodyNode, devicePixelRatio); } // Test document modification by changing the H1 tag. @@ -226,6 +232,28 @@ class TestDOMVisitor : public CefDOMVisitor { browser_->GetMainFrame()->SendProcessMessage(PID_BROWSER, return_msg); } + // Used to convert between device pixels and CSS pixels. + float GetDevicePixelRatio() { + auto context = browser_->GetMainFrame()->GetV8Context(); + EXPECT_TRUE(context); + + CefRefPtr retval; + CefRefPtr exception; + EXPECT_TRUE(context->Eval("window.devicePixelRatio", CefString(), 0, retval, + exception)); + if (exception) { + ADD_FAILURE() << exception->GetMessage().c_str(); + return 1.0; + } + + if (retval->IsValid() && retval->IsDouble()) { + return static_cast(retval->GetDoubleValue()); + } + + ADD_FAILURE() << "Failed to retrieve devicePixelRatio"; + return 1.0; + } + CefRefPtr browser_; DOMTestType test_type_; @@ -263,6 +291,7 @@ class TestDOMHandler : public TestHandler { explicit TestDOMHandler(DOMTestType test) : test_type_(test) {} void RunTest() override { + // Specified values are in CSS pixels. std::stringstream mainHtml; mainHtml << "" "The Title"