Skip to content

Commit

Permalink
fixed: detection of duplicate nodeName failed to find sibling duplica…
Browse files Browse the repository at this point in the history
…te leaf nodes as children of root,

#91
  • Loading branch information
MartijnR committed Jan 6, 2021
1 parent 1992477 commit 0449717
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
Expand Down
3 changes: 2 additions & 1 deletion test/spec/xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
19 changes: 11 additions & 8 deletions test/xform/duplicate-nodename.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:odk="http://www.opendatakit.org/xforms"
xmlns:orx="http://openrosa.org/xforms"
<h:html xmlns="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:jr="http://openrosa.org/javarosa"
xmlns:odk="http://www.opendatakit.org/xforms"
xmlns:orx="http://openrosa.org/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:head>
<h:title>data</h:title>
<model>
<instance>
<data id="data">
<a/>
<c/>
<!-- warning, not a repeat -->
<c/>
<g>
<!-- warning -->

<a/>
<!-- another warning -->
<g/>
Expand Down Expand Up @@ -45,4 +48,4 @@
</input>
</group>
</h:body>
</h:html>
</h:html>

0 comments on commit 0449717

Please sign in to comment.