Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the detection of epub:type-based page markers + Run script tests in headless Chrome #100

Merged
merged 3 commits into from
Oct 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-plugin-import": "^2.3.0",
"jest": "^21.1.0",
"jest": "21.3.0-beta.3",
"rimraf": "^2.6.1",
"standard-version": "^4.2.0",
"uglify-js": "^3.0.8",
Expand Down
75 changes: 11 additions & 64 deletions src/checker/checker-chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,26 @@ const fs = require('fs');
const path = require('path');
const pMap = require('p-map');
const puppeteer = require('puppeteer');
const axe2ace = require('../report/axe2ace.js');
const winston = require('winston');

const PATH_TO_AXE = path.join(path.dirname(require.resolve('axe-core')), 'axe.min.js');
if (!fs.existsSync(PATH_TO_AXE)) {
winston.verbose(PATH_TO_AXE);
throw new Error('Can’t find aXe');
}

const PATH_TO_H5O = path.join(path.dirname(require.resolve('h5o')), 'dist/outliner.min.js');
if (!fs.existsSync(PATH_TO_H5O)) {
winston.verbose(PATH_TO_H5O);
throw new Error('Can’t find h5o');
}

const PATH_TO_AXE_PATCH_GETSELECTOR = path.join(__dirname, '../scripts/axe-patch-getselector.js');
if (!fs.existsSync(PATH_TO_AXE_PATCH_GETSELECTOR)) {
winston.verbose(PATH_TO_AXE_PATCH_GETSELECTOR);
throw new Error('Can’t find axe-patch-getselector script');
}

const PATH_TO_AXE_PATCH_ARIALOOKUPTABLE = path.join(__dirname, '../scripts/axe-patch-arialookuptable.js');
if (!fs.existsSync(PATH_TO_AXE_PATCH_ARIALOOKUPTABLE)) {
winston.verbose(PATH_TO_AXE_PATCH_ARIALOOKUPTABLE);
throw new Error('Can’t find axe-patch-arialookuptable script');
}

const PATH_TO_ACE_AXE = path.join(__dirname, '../scripts/ace-axe.js');
if (!fs.existsSync(PATH_TO_ACE_AXE)) {
winston.verbose(PATH_TO_ACE_AXE);
throw new Error('Can’t find ace-axe script');
}
const axe2ace = require('../report/axe2ace.js');
const utils = require('./puppeteer-utils');

const PATH_TO_ACE_EXTRACTION = path.join(__dirname, '../scripts/ace-extraction.js');
if (!fs.existsSync(PATH_TO_ACE_EXTRACTION)) {
winston.verbose(PATH_TO_ACE_EXTRACTION);
throw new Error('Can’t find ace-extraction script');
}
const scripts = [
path.resolve(require.resolve('axe-core'), '../axe.min.js'),
path.resolve(require.resolve('h5o'), '../dist/outliner.min.js'),
require.resolve('../scripts/axe-patch-getselector.js'),
require.resolve('../scripts/axe-patch-arialookuptable.js'),
require.resolve('../scripts/ace-axe.js'),
require.resolve('../scripts/ace-extraction.js'),
];

async function checkSingle(spineItem, epub, browser) {
winston.verbose(`- Processing ${spineItem.relpath}`);
try {
const page = await browser.newPage();
await page.goto(spineItem.url);
// page.on('console', msg => console.log(msg.text));

// BEGIN HACK
// Used to differentiate original `script` elements from the one
// added by Puppeteer.
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => script.setAttribute('data-ace-orig', ''));
});
// END HACK
await page.addScriptTag({ path: PATH_TO_AXE });
await page.addScriptTag({ path: PATH_TO_AXE_PATCH_GETSELECTOR });
await page.addScriptTag({ path: PATH_TO_AXE_PATCH_ARIALOOKUPTABLE });
await page.addScriptTag({ path: PATH_TO_H5O });
await page.addScriptTag({ path: PATH_TO_ACE_AXE });
await page.addScriptTag({ path: PATH_TO_ACE_EXTRACTION });
// BEGIN HACK
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => {
if (script.hasAttribute('data-ace-orig')) {
script.removeAttribute('data-ace-orig');
} else {
script.setAttribute('data-ace', '');
}
});
});
// END HACK

await utils.addScripts(scripts, page);

const results = await page.evaluate(() => new Promise((resolve, reject) => {
/* eslint-disable */
Expand Down
32 changes: 32 additions & 0 deletions src/checker/puppeteer-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

async function addScripts(paths, page) {
// BEGIN HACK
// Used to differentiate original `script` elements from the one
// added by Puppeteer.
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach(script => script.setAttribute('data-ace-orig', ''));
});
/* eslint-disable no-restricted-syntax, no-await-in-loop */
for (const path of paths) {
await page.addScriptTag({ path });
}
/* eslint-enable no-restricted-syntax, no-await-in-loop */
// BEGIN HACK
// FIXME remove this hack when GoogleChrome/puppeteer#1179 is fixed
await page.$$eval('script', (scripts) => {
scripts.forEach((script) => {
if (script.hasAttribute('data-ace-orig')) {
script.removeAttribute('data-ace-orig');
} else {
script.setAttribute('data-ace', '');
}
});
});
// END HACK
}

module.exports = {
addScripts,
};
2 changes: 1 addition & 1 deletion src/scripts/ace-extraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,5 +242,5 @@ ace.hasMathML = function() {
}

ace.hasPageBreaks = function() {
return document.querySelectorAll('[epub\\:type~="pagebreak"], [role~="doc-pagebreak"]').length > 0;
return document.querySelectorAll('[*|type~="pagebreak"], [role~="doc-pagebreak"]').length > 0;
}
Loading