From 0449717314e500358c4cee9651bc2ca26afb650d Mon Sep 17 00:00:00 2001 From: Martijn van de Rijdt Date: Wed, 6 Jan 2021 16:36:23 -0700 Subject: [PATCH] fixed: detection of duplicate nodeName failed to find sibling duplicate leaf nodes as children of root, https://github.com/enketo/enketo-validate/issues/91 --- CHANGELOG.md | 5 +++++ src/xform.js | 11 ++++++++--- test/spec/xform.spec.js | 3 ++- test/xform/duplicate-nodename.xml | 19 +++++++++++-------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59c9447..63f91d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +[ Unreleased ] +--------------------- +##### Fixed +- Detection of duplicate nodeName failed to find sibling duplicate leaf nodes as children of root. + [1.12.0] - 2020-10-16 --------------------- ##### Added diff --git a/src/xform.js b/src/xform.js index 9fd8c9f..bb845d4 100644 --- a/src/xform.js +++ b/src/xform.js @@ -414,9 +414,14 @@ class XForm { dataNodes.forEach( el => { const nodeName = el.nodeName; const index = dataNodeNames.indexOf( nodeName ); - // Save XPath determination for when necessary, to not negatively affect performance. - if ( index !== -1 && utils.getXPath( dataNodes[ index ], 'instance' ) !== utils.getXPath( el, 'instance' ) ) { - warnings.push( `Duplicate question or group name "${nodeName}" found. Unique names are recommended` ); + if ( index !== -1 ) { + // Save XPath determination for when necessary, to not negatively affect performance. + const path1 = utils.getXPath( dataNodes[ index ], 'instance' ); + const path2 = utils.getXPath( el, 'instance' ); + // Guess whether this could be repeat + if ( path1 !== path2 || ( path1.split( '/' ).length < 4 && el.children.length === 0 ) ) { + warnings.push( `Duplicate question or group name "${nodeName}" found. Unique names are recommended` ); + } } dataNodeNames.push( nodeName ); } ); diff --git a/test/spec/xform.spec.js b/test/spec/xform.spec.js index 44f1475..a9c1a0d 100644 --- a/test/spec/xform.spec.js +++ b/test/spec/xform.spec.js @@ -302,7 +302,8 @@ describe( 'XForm', () => { it( 'outputs warnings', async() => { const result = await validator.validate( loadXForm( 'duplicate-nodename.xml' ) ); - expect( result.warnings.length ).to.equal( 2 ); + expect( result.warnings.length ).to.equal( 3 ); + expect( arrContains( result.warnings, /Duplicate .* name "c" found/i ) ).to.equal( true ); expect( arrContains( result.warnings, /Duplicate .* name "a" found/i ) ).to.equal( true ); expect( arrContains( result.warnings, /Duplicate .* name "g" found/i ) ).to.equal( true ); expect( arrContains( result.warnings, /Duplicate .* name "b" found/i ) ).to.equal( false ); diff --git a/test/xform/duplicate-nodename.xml b/test/xform/duplicate-nodename.xml index 62146df..9844ad7 100644 --- a/test/xform/duplicate-nodename.xml +++ b/test/xform/duplicate-nodename.xml @@ -1,10 +1,10 @@ - data @@ -12,8 +12,11 @@ + + + - + @@ -45,4 +48,4 @@ - \ No newline at end of file +