-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathplot.js
More file actions
170 lines (155 loc) · 5.85 KB
/
plot.js
File metadata and controls
170 lines (155 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {promises as fs} from "fs";
import {createCanvas, loadImage} from "canvas";
import {max, mean, quantile} from "d3";
import * as path from "path";
import beautify from "js-beautify";
import {setOffset} from "../src/style.js";
import assert from "./assert.js";
import it from "./jsdom.js";
import * as plots from "./plots/index.ts"; // TODO index.js
setOffset(0.5);
for (const [name, plot] of Object.entries(plots)) {
it(`plot ${name}`, async () => {
const root = await (name.startsWith("warn") ? assert.warnsAsync : assert.doesNotWarnAsync)(plot);
const ext = root.tagName === "svg" ? "svg" : "html";
for (const svg of root.tagName === "svg" ? [root] : root.querySelectorAll("svg")) {
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "http://www.w3.org/2000/svg");
svg.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xlink", "http://www.w3.org/1999/xlink");
}
reindexStyle(root);
reindexMarker(root);
reindexClip(root);
reindexPattern(root);
let expected;
let actual = normalizeHtml(root.outerHTML);
const outfile = path.resolve("./test/output", `${path.basename(name, ".js")}.${ext}`);
const diffile = path.resolve("./test/output", `${path.basename(name, ".js")}-changed.${ext}`);
try {
expected = normalizeHtml(await fs.readFile(outfile, "utf8")); // TODO remove after regenerating snapshots
} catch (error) {
if (error.code === "ENOENT" && process.env.CI !== "true") {
console.warn(`! generating ${outfile}`);
await fs.writeFile(outfile, actual, "utf8");
return;
} else {
throw error;
}
}
// node-canvas won’t produce the same output on different architectures, so
// we parse and compare pixel values instead of the encoded output.
const equal = stripImages(actual) === stripImages(expected) && (await compareImages(actual, expected));
if (equal) {
if (process.env.CI !== "true") {
try {
await fs.unlink(diffile);
console.warn(`! deleted ${diffile}`);
} catch (error) {
if (error.code !== "ENOENT") {
throw error;
}
}
}
} else {
console.warn(`! generating ${diffile}`);
await fs.writeFile(diffile, actual, "utf8");
}
assert.ok(equal, `${name} must match snapshot`);
});
}
function normalizeHtml(html) {
return beautify.html(
html
.replace(/ /g, "\xa0") // normalize HTML entities
.replace(/\d+\.\d{4,}/g, (d) => +(+d).toFixed(3)), // limit numerical precision
{
indent_size: 2,
inline: ["title", "tspan", "span", "svg", "a", "i"],
indent_inner_html: false
}
);
}
function reindexStyle(root) {
const uid = "plot-d6a7b5"; // see defaultClassName
for (const style of root.querySelectorAll("style")) {
const parent = style.parentNode;
for (const child of [parent, ...parent.querySelectorAll("[class]")]) {
child.setAttribute("class", child.getAttribute("class").replace(new RegExp(`\\b${uid}\\b`, "g"), "plot"));
}
style.textContent = style.textContent.replace(new RegExp(`[.]${uid}\\b`, "g"), `.plot`);
}
}
function reindexMarker(root) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-marker-]")) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-marker-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["marker-start", "marker-mid", "marker-end"]) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
}
}
}
function reindexClip(root) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-clip-]")) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-clip-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["clip-path"]) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
}
}
}
function reindexPattern(root) {
let index = 0;
const map = new Map();
for (const node of root.querySelectorAll("[id^=plot-pattern-]")) {
let id = node.getAttribute("id");
if (map.has(id)) id = map.get(id);
else map.set(id, (id = `plot-pattern-${++index}`));
node.setAttribute("id", id);
}
for (const key of ["fill", "stroke"]) {
for (const node of root.querySelectorAll(`[${key}]`)) {
let id = node.getAttribute(key).slice(5, -1);
if (map.has(id)) node.setAttribute(key, `url(#${map.get(id)})`);
}
}
}
const imageRe = /data:image\/png;base64,[^"]+/g;
function stripImages(string) {
return string.replace(imageRe, "<replaced>");
}
async function compareImages(a, b) {
const reA = new RegExp(imageRe, "g");
const reB = new RegExp(imageRe, "g");
let matchA;
let matchB;
while (((matchA = reA.exec(a)), (matchB = reB.exec(b)))) {
const [imageA, imageB] = await Promise.all([getImageData(matchA[0]), getImageData(matchB[0])]);
const {width, height} = imageA;
if (width !== imageB.width || height !== imageB.height) return false;
const E = imageA.data.map((a, i) => Math.abs(a - imageB.data[i]));
if (!(quantile(E, 0.95) <= 1)) return false; // at least 95% with almost no error
if (!(mean(E) < 0.1)) return false; // no more than 0.1 average error
if (!(max(E) < 10)) return false; // no more than 10 maximum error
}
return true;
}
async function getImageData(url) {
const image = await loadImage(url);
const canvas = createCanvas(image.width, image.height);
const context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
return context.getImageData(0, 0, image.width, image.height);
}