diff --git a/scripts/DOM/browser.js b/scripts/DOM/browser.js index 7b3e57a..80bf3d7 100644 --- a/scripts/DOM/browser.js +++ b/scripts/DOM/browser.js @@ -1,5 +1,5 @@ -console.info('DOM implementation in browser') +//console.info('DOM implementation in browser') /** @type { typeof DOMImplementation.prototype.createDocument } */ export function createDocument(...args){ diff --git a/scripts/DOM/node.js b/scripts/DOM/node.js index 5b54354..7ddd2fc 100644 --- a/scripts/DOM/node.js +++ b/scripts/DOM/node.js @@ -1,6 +1,6 @@ import { DOMImplementation } from "@xmldom/xmldom" -console.info('DOM implementation in Node.js based on xmldom') +//console.info('DOM implementation in Node.js based on xmldom') const implementation = new DOMImplementation() diff --git a/scripts/odf/fillOdtTemplate.js b/scripts/odf/fillOdtTemplate.js index 82a9bb1..8ef4e4b 100644 --- a/scripts/odf/fillOdtTemplate.js +++ b/scripts/odf/fillOdtTemplate.js @@ -30,7 +30,7 @@ const ODTMimetype = 'application/vnd.oasis.opendocument.text' * @param {any} context - data / global object * @return {any} */ -function evaludateTemplateExpression(expression, context){ +function evaluateTemplateExpression(expression, context){ const parts = expression.trim().split('.') let value = context; @@ -76,7 +76,7 @@ function findPlacesToFillInString(str) { parts.push(fixedPart) - parts.push(data => evaludateTemplateExpression(expression, data)) + parts.push(data => evaluateTemplateExpression(expression, data)) remaining = newRemaining } @@ -189,12 +189,10 @@ function fillEachBlock(startNode, iterableExpression, itemExpression, endNode, d // Find the iterable in the data // PPP eventually, evaluate the expression as a JS expression - const iterable = evaludateTemplateExpression(iterableExpression, data) - if(!iterable){ - throw new TypeError(`Missing iterable (${iterableExpression})`) - } - if(typeof iterable[Symbol.iterator] !== 'function'){ - throw new TypeError(`'${iterableExpression}' is not iterable`) + let iterable = evaluateTemplateExpression(iterableExpression, data) + if(!iterable || typeof iterable[Symbol.iterator] !== 'function'){ + // when there is no iterable, silently replace with empty array + iterable = [] } // create each loop result diff --git a/tests/fill-odt-template.js b/tests/fill-odt-template.js index bbb66dc..dcec8b4 100644 --- a/tests/fill-odt-template.js +++ b/tests/fill-odt-template.js @@ -71,6 +71,35 @@ Pâtes à lasagne (fraîches !) `) +}); + +test('Filling with {#each} and non-iterable value results in no error and empty result', async t => { + const templatePath = join(import.meta.dirname, './data/enum-courses.odt') + const templateContent = `🧺 La liste de courses incroyable 🧺 + +{#each listeCourses as élément} +{élément} +{/each} +` + + const data = { + listeCourses : undefined + } + + const odtTemplate = await getOdtTemplate(templatePath) + + const templateTextContent = await getOdtTextContent(odtTemplate) + + t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template') + + const odtResult = await fillOdtTemplate(odtTemplate, data) + + const odtResultTextContent = await getOdtTextContent(odtResult) + t.deepEqual(odtResultTextContent, `🧺 La liste de courses incroyable 🧺 + +`) + + });