Skip to content

Commit

Permalink
fix: Error when running HTML checks (HTML5Outline is not defined)
Browse files Browse the repository at this point in the history
EPUB scripts based on RequireJS were conflicting with the injected
HTML5 outliner library (h5o), and caused an unexpected error.
("HTML5Outline is not defined").

This fix wraps h5o in a function that undefines any global `define`
function, to prevent this conflict.

Fixes #108
  • Loading branch information
rdeltour committed Dec 13, 2017
1 parent 2fbac7e commit 03e3a3e
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 1 deletion.
29 changes: 29 additions & 0 deletions packages/ace-core/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const { config } = require('../../../scripts/build-utils');

function buildH5O() {
/**
* Wrap the browserified h5o in a function to temporarily undefine 'define'
* since Browserify and RequireJS otherwise don’t go along
* see: https://github.com/browserify/browserify/issues/790
*/
const vendorDir = path.resolve(__dirname, '..', config.buildDir, 'scripts/vendor');
const srcPath = path.resolve(require.resolve('h5o'), '../dist/outliner.min.js');
const destPath = path.resolve(vendorDir, 'outliner.min.js');

mkdirp.sync(path.dirname(destPath));
const ws = fs.createWriteStream(destPath);
const rs = fs.createReadStream(srcPath);
ws.write('(function () { var define = undefined;');
rs.pipe(ws, { end: false });
rs.on('end', () => {
ws.end('})();');
});
}

buildH5O();

2 changes: 1 addition & 1 deletion packages/ace-core/src/checker/checker-chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const utils = require('@daisy/puppeteer-utils');

const scripts = [
path.resolve(require.resolve('axe-core'), '../axe.min.js'),
path.resolve(require.resolve('h5o'), '../dist/outliner.min.js'),
require.resolve('../scripts/vendor/outliner.min.js'),
require.resolve('../scripts/axe-patch-getselector.js'),
require.resolve('../scripts/axe-patch-arialookuptable.js'),
require.resolve('../scripts/ace-axe.js'),
Expand Down
5 changes: 5 additions & 0 deletions tests/__tests__/regression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ test('issue #85: failed to detect page markers from `epub:type`', async () => {
expect(report['earl:result']['earl:outcome']).toEqual('pass');
expect(report.properties.hasPageBreaks).toBe(true);
});

test('issue #108: HTML5Outline is not defined (RequireJS conflict)', async () => {
const report = await ace('../data/issue-108');
expect(report['earl:result']['earl:outcome']).toEqual('pass');
});
10 changes: 10 additions & 0 deletions tests/data/issue-108/EPUB/content_001.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Minimal EPUB</title>
</head>
<body>
<script src="require.js"></script>
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
</body>
</html>
12 changes: 12 additions & 0 deletions tests/data/issue-108/EPUB/nav.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
</body>
</html>
24 changes: 24 additions & 0 deletions tests/data/issue-108/EPUB/package.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="uid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title">Minimal EPUB 3.0</dc:title>
<dc:language>en</dc:language>
<dc:identifier id="uid">NOID</dc:identifier>
<meta property="dcterms:modified">2017-01-01T00:00:01Z</meta>
<meta property="schema:accessibilityFeature">structuralNavigation</meta>
<meta property="schema:accessibilitySummary">everything OK!</meta>
<meta property="schema:accessibilityHazard">noFlashingHazard</meta>
<meta property="schema:accessibilityHazard">noSoundHazard</meta>
<meta property="schema:accessibilityHazard">noMotionSimulationHazard</meta>
<meta property="schema:accessMode">textual</meta>
<meta property="schema:accessModeSufficient">textual</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml" properties="scripted"/>
<item id="requirejs" href="require.js" media-type="text/javascript"/>
</manifest>
<spine>
<itemref idref="content_001"/>
</spine>
</package>
5 changes: 5 additions & 0 deletions tests/data/issue-108/EPUB/require.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/data/issue-108/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
1 change: 1 addition & 0 deletions tests/data/issue-108/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip

0 comments on commit 03e3a3e

Please sign in to comment.