getBoundingClientRect always returns empty rect on a collapsed range
authorrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 29 Mar 2019 01:56:51 +0000 (01:56 +0000)
committerrniwa@webkit.org <rniwa@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 29 Mar 2019 01:56:51 +0000 (01:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196380

Reviewed by Wenson Hsieh.

Source/WebCore:

The bug was caused by Range::boundingRect merging rects via FloatRect::unite which ignores empty rects.
Use uniteIfNonZero instead to fix the bug. Note that we can't use uniteEvenIfEmpty because that would
set x, y to always 0, 0 as we would end up merging any rect with the initial empty rect.

Test: fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html

* dom/Range.cpp:
(WebCore::Range::boundingRect const):

LayoutTests:

Added a regression test.

* fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range-expected.txt: Added.
* fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@243635 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range-expected.txt [new file with mode: 0644]
LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/Range.cpp

index 3ed84d9..f95eb2c 100644 (file)
@@ -1,3 +1,15 @@
+2019-03-28  Ryosuke Niwa  <rniwa@webkit.org>
+
+        getBoundingClientRect always returns empty rect on a collapsed range
+        https://bugs.webkit.org/show_bug.cgi?id=196380
+
+        Reviewed by Wenson Hsieh.
+
+        Added a regression test.
+
+        * fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range-expected.txt: Added.
+        * fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html: Added.
+
 2019-03-28  Chris Dumez  <cdumez@apple.com>
 
         Re-sync web-platform-tests/html/browsers/the-window-object/ from upstream
diff --git a/LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range-expected.txt b/LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range-expected.txt
new file mode 100644 (file)
index 0000000..a23df7d
--- /dev/null
@@ -0,0 +1,15 @@
+hello
+This tests calling getBoundingClientRect on a selection range when it is collapsed.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS rect = getSelection().getRangeAt(0).getBoundingClientRect(); rect.x is 10
+PASS rect.x is 10
+PASS rect.y > 5 is true
+PASS rect.width is 0
+PASS rect.height > 15 is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html b/LayoutTests/fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html
new file mode 100644 (file)
index 0000000..a5c9cbf
--- /dev/null
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html>
+<body>
+<div id="target">hello</div>
+<p id="description"></p>
+<div id="console"></div>
+<style>
+html, body { margin: 0; padding: 0; }
+body { margin: 10px; }
+#target { font-size: 20px; line-height: 100%; }
+</style>
+<script src="../../../resources/js-test.js"></script>
+<script>
+
+description('This tests calling getBoundingClientRect on a selection range when it is collapsed.');
+
+getSelection().setBaseAndExtent(target.lastChild, 0, target.lastChild, 0);
+
+shouldBe('rect = getSelection().getRangeAt(0).getBoundingClientRect(); rect.x', '10');
+shouldBe('rect.x', '10');
+shouldBeTrue('rect.y > 5');
+shouldBe('rect.width', '0');
+shouldBeTrue('rect.height > 15');
+
+successfullyParsed = true;
+
+</script>
+</body>
+</html>
index 7642502..9028171 100644 (file)
@@ -1,3 +1,19 @@
+2019-03-28  Ryosuke Niwa  <rniwa@webkit.org>
+
+        getBoundingClientRect always returns empty rect on a collapsed range
+        https://bugs.webkit.org/show_bug.cgi?id=196380
+
+        Reviewed by Wenson Hsieh.
+
+        The bug was caused by Range::boundingRect merging rects via FloatRect::unite which ignores empty rects.
+        Use uniteIfNonZero instead to fix the bug. Note that we can't use uniteEvenIfEmpty because that would
+        set x, y to always 0, 0 as we would end up merging any rect with the initial empty rect.
+
+        Test: fast/dom/Range/getBoundingClientRect-on-collapsed-selection-range.html
+
+        * dom/Range.cpp:
+        (WebCore::Range::boundingRect const):
+
 2019-03-28  Justin Fan  <justin_fan@apple.com>
 
         [Web GPU] Prototype compute pipeline with MSL
index e7ed255..73db669 100644 (file)
@@ -1842,7 +1842,7 @@ FloatRect Range::boundingRect(CoordinateSpace space, RespectClippingForTextRects
 {
     FloatRect result;
     for (auto& rect : borderAndTextRects(space, respectClippingForTextRects))
-        result.unite(rect);
+        result.uniteIfNonZero(rect);
     return result;
 }