Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
fixed: recursive loop in getIndex does not work for > double-nested r…
Browse files Browse the repository at this point in the history
…epeat info elements, closes #720
  • Loading branch information
MartijnR committed May 19, 2021
1 parent 717ee3e commit 58d8043
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
16 changes: 3 additions & 13 deletions src/js/repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,17 @@ export default {
return 0;
}

// get repeatSeriesIndex using parent repeat element
// this fixes a nested repeats error https://github.com/enketo/enketo-core/issues/720
const repeatPath = el.dataset.name;
if( repeatPath && repeatPath.split( '/' ).length - 1 > 3 && el ){
let parentRepeatElement = el.closest( `.or-repeat[name='${repeatPath.slice( 0, repeatPath.lastIndexOf( '/' ) )}']` );
if( parentRepeatElement && parentRepeatElement.parentElement ){
el = parentRepeatElement;
}
}

let checkEl = el.parentElement.closest( '.or-repeat' );
const info = el.classList.contains( 'or-repeat-info' );
let count = info ? 1 : Number( el.querySelector( '.repeat-number' ).textContent );
let name;
const name = el.dataset.name || el.getAttribute( 'name' );

while ( checkEl ) {
while ( checkEl.previousElementSibling && checkEl.previousElementSibling.matches( '.or-repeat' ) ) {
checkEl = checkEl.previousElementSibling;
if ( info ) {
count++;
count += checkEl.querySelectorAll( `.or-repeat-info[data-name="${name}"]` ).length;
} else {
name = name || el.getAttribute( 'name' );
count += checkEl.querySelectorAll( `.or-repeat[name="${name}"]` ).length;
}
}
Expand Down
6 changes: 3 additions & 3 deletions test/spec/repeat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe( 'repeat functionality', () => {

// https://github.com/enketo/enketo-core/issues/720
it( 'nested repeats are added correctly', () => {
const form = loadForm( 'nested-repeats.xml');
const form = loadForm( 'nested-repeats.xml' );
form.init();

// add repeats by clicking the add buttons
Expand All @@ -88,8 +88,8 @@ describe( 'repeat functionality', () => {
// check that we have the correct html
expect(
form.view.html.querySelectorAll( '[data-itext-id="/data/group1/repeat1/repeat11/repeat111/school_roomtype:label"]' ).length
).toEqual(4)
})
).toEqual( 4 );
} );
} );

describe( 'fixes unique ids in cloned repeats', () => {
Expand Down

0 comments on commit 58d8043

Please sign in to comment.