Skip to content

Commit

Permalink
fix streams (getodk#1256)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Oct 30, 2024
1 parent ebfb355 commit adb5ee0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/data/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const submissionXmlToFieldStream = (fields, xml, includeStructuralAttrs = false,
const stack = new SchemaStack(fields, true);
let textBuffer = ''; // agglomerates text nodes that come as multiple events.
const parser = new hparser.Parser({
onend: () => {
if (!stack.hasExited()) throw new Error('Stack has not exited yet');

parser.reset();
outStream.push(null);
},
onopentag: (name, attrs) => {
const field = stack.push(name);
if (field != null) {
Expand All @@ -58,18 +64,15 @@ const submissionXmlToFieldStream = (fields, xml, includeStructuralAttrs = false,
textBuffer += text;
},
onclosetag: () => {
if (stack.hasExited()) throw new Error('Stack has already exited');

const field = stack.pop();

if (textBuffer !== '' || includeEmptyNodes) {
if ((field != null) && !field.isStructural()) // don't output useless whitespace
outStream.push({ field, text: textBuffer });
textBuffer = '';
}

if (stack.hasExited()) {
parser.reset();
outStream.push(null);
}
}
}, { xmlMode: true, decodeEntities: true });

Expand Down

0 comments on commit adb5ee0

Please sign in to comment.