@@ -6,17 +6,149 @@
66 <script>
77 ${compiled_elm_code}
88 </script>
9+ <script>
10+ // Helpers
11+ // Calculations for canvas
12+ var PIXEL_RATIO = (function () {
13+ var ctx = document.createElement("canvas").getContext("2d"),
14+ dpr = window.devicePixelRatio || 1,
15+ bsr =
16+ ctx.webkitBackingStorePixelRatio ||
17+ ctx.mozBackingStorePixelRatio ||
18+ ctx.msBackingStorePixelRatio ||
19+ ctx.oBackingStorePixelRatio ||
20+ ctx.backingStorePixelRatio ||
21+ 1;
22+
23+ return dpr / bsr;
24+ })();
25+
26+ function createHiDPICanvas(w, h, id, ratio) {
27+ if (!ratio) {
28+ ratio = PIXEL_RATIO;
29+ }
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);
36+ return can;
37+ }
38+
39+ function isVisible(id, bbox) {
40+ var current = document.getElementById(id);
41+ var result = 0;
42+ if (current == document.elementFromPoint(bbox["left"], bbox["top"])) {
43+ result++;
44+ }
45+ if (
46+ current == document.elementFromPoint(bbox["left"], bbox["bottom"] - 1)
47+ ) {
48+ result++;
49+ }
50+ if (
51+ current == document.elementFromPoint(bbox["right"] - 1, bbox["top"])
52+ ) {
53+ result++;
54+ }
55+ if (
56+ current ==
57+ document.elementFromPoint(bbox["right"] - 1, bbox["bottom"] - 1)
58+ ) {
59+ result++;
60+ }
61+
62+ if (result == 4) {
63+ return true;
64+ } else {
65+ return false;
66+ }
67+ }
68+
69+ function getStyle(element) {
70+ var props = [];
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]);
76+ }
77+ return props;
78+ }
79+
80+ function getBoundingBox(element) {
81+ var bbox = element.getBoundingClientRect();
82+ var style = window.getComputedStyle(element, null);
83+
84+ var padding = {
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")),
89+ };
90+
91+ return {
92+ top: bbox.top,
93+ bottom: bbox.bottom,
94+ left: bbox.left,
95+ right: bbox.right,
96+ width: bbox.width,
97+ height: bbox.height,
98+ padding: padding,
99+ };
100+ }
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");
114+
115+ ctx.font = fontSize + " " + fontFamily; //"16px Roboto";
116+ let text = ctx.measureText(element.children[i].innerText);
117+
118+ childrenTextMetrics.push({
119+ actualBoundingBoxAscent: text.actualBoundingBoxAscent,
120+ actualBoundingBoxDescent: text.actualBoundingBoxDescent,
121+ actualBoundingBoxLeft: text.actualBoundingBoxLeft,
122+ actualBoundingBoxRight: text.actualBoundingBoxRight,
123+ width: text.width,
124+ });
125+ }
126+ }
127+ return childrenTextMetrics;
128+ }
129+ </script>
9130 </head>
10131 <style></style>
11− <body id="root"></body>
132+ <body id="root">
133+ <body id="root">
134+ <div id="elm-home"></div>
135+ <canvas id="canvas"></canvas>
136+ </body>
137+ </body>
12138
13139 <script type="text/javascript">
14− var app = Elm.Tests.Run.init();
140+ // Starting Elm App,
141+ var app = Elm.${module_name}.init({
142+ node: document.getElementById("elm-home"),
143+ });
144+
145+
146+ // create a canvas for text measurements
147+ let canvas = createHiDPICanvas(200, 100, "canvas");
15148
16149 var test_results = "waiting..";
17150
18151 app.ports.report.subscribe(function (results) {
19− // these are extracted by the script that runs the browser automation.
20152 test_results = results;
21153 });
22154
@@ -32,76 +164,20 @@
32164 }
33165 var style = getStyle(element);
34166 var bbox = getBoundingBox(element);
167+ var metrics = getTextMetrics(element);
35168
36169 var visible = isVisible(id, bbox);
37170
38− var result = { bbox: bbox, style: style, id: id, isVisible: visible };
171+ var result = {
172+ textMetrics: metrics,
173+ bbox: bbox,
174+ style: style,
175+ id: id,
176+ isVisible: visible,
177+ };
39178 results.push(result);
40179 }
41180 app.ports.styles.send(results);
42181 });
43−
44− function isVisible(id, bbox) {
45− var current = document.getElementById(id);
46− var result = 0;
47− if (current == document.elementFromPoint(bbox["left"], bbox["top"])) {
48− result++;
49− }
50− if (
51− current == document.elementFromPoint(bbox["left"], bbox["bottom"] - 1)
52− ) {
53− result++;
54− }
55− if (
56− current == document.elementFromPoint(bbox["right"] - 1, bbox["top"])
57− ) {
58− result++;
59− }
60− if (
61− current ==
62− document.elementFromPoint(bbox["right"] - 1, bbox["bottom"] - 1)
63− ) {
64− result++;
65− }
66−
67− if (result == 4) {
68− return true;
69− } else {
70− return false;
71− }
72− }
73−
74− function getStyle(element) {
75− var props = [];
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]);
81− }
82− return props;
83− }
84−
85− function getBoundingBox(element) {
86− var bbox = element.getBoundingClientRect();
87− var style = window.getComputedStyle(element, null);
88−
89− var padding = {
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")),
94− };
95−
96− return {
97− top: bbox.top,
98− bottom: bbox.bottom,
99− left: bbox.left,
100− right: bbox.right,
101− width: bbox.width,
102− height: bbox.height,
103− padding: padding,
104− };
105− }
106182 </script>
107183</html>