@@ -6,17 +6,149 @@
7 ${compiled_elm_code}
7 ${compiled_elm_code}
11+ // Calculations for canvas
12+ var PIXEL_RATIO = (function () {
13+ var ctx = document.createElement("canvas").getContext("2d"),
14+ dpr = window.devicePixelRatio || 1,
16+ ctx.webkitBackingStorePixelRatio ||
17+ ctx.mozBackingStorePixelRatio ||
18+ ctx.msBackingStorePixelRatio ||
19+ ctx.oBackingStorePixelRatio ||
20+ ctx.backingStorePixelRatio ||
26+ function createHiDPICanvas(w, h, id, ratio) {
30+ let can = document.getElementById(id);
31+ can.width = w * ratio;
32+ can.height = h * ratio;
33+ can.style.width = w + "px";
34+ can.style.height = h + "px";
35+ can.getContext("2d").setTransform(ratio, 0, 0, ratio, 0, 0);
39+ function isVisible(id, bbox) {
40+ var current = document.getElementById(id);
42+ if (current == document.elementFromPoint(bbox["left"], bbox["top"])) {
46+ current == document.elementFromPoint(bbox["left"], bbox["bottom"] - 1)
51+ current == document.elementFromPoint(bbox["right"] - 1, bbox["top"])
57+ document.elementFromPoint(bbox["right"] - 1, bbox["bottom"] - 1)
69+ function getStyle(element) {
71+ var style = window.getComputedStyle(element);
72+ for (var i = style.length; i--; ) {
73+ var name = style.item(i);
74+ var value = style.getPropertyValue(name);
75+ props.push([name, value]);
80+ function getBoundingBox(element) {
81+ var bbox = element.getBoundingClientRect();
82+ var style = window.getComputedStyle(element, null);
85+ top: parseFloat(style.getPropertyValue("padding-top")),
86+ bottom: parseFloat(style.getPropertyValue("padding-bottom")),
87+ left: parseFloat(style.getPropertyValue("padding-left")),
88+ right: parseFloat(style.getPropertyValue("padding-right")),
101+ function getTextMetrics(element) {
102+ // Given our element, we want to return the text metrics on the text within.
103+ // If it is an element we want to measure:
104+ // we want to get the font family
105+ // and the font size.
106+ // We're going to presume that the font is loaded.
107+ let childrenTextMetrics = [];
108+ for (i = 0; i < element.children.length; i++) {
109+ if (element.children[i].classList.contains("t")) {
110+ let style = window.getComputedStyle(element, null);
111+ let fontSize = style.getPropertyValue("font-size");
112+ let fontFamily = style.getPropertyValue("font-family");
113+ let ctx = canvas.getContext("2d");
115+ ctx.font = fontSize + " " + fontFamily; //"16px Roboto";
116+ let text = ctx.measureText(element.children[i].innerText);
118+ childrenTextMetrics.push({
119+ actualBoundingBoxAscent: text.actualBoundingBoxAscent,
120+ actualBoundingBoxDescent: text.actualBoundingBoxDescent,
121+ actualBoundingBoxLeft: text.actualBoundingBoxLeft,
122+ actualBoundingBoxRight: text.actualBoundingBoxRight,
127+ return childrenTextMetrics;
10 <style></style>
131 <style></style>
11− <body id="root"></body>
132+ <body id="root">
134+ <div id="elm-home"></div>
135+ <canvas id="canvas"></canvas>
13 <script type="text/javascript">
139 <script type="text/javascript">
140+ // Starting Elm App,
14− var app = Elm.Tests.Run.init();
141+ var app = Elm.${module_name}.init({
142+ node: document.getElementById("elm-home"),
146+ // create a canvas for text measurements
147+ let canvas = createHiDPICanvas(200, 100, "canvas");
16 var test_results = "waiting..";
149 var test_results = "waiting..";
18 app.ports.report.subscribe(function (results) {
151 app.ports.report.subscribe(function (results) {
19− // these are extracted by the script that runs the browser automation.
20 test_results = results;
152 test_results = results;
@@ -32,76 +164,20 @@
33 var style = getStyle(element);
165 var style = getStyle(element);
34 var bbox = getBoundingBox(element);
166 var bbox = getBoundingBox(element);
167+ var metrics = getTextMetrics(element);
36 var visible = isVisible(id, bbox);
169 var visible = isVisible(id, bbox);
172+ textMetrics: metrics,
38− var result = { bbox: bbox, style: style, id: id, isVisible: visible };
176+ isVisible: visible,
39 results.push(result);
178 results.push(result);
41 app.ports.styles.send(results);
180 app.ports.styles.send(results);
44− function isVisible(id, bbox) {
45− var current = document.getElementById(id);
47− if (current == document.elementFromPoint(bbox["left"], bbox["top"])) {
51− current == document.elementFromPoint(bbox["left"], bbox["bottom"] - 1)
56− current == document.elementFromPoint(bbox["right"] - 1, bbox["top"])
62− document.elementFromPoint(bbox["right"] - 1, bbox["bottom"] - 1)
74− function getStyle(element) {
76− var style = window.getComputedStyle(element);
77− for (var i = style.length; i--; ) {
78− var name = style.item(i);
79− var value = style.getPropertyValue(name);
80− props.push([name, value]);
85− function getBoundingBox(element) {
86− var bbox = element.getBoundingClientRect();
87− var style = window.getComputedStyle(element, null);
90− top: parseFloat(style.getPropertyValue("padding-top")),
91− bottom: parseFloat(style.getPropertyValue("padding-bottom")),
92− left: parseFloat(style.getPropertyValue("padding-left")),
93− right: parseFloat(style.getPropertyValue("padding-right")),
102− height: bbox.height,
106 </script>
182 </script>