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

Commit

Permalink
Fix: regression when no media mapping provided
Browse files Browse the repository at this point in the history
The change introduced in #150 causes errors/inconsistent URL-escaping behavior when:

- No media mapping is provided by the survey
- A survey includes binary defaults
- A survey references a `jr:` URL in markdown

This fix restores the previous behavior, but incorporates the fix from #150 to prevent incorrectly injecting paragraphs into markdown fragments.
  • Loading branch information
eyelidlessness authored and lognaturel committed May 6, 2022
1 parent 7976c11 commit 6160b68
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@ function transform(survey) {
}
: {};

let mediaMap = null;

if (survey.media) {
mediaMap = Object.fromEntries(
Object.entries(survey.media).map((entry) =>
entry.map(escapeURLPath)
)
);
}
const mediaMap = Object.fromEntries(
Object.entries(survey.media || {}).map((entry) =>
entry.map(escapeURLPath)
)
);

return _parseXml(survey.xform)
.then((doc) => {
Expand Down Expand Up @@ -501,17 +497,15 @@ function _renderMarkdown(htmlDoc, mediaMap) {
let rendered = markdown.toHtml(original);

if (original !== rendered) {
if (mediaMap != null) {
const fragment = libxmljs.parseHtmlFragment(
`<div class="temporary-root">${rendered}</div>`
);

rendered = _replaceMediaSources(fragment, mediaMap)
.root()
.childNodes()
.map((node) => node.toString(false))
.join('');
}
const fragment = libxmljs.parseHtmlFragment(
`<div class="temporary-root">${rendered}</div>`
);

rendered = _replaceMediaSources(fragment, mediaMap)
.root()
.childNodes()
.map((node) => node.toString(false))
.join('');

key = `$$$${index}`;
replacements[key] = rendered;
Expand Down
19 changes: 19 additions & 0 deletions test/transformer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,25 @@ describe('transformer', () => {
'/hallo%20spaceboy/wishful%20beginnings.xml?p=q&amp;r'
);
});

// Regression in https://github.com/enketo/enketo-transformer/pull/150.
// Before that change, omitting a media mapping would fall back to an empty object
// as a default.
it('returns an empty media map when none was provided at the call site', async () => {
const result = await transformer.transform({ xform });

expect(result.form).to.contain('jr://images/first%20image.jpg');
expect(result.form).to.contain('jr://audio/a%20song.mp3');
expect(result.form).to.contain('jr://video/some%20video.mp4');
expect(result.model).to.contain(
'jr://images/another%20image.png'
);
expect(result.model).to.contain('jr://file/an%20instance.xml');
expect(result.model).to.contain(
'jr://file-csv/a%20spreadsheet.csv'
);
expect(result.form).to.contain('jr://file/a%20link.xml');
});
});
});

Expand Down

0 comments on commit 6160b68

Please sign in to comment.