From 3e12a8beee5d055e816bed301a03b24fef2516fb Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Fri, 6 Apr 2018 13:33:51 -0400 Subject: [PATCH] Detect and warn when duplicate ids exist and --verbose flag is present. Fixes gh-138 --- bin/ecmarkup.js | 0 src/Builder.ts | 20 +++++++++++++++++- test/duplicate-ids.html | 36 ++++++++++++++++++++++++++++++++ test/duplicate-ids.html.baseline | 36 ++++++++++++++++++++++++++++++++ test/duplicate-ids.js | 33 +++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) mode change 100644 => 100755 bin/ecmarkup.js create mode 100644 test/duplicate-ids.html create mode 100644 test/duplicate-ids.html.baseline create mode 100644 test/duplicate-ids.js diff --git a/bin/ecmarkup.js b/bin/ecmarkup.js old mode 100644 new mode 100755 diff --git a/src/Builder.ts b/src/Builder.ts index 138e96fd..a55462ee 100644 --- a/src/Builder.ts +++ b/src/Builder.ts @@ -1,5 +1,8 @@ import Spec from "./Spec"; import { Context } from './Context'; +import * as utils from './utils'; + +const nodeIds = new Set(); /*@internal*/ export default class Builder { @@ -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 { throw new Error('Builder not implemented'); } diff --git a/test/duplicate-ids.html b/test/duplicate-ids.html new file mode 100644 index 00000000..48ed7c7a --- /dev/null +++ b/test/duplicate-ids.html @@ -0,0 +1,36 @@ + + +

A

+ +

Sub A

+ + + Multiple examples are numbered similar to notes + +
+
+ +

Section A: Extras

+ + + + +
Column 1Column 2
ValueValue 2
+
+
+ +

Section A: Extras

+ + + + +
Column 1Column 2
ValueValue 2
+
+ + Multiple examples are numbered similar to notes + +
diff --git a/test/duplicate-ids.html.baseline b/test/duplicate-ids.html.baseline new file mode 100644 index 00000000..f29b5248 --- /dev/null +++ b/test/duplicate-ids.html.baseline @@ -0,0 +1,36 @@ + +
+ +

1A

+ +

1.1Sub A

+ +
Example (Informative): An example
+ Multiple examples are numbered similar to notes + +
+
+
+ +

2Section A: Extras

+
Table 1 (Informative): A Table Of Stuff
+ + + +
Column 1Column 2
ValueValue 2
+
+
+ +

3Section A: Extras

+
Table 2 (Informative): A Table Of Stuff
+ + + +
Column 1Column 2
ValueValue 2
+
+
Example (Informative): An example
+ Multiple examples are numbered similar to notes + +
+
+
\ No newline at end of file diff --git a/test/duplicate-ids.js b/test/duplicate-ids.js new file mode 100644 index 00000000..2d3cf757 --- /dev/null +++ b/test/duplicate-ids.js @@ -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(/ has duplicate id: sec-a/g).length, 3); + assert.equal(result.includes(' has duplicate id: table-of-stuff'), true); + assert.equal(result.includes(' 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(' has duplicate id: sec-a'), false); + assert.equal(result.includes(' has duplicate id: table-of-stuff'), false); + assert.equal(result.includes(' has duplicate id: an-example'), false); + done(); + }); + }); +});