[HtmlUnit] SVN: [15545] trunk/htmlunit/src

rbri--- via HtmlUnit-develop <[email protected]> Mon, 27 Aug 2018 18:41:25 +0000
Newsgroups gmane.comp.java.htmlunit.devel
Message-ID <[email protected]>
Revision: 15545
          http://sourceforge.net/p/htmlunit/code/15545
Author:   rbri
Date:     2018-08-27 18:41:21 +0000 (Mon, 27 Aug 2018)
Log Message:
-----------
improved calculation of the left offset

Issue 1986

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml	2018-08-24 18:34:16 UTC (rev 15544)
+++ trunk/htmlunit/src/changes/changes.xml	2018-08-27 18:41:21 UTC (rev 15545)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.33" date="xxxx, 2018" description="Bugfixes">
+            <action type="update" dev="rbri" issue="1986">
+                Improved calculation of the left offset.
+            </action>
             <action type="update" dev="rbri">
                 We are using the typed array implementation from Rhino now, our own impl is gone.
             </action>

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java	2018-08-24 18:34:16 UTC (rev 15544)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclaration.java	2018-08-27 18:41:21 UTC (rev 15545)
@@ -131,6 +131,7 @@
 import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement;
 
 import net.sourceforge.htmlunit.corejs.javascript.Context;
+import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
 
 /**
  * An object for a CSSStyleDeclaration, which is computed.
@@ -1504,9 +1505,11 @@
         else if (STATIC.equals(p)) {
             // We need to calculate the horizontal displacement caused by *previous* siblings.
             left = 0;
-            for (DomNode n = getDomNodeOrDie(); n != null; n = n.getPreviousSibling()) {
-                if (n.getScriptableObject() instanceof HTMLElement) {
-                    final HTMLElement e = n.getScriptableObject();
+            DomNode prev = getElement().getDomNodeOrDie().getPreviousSibling();
+            while (prev != null) {
+                final Scriptable prevScriptable = prev.getScriptableObject();
+                if (prevScriptable instanceof HTMLElement) {
+                    final HTMLElement e = (HTMLElement) prevScriptable;
                     final ComputedCSSStyleDeclaration style = e.getWindow().getComputedStyle(e, null);
                     final String d = style.getDisplay();
                     if ("block".equals(d)) {
@@ -1516,12 +1519,16 @@
                         left += style.getCalculatedWidth(true, true);
                     }
                 }
-                else if (n.getScriptableObject() instanceof Text) {
-                    left += n.getTextContent().length() * getBrowserVersion().getPixesPerChar();
+                else if (prevScriptable instanceof Text) {
+                    final String content = prev.getTextContent();
+                    if (content != null) {
+                        left += content.trim().length() * getBrowserVersion().getPixesPerChar();
+                    }
                 }
-                if (n instanceof HtmlTableRow) {
+                if (prev instanceof HtmlTableRow) {
                     break;
                 }
+                prev = prev.getPreviousSibling();
             }
         }
         else {
@@ -1857,5 +1864,4 @@
         }
         return pixelValue(element, value) + "px";
     }
-
 }

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java	2018-08-24 18:34:16 UTC (rev 15544)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/css/ComputedCSSStyleDeclarationTest.java	2018-08-27 18:41:21 UTC (rev 15545)
@@ -2202,4 +2202,24 @@
 
         loadPageWithAlerts2(html);
     }
+
+    /**
+     * @throws Exception if an error occurs
+     */
+    @Test
+    @Alerts("true")
+    public void boundingClientRectIgnoreSiblingWhitespace() throws Exception {
+        final String html = "<html><body>\n"
+            + "<table>\n"
+            + "<tr>\n"
+            + "  <td>  \n\t    <div id='a'>a</div></td>\n"
+            + "</tr>\n"
+            + "</table>\n"
+            + "<script>\n"
+            + "  var e = document.getElementById('a');\n"
+            + "  alert(e.getBoundingClientRect().left < 12);\n"
+            + "</script>\n"
+            + "</body></html>";
+        loadPageWithAlerts2(html);
+    }
 }


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot