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

Detect and warn when duplicate ids exist and --verbose flag is present. Fixes gh-138 #139

Merged
merged 1 commit into from
Apr 16, 2018
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
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>
33 changes: 33 additions & 0 deletions test/duplicate-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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;
}
// "sec-a" appears 4 times in the fixture, so there should be 3 warnings.
assert.equal(result.match(/<emu-clause> has duplicate id: sec-a/g).length, 3);
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-table> has duplicate id: table-of-stuff'), false);
assert.equal(result.includes('<emu-example> has duplicate id: an-example'), false);
done();
});
});
});