Bug 1545726 - Implement DOM.getBoxModel. r=remote-protocol-reviewers,ato
authorAlexandre Poirot <poirot.alex@gmail.com>
Tue, 25 Jun 2019 19:34:05 +0000
changeset 480072 8d375a95f85ead88363a103ad3ec8d4f1d39ef5b
parent 480071 41f3dfea7b8f5d044caee9ccfd09845364fdc714
child 480073 e8310aa09588f45d285e4ee12af10fb4c52d566f
push id36201
push usercsabou@mozilla.com
push dateWed, 26 Jun 2019 03:57:29 +0000
treeherdermozilla-central@a3cad1d7836c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersremote-protocol-reviewers, ato
bugs1545726
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1545726 - Implement DOM.getBoxModel. r=remote-protocol-reviewers,ato Differential Revision: https://phabricator.services.mozilla.com/D30203
remote/domains/content/DOM.jsm
--- a/remote/domains/content/DOM.jsm
+++ b/remote/domains/content/DOM.jsm
@@ -51,9 +51,62 @@ class DOM extends ContentProcessDomain {
         quad.p1.x, quad.p1.y,
         quad.p2.x, quad.p2.y,
         quad.p3.x, quad.p3.y,
         quad.p4.x, quad.p4.y,
       ].map(Math.round);
     });
     return { quads };
   }
+
+  getBoxModel({ objectId }) {
+    const Runtime = this.session.domains.get("Runtime");
+    const obj = Runtime.getRemoteObject(objectId);
+    const unsafeObject = obj.unsafeDereference();
+    const bounding = unsafeObject.getBoundingClientRect();
+    const model = {
+      width: Math.round(bounding.width),
+      height: Math.round(bounding.height),
+    };
+    for (const box of ["content", "padding", "border", "margin"]) {
+      const quads = unsafeObject.getBoxQuads({box, relativeTo: this.content.document});
+
+      // getBoxQuads may return more than one element. In this case we have to compute the bounding box
+      // of all these boxes.
+      let bounding = {
+        p1: { x: Infinity, y: Infinity },
+        p2: { x: -Infinity, y: Infinity },
+        p3: { x: -Infinity, y: -Infinity },
+        p4: { x: Infinity, y: -Infinity },
+      };
+      quads.forEach(quad => {
+        bounding = {
+          p1: {
+            x: Math.min(bounding.p1.x, quad.p1.x),
+            y: Math.min(bounding.p1.y, quad.p1.y),
+          },
+          p2: {
+            x: Math.max(bounding.p2.x, quad.p2.x),
+            y: Math.min(bounding.p2.y, quad.p2.y),
+          },
+          p3: {
+            x: Math.max(bounding.p3.x, quad.p3.x),
+            y: Math.max(bounding.p3.y, quad.p3.y),
+          },
+          p4: {
+            x: Math.min(bounding.p4.x, quad.p4.x),
+            y: Math.max(bounding.p4.y, quad.p4.y),
+          },
+        };
+      });
+
+      model[box] = [
+        bounding.p1.x, bounding.p1.y,
+        bounding.p2.x, bounding.p2.y,
+        bounding.p3.x, bounding.p3.y,
+        bounding.p4.x, bounding.p4.y,
+      ].map(Math.round);
+    }
+    return {
+      model,
+    };
+  }
 }