From fe9585ffcec37d4cdd7aad612edda0b477476d13 Mon Sep 17 00:00:00 2001 From: Andrew Seier Date: Thu, 6 Mar 2025 15:54:47 -0800 Subject: [PATCH] Adds arrays to performance test templates. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only demo / test changes here — it’s not too hard to support this since most (all?) templating engines will accept a native _array_ as input for a set of children. --- demo/performance/common.js | 9 ++++++++- demo/performance/react.js | 5 ++++- test/test-parser.js | 4 ++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/demo/performance/common.js b/demo/performance/common.js index 0a454fd..bf39a62 100644 --- a/demo/performance/common.js +++ b/demo/performance/common.js @@ -58,6 +58,7 @@ export default class CommonTest { title: 'test', content1: 'AAA', content2: 'BBB', + items: [{ text: 'one' }, { text: 'two' }, { text: 'three' }], }, { attr: '456', @@ -76,6 +77,7 @@ export default class CommonTest { title: 'test', content1: 'ZZZ', content2: 'BBB', + items: [{ text: 'one' }, { text: 'two' }, { text: 'three' }, { text: 'four' }, { text: 'five' }, { text: 'six' }], }, ]; @@ -235,7 +237,7 @@ export class HtmlLiteralInterface { // We can get around the optimization by using eval though! static getResultEval(html, properties) { // eslint-disable-next-line no-unused-vars - const { attr, one, two, three, four, five, six, seven, eight, nine, ten, id, hidden, title, content1, content2 } = properties; + const { attr, one, two, three, four, five, six, seven, eight, nine, ten, id, hidden, title, content1, content2, items } = properties; // eslint-disable-next-line no-eval return eval(`html\`
@@ -243,6 +245,11 @@ export class HtmlLiteralInterface {
\${content1} -- \${content2}
+
    + \${(items ?? []).map(item => { + return html\`
  • \${item.text}
  • \`; + })} +
diff --git a/demo/performance/react.js b/demo/performance/react.js index 22d5566..77650c9 100644 --- a/demo/performance/react.js +++ b/demo/performance/react.js @@ -11,7 +11,7 @@ class Test extends CommonTest { // TODO: This is sorta cheating since we aren’t asking it to _parse_ anything… static getResult(properties) { - const { attr, one, two, three, four, five, six, seven, eight, nine, ten, id, hidden, title, content1, content2 } = properties; + const { attr, one, two, three, four, five, six, seven, eight, nine, ten, id, hidden, title, content1, content2, items } = properties; return createElement('div', { 'data-id': 'p1', attr }, [ createElement('div', { 'data-id': 'p2', 'data-foo': '', one, two, three, four, five, six, seven, eight, nine, ten }, [ createElement('div', { 'data-id': 'p3', 'data-bar': 'bar' }, [ @@ -20,6 +20,9 @@ class Test extends CommonTest { ' -- ', content2, ]), + createElement('ul', { 'data-id': 'list' }, (items ?? []).map(item => { + return createElement('li', null, [item.text]); + })), ]), createElement('p', null, [ 'Just something a little ', diff --git a/test/test-parser.js b/test/test-parser.js index ab175a2..d7e9cbd 100644 --- a/test/test-parser.js +++ b/test/test-parser.js @@ -1544,12 +1544,16 @@ describe('errors coverage', () => { assertThrows(callback, expectedMessage, { startsWith: true }); }); + ////////////////////////////////////////////////////////////////////////////// + it('throws when cdata exists', () => { const callback = () => htmlol``; const expectedMessage = '[#140]'; assertThrows(callback, expectedMessage, { startsWith: true }); }); + ////////////////////////////////////////////////////////////////////////////// + it('throws when escapes are used', () => { const callback = () => htmlol`\n`; const expectedMessage = '[#150]';