Skip to content

Commit

Permalink
fix(mock-doc): don't force template tags to have a shadowroot (#6078)
Browse files Browse the repository at this point in the history
* fix(mock-doc): don't force template tags to have a shadowroot

* add it if it is actually a shadow root

* better detection of fragment
  • Loading branch information
christian-bromann authored Dec 17, 2024
1 parent 3249003 commit b63039f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/mock-doc/serialize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ function* streamToHtml(
*/
if (
tag === 'template' &&
(!(node as Element).getAttribute || !(node as Element).getAttribute('shadowrootmode'))
(!(node as Element).getAttribute || !(node as Element).getAttribute('shadowrootmode')) &&
/**
* If the node is a shadow root, we want to add the `shadowrootmode` attribute
*/
('host' in node || node.nodeName.toLocaleLowerCase() === shadowRootTag)
) {
const mode = ` shadowrootmode="open"`;
yield mode;
Expand Down
4 changes: 2 additions & 2 deletions src/mock-doc/test/html-parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ describe('parseHtml', () => {
<template>text</template>
`);

expect(doc.head.innerHTML).toBe(`<template shadowrootmode="open">text</template>`);
expect(doc.head.innerHTML).toBe(`<template>text</template>`);

const tmplElm: HTMLTemplateElement = doc.head.firstElementChild as any;

expect(tmplElm.outerHTML).toBe(`<template shadowrootmode="open">text</template>`);
expect(tmplElm.outerHTML).toBe(`<template>text</template>`);
expect(tmplElm.content?.firstChild?.textContent).toBe(`text`);
expect(tmplElm.childNodes).toHaveLength(0);
});
Expand Down
2 changes: 1 addition & 1 deletion src/mock-doc/test/serialize-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('serializeNodeToHtml', () => {
doc.body.innerHTML = input;

const output = serializeNodeToHtml(doc.body);
expect(output).toBe(`<template shadowrootmode="open">text</template>`);
expect(output).toBe(`<template>text</template>`);
});

it('svg', () => {
Expand Down

0 comments on commit b63039f

Please sign in to comment.