Quantcast

[HtmlUnit] SVN: [14366] trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[HtmlUnit] SVN: [14366] trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit

asashour-2
Revision: 14366
          http://sourceforge.net/p/htmlunit/code/14366
Author:   asashour
Date:     2017-05-04 20:38:26 +0000 (Thu, 04 May 2017)
Log Message:
-----------
Range.getBoundingClientRect

Modified Paths:
--------------
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/impl/SimpleRange.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Range.java

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/impl/SimpleRange.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/impl/SimpleRange.java 2017-05-04 20:35:08 UTC (rev 14365)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/impl/SimpleRange.java 2017-05-04 20:38:26 UTC (rev 14366)
@@ -235,7 +235,9 @@
         if (isOffsetChars(startContainer_)) {
             start = (DomNode) startContainer_;
             String text = getText(start);
-            text = text.substring(0, startOffset_);
+            if (startOffset_ < text.length()) {
+                text = text.substring(0, startOffset_);
+            }
             setText(start, text);
         }
         else if (startContainer_.getChildNodes().getLength() > startOffset_) {
@@ -247,7 +249,9 @@
         if (isOffsetChars(endContainer_)) {
             end = (DomNode) endContainer_;
             String text = getText(end);
-            text = text.substring(endOffset_);
+            if (endOffset_ < text.length()) {
+                text = text.substring(endOffset_);
+            }
             setText(end, text);
         }
         else if (endContainer_.getChildNodes().getLength() > endOffset_) {

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2017-05-04 20:35:08 UTC (rev 14365)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/Element.java 2017-05-04 20:38:26 UTC (rev 14366)
@@ -703,7 +703,7 @@
      * @return a collection of rectangles that describes the layout of the contents
      */
     @JsxFunction
-    public Object getClientRects() {
+    public ClientRectList getClientRects() {
         final ClientRectList rectList = new ClientRectList();
         rectList.setParentScope(getWindow());
         rectList.setPrototype(getPrototype(rectList.getClass()));

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Range.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Range.java 2017-05-04 20:35:08 UTC (rev 14365)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/dom/Range.java 2017-05-04 20:38:26 UTC (rev 14366)
@@ -491,7 +491,7 @@
      * @return a collection of rectangles that describes the layout of the contents
      */
     @JsxFunction
-    public Object getClientRects() {
+    public ClientRectList getClientRects() {
         final ClientRectList rectList = new ClientRectList();
         rectList.setParentScope(getWindow());
         rectList.setPrototype(getPrototype(rectList.getClass()));
@@ -510,4 +510,31 @@
 
         return rectList;
     }
+
+    /**
+     * Returns an object that bounds the contents of the range.
+     * this a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
+     * @return an object the bounds the contents of the range
+     */
+    @JsxFunction
+    public ClientRect getBoundingClientRect() {
+        final ClientRect rect = new ClientRect();
+        rect.setParentScope(getWindow());
+        rect.setPrototype(getPrototype(rect.getClass()));
+
+        // simple impl for now
+        final DomDocumentFragment fragment = (DomDocumentFragment) toW3C().extractContents();
+        for (DomNode node : fragment.getDescendants()) {
+            final ScriptableObject scriptable = node.getScriptableObject();
+            if (scriptable instanceof HTMLElement) {
+                final ClientRect childRect = ((HTMLElement) scriptable).getBoundingClientRect();
+                rect.setTop(Math.min(rect.getTop(), childRect.getTop()));
+                rect.setLeft(Math.min(rect.getLeft(), childRect.getLeft()));
+                rect.setRight(Math.max(rect.getRight(), childRect.getRight()));
+                rect.setBottom(Math.max(rect.getBottom(), childRect.getBottom()));
+            }
+        }
+
+        return rect;
+    }
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop
Loading...