Skip to content

Commit

Permalink
Detect and warn when duplicate ids exist and --verbose flag is present.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Apr 6, 2018
1 parent aecb24e commit b90ffb4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
Empty file modified bin/ecmarkup.js
100644 → 100755
Empty file.
20 changes: 19 additions & 1 deletion src/Builder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Spec from "./Spec";
import { Context } from './Context';
import * as utils from './utils';

const nodeIds = new Set<string>();

/*@internal*/
export default class Builder {
Expand All @@ -9,8 +12,23 @@ export default class Builder {
constructor(spec: Spec, node: HTMLElement, ... args: any[]) {
this.spec = spec;
this.node = node;

let nodeId = node.getAttribute('id')!;

if (nodeId !== null) {
if (nodeIds.has(nodeId)) {
this._log(`<${node.tagName.toLowerCase()}> has duplicate id: ${nodeId}`);
}
nodeIds.add(nodeId);
}
}


/*@internal*/
_log(str: string) {
if (!this.spec.opts.verbose) return;
utils.logWarning(str);
}

static enter(context: Context): PromiseLike<void> | void {
throw new Error('Builder not implemented');
}
Expand Down
36 changes: 36 additions & 0 deletions test/duplicate-ids.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<pre class=metadata>
toc: false
copyright: false
assets: none
</pre>
<emu-clause id="sec-a">
<h1>A</h1>
<emu-clause id="sec-a">
<h1>Sub A</h1>

<emu-example id="an-example" caption="An example">
Multiple examples are numbered similar to notes
</emu-example>
</emu-clause>
</emu-clause>
<emu-clause id="sec-a">
<h1>Section A: Extras</h1>
<emu-table id="table-of-stuff" caption="A Table Of Stuff" informative>
<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Value</td><td>Value 2</td></tr>
</table>
</emu-table>
</emu-clause>
<emu-clause id="sec-a">
<h1>Section A: Extras</h1>
<emu-table id="table-of-stuff" caption="A Table Of Stuff" informative>
<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Value</td><td>Value 2</td></tr>
</table>
</emu-table>
<emu-example id="an-example" caption="An example">
Multiple examples are numbered similar to notes
</emu-example>
</emu-clause>
36 changes: 36 additions & 0 deletions test/duplicate-ids.html.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<head><meta charset="utf-8"></head><body><div id="spec-container">
<emu-clause id="sec-a">
<h1 class="first"><span class="secnum">1</span>A</h1>
<emu-clause id="sec-a">
<h1><span class="secnum">1.1</span>Sub A</h1>

<emu-example id="an-example" caption="An example"><figure><figcaption>Example (Informative): An example</figcaption>
Multiple examples are numbered similar to notes

</figure></emu-example>
</emu-clause>
</emu-clause>
<emu-clause id="sec-a">
<h1><span class="secnum">2</span>Section A: Extras</h1>
<emu-table id="table-of-stuff" caption="A Table Of Stuff" informative=""><figure><figcaption>Table 1 (Informative): A Table Of Stuff</figcaption>
<table>
<tbody><tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Value</td><td>Value 2</td></tr>
</tbody></table>
</figure></emu-table>
</emu-clause>
<emu-clause id="sec-a">
<h1><span class="secnum">3</span>Section A: Extras</h1>
<emu-table id="table-of-stuff" caption="A Table Of Stuff" informative=""><figure><figcaption>Table 2 (Informative): A Table Of Stuff</figcaption>
<table>
<tbody><tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Value</td><td>Value 2</td></tr>
</tbody></table>
</figure></emu-table>
<emu-example id="an-example" caption="An example"><figure><figcaption>Example (Informative): An example</figcaption>
Multiple examples are numbered similar to notes

</figure></emu-example>
</emu-clause>
</div></body>
36 changes: 36 additions & 0 deletions test/duplicate-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
const cp = require('child_process');
const assert = require('assert');

describe('detecting duplicate ids', () => {
it('reports when --verbose flag is present', (done) => {
cp.exec('./bin/ecmarkup.js --verbose test/duplicate-ids.html', (error, result) => {
if (error) {
assert.fail(error);
done();
return;
}
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), true);
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), true);
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), true);
assert.equal(result.includes('<emu-table> has duplicate id: table-of-stuff'), true);
assert.equal(result.includes('<emu-example> has duplicate id: an-example'), true);
done();
});
});
it('does not report when --verbose flag is not present', (done) => {
cp.exec('./bin/ecmarkup.js test/duplicate-ids.html', (error, result) => {
if (error) {
assert.fail(error);
done();
return;
}
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), false);
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), false);
assert.equal(result.includes('<emu-clause> has duplicate id: sec-a'), false);
assert.equal(result.includes('<emu-table> has duplicate id: table-of-stuff'), false);
assert.equal(result.includes('<emu-example> has duplicate id: an-example'), false);
done();
});
});
});

0 comments on commit b90ffb4

Please sign in to comment.