From 51a0f26d9730263aff195888978569928a1c1f24 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 13 Mar 2023 14:00:38 +0100 Subject: [PATCH] Fix bug when slide number >= 10 --- index.js | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a611d38..f31721b 100644 --- a/index.js +++ b/index.js @@ -235,7 +235,14 @@ async function main() { for (let pageIndex = 1;; pageIndex++) { - let root = await page.$('#\\3' + pageIndex); + let pageIndexStr = "" + pageIndex; + + let pageId = ""; + for (let c of pageIndexStr) { + pageId += "\\3" + c; + } + + let root = await page.$('#' + pageId); if (root === null) { break; } @@ -264,16 +271,19 @@ async function main() { await page.screenshot({path: (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot2.png'}); // Compare both screenshots - let file1 = await fs.readFile(__dirname + '/' + 'screenshot1.png'); - let file2 = await fs.readFile(__dirname + '/' + 'screenshot2.png'); + let file1 = await fs.readFile((outputDir === null ? __dirname : outputDir) + '/' + 'screenshot1.png'); + let file2 = await fs.readFile((outputDir === null ? __dirname : outputDir) + '/' + 'screenshot2.png'); let filesAreSame = file1.map((x, i) => x === file2[i]).reduce((a, b) => a && b, true); if (!filesAreSame) { // Check psnr - let psnr = await image.psnr(__dirname + '/' + 'screenshot1.png', __dirname + '/' + 'screenshot2.png'); + let psnr = await image.psnr( + (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot1.png', + (outputDir === null ? __dirname : outputDir) + '/' + 'screenshot2.png' + ); // Crash if they're different - if (psnr > 70) { + if (psnr > 0) { warning(filename + " produced slight diff: psnr = " + psnr); } else { await error("page edit changed the layout: psnr = " + psnr); @@ -370,10 +380,12 @@ async function analyseElement(element, page, outputDir = null, threshold = undef analyse.class = className; analyse.box = box; - box.x /= size.width; - box.width /= size.width; - box.y /= size.height; - box.height /= size.height; + if (box !== null) { + box.x /= size.width; + box.width /= size.width; + box.y /= size.height; + box.height /= size.height; + } if (outputDir !== null) { analyse.uuid = uuid(); @@ -412,12 +424,16 @@ async function analyseElement(element, page, outputDir = null, threshold = undef analyse.text = textContent; } - // Select the children of this HTML element. - let children = await element.$$('> *'); + if (tagName !== "svg") { + + // Select the children of this HTML element. + let children = await element.$$('> *'); + + for (let child of children) { + // Recursively analyse the children + analyse.children.push(await analyseElement(child, page, outputDir, threshold)); + } - for (let child of children) { - // Recursively analyse the children - analyse.children.push(await analyseElement(child, page, outputDir, threshold)); } return analyse;