Skip to content

Commit

Permalink
fix: multiple dc:source caused an unexpected error
Browse files Browse the repository at this point in the history
Closes #49
  • Loading branch information
rdeltour committed Oct 4, 2017
1 parent 53f5571 commit c719977
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/checker/checker-epub.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const ASSERTED_BY = 'Ace';
const MODE = 'automatic';
const KB_BASE = 'https://daisy.github.io/a11y-kb/';

function asString(arrayOrString) {
if (Array.isArray(arrayOrString) && arrayOrString.length > 0) {
return asString(arrayOrString[0]);
} else if (arrayOrString !== undefined) {
return arrayOrString.toString().trim();
}
return '';
}

function newViolation({ impact = 'serious', title, testDesc, resDesc, kbPath, kbTitle }) {
return new builders.AssertionBuilder()
.withAssertedBy(ASSERTED_BY)
Expand Down Expand Up @@ -54,8 +63,8 @@ function checkMetadata(assertions, epub) {
}

function checkTitle(assertions, epub) {
const title = epub.metadata['dc:title'];
if (title === undefined || title.trim() === '') {
const title = asString(epub.metadata['dc:title']);
if (title === '') {
assertions.withAssertions(newViolation({
title: 'epub-title',
testDesc: 'Ensures the EPUB has a title',
Expand All @@ -69,7 +78,7 @@ function checkTitle(assertions, epub) {
function checkPageSource(assertion, epub) {
if (epub.navDoc.hasPageList
&& (epub.metadata['dc:source'] === undefined
|| epub.metadata['dc:source'].trim() === '')) {
|| epub.metadata['dc:source'].toString() === '')) {
assertion.withAssertions(newViolation({
title: 'epub-pagesource',
testDesc: 'Ensures the source of page breaks is identified',
Expand All @@ -84,9 +93,7 @@ function check(epub, report) {
winston.info('Checking package...');
const assertion = new builders.AssertionBuilder()
.withSubAssertions()
.withTestSubject(
epub.packageDoc.src,
(epub.metadata['dc:title'] !== undefined) ? epub.metadata['dc:title'] : '');
.withTestSubject(epub.packageDoc.src, asString(epub.metadata['dc:title']));

// Check a11y metadata
checkMetadata(assertion, epub);
Expand Down
42 changes: 42 additions & 0 deletions tests/__tests__/parsing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const fs = require('fs');
const path = require('path');
const tmp = require('tmp');

const runAce = require('../runAceJS');

tmp.setGracefulCleanup();

let outdir;
let tmpdir;
let reportPath;

beforeEach(() => {
outdir = tmp.dirSync({ prefix: 'ace_out_', unsafeCleanup: true });
tmpdir = tmp.dirSync({ prefix: 'ace_tmp_', unsafeCleanup: true });
reportPath = path.join(outdir.name, 'ace.json');
});

afterEach(() => {
outdir.removeCallback();
tmpdir.removeCallback();
});


function ace(epub, options = {}) {
return runAce(path.join(__dirname, epub), Object.assign({
outdir: outdir.name,
tmp: tmpdir.name,
}, options))
.then(() => {
expect(fs.existsSync(reportPath)).toBeTruthy();
return JSON.parse(fs.readFileSync(reportPath, 'utf8'));
})
.catch(err => console.log(err));
}

test('issue #49: multiple \'dc:title\' elements', async () => {
const report = await ace('../data/issue49');
expect(report['earl:result']['earl:outcome']).toEqual('pass');
});
9 changes: 9 additions & 0 deletions tests/data/issue49/EPUB/content_001.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<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>
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
</body>
</html>
12 changes: 12 additions & 0 deletions tests/data/issue49/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>
25 changes: 25 additions & 0 deletions tests/data/issue49/EPUB/package.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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="t1">Minimal EPUB 3.0</dc:title>
<dc:title id="t2">Other title</dc:title>
<meta refines="#t2" property="title-type">subtitle</meta>
<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"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
6 changes: 6 additions & 0 deletions tests/data/issue49/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/issue49/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip

0 comments on commit c719977

Please sign in to comment.