Skip to content

Commit cad3af2

Browse files
committed
Throw a clear error if more than 1 root Element is found
1 parent f445555 commit cad3af2

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/LiveComponent/assets/src/Component/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,14 @@ export default class Component {
359359
modifiedModelValues[modelName] = this.valueStore.get(modelName);
360360
});
361361

362-
const newElement = htmlToElement(html);
362+
let newElement;
363+
try {
364+
newElement = htmlToElement(html);
365+
} catch (error) {
366+
console.error('There was a problem with the component HTML returned:');
367+
368+
throw error;
369+
}
363370
// normalize new element into non-loading state before diff
364371
this.hooks.triggerHook('loading.state:finished', newElement);
365372

src/LiveComponent/assets/src/dom_utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ export function htmlToElement(html: string): HTMLElement {
220220
html = html.trim();
221221
template.innerHTML = html;
222222

223+
if (template.content.childElementCount > 1) {
224+
throw new Error(`Component HTML contains ${template.content.childElementCount} elements, but only 1 root element is allowed.`);
225+
}
226+
223227
const child = template.content.firstElementChild;
224228
if (!child) {
225229
throw new Error('Child not found');

src/LiveComponent/assets/test/controller/render.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,24 @@ describe('LiveController rendering Tests', () => {
365365

366366
await waitFor(() => expect(test.element).toHaveTextContent('123'));
367367
});
368+
369+
it('can understand comment in the response', async () => {
370+
const test = await createTest({ season: 'summer' }, (data: any) => `
371+
<!-- messy comment -->
372+
<div ${initComponent(data)}>
373+
The season is: ${data.season}
374+
</div>
375+
`);
376+
377+
test.expectsAjaxCall('get')
378+
.expectSentData(test.initialData)
379+
.serverWillChangeData((data) => {
380+
data.season = 'autumn';
381+
})
382+
.init();
383+
384+
await test.component.render();
385+
// verify the component *did* render ok
386+
expect(test.element).toHaveTextContent('The season is: autumn');
387+
});
368388
});

0 commit comments

Comments
 (0)