Skip to content

Commit 77e40f3

Browse files
alujsAndrew Lu
and
Andrew Lu
authored
Updated Issue 255, updated tests. (#304)
Co-authored-by: Andrew Lu <[email protected]>
1 parent 821b9ca commit 77e40f3

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

packages/core/src/__tests__/__snapshots__/html.test.ts.snap

+50-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`Html Shadow DOM 1`] = `
44
"<div data-name=\\"div-1\\">
55
<button data-name=\\"button-1\\">Write a review</button>
66
7-
<span data-name=\\"show\\">
7+
<template data-name=\\"show\\">
88
<input placeholder=\\"Email\\" />
99
1010
<input placeholder=\\"Title\\" class=\\"input\\" />
@@ -15,7 +15,7 @@ exports[`Html Shadow DOM 1`] = `
1515
></textarea>
1616
1717
<button class=\\"button\\" data-name=\\"button-2\\">Submit</button>
18-
</span>
18+
</template>
1919
2020
<span data-name=\\"for\\"></span>
2121
<template data-template-for=\\"for\\">
@@ -84,7 +84,29 @@ exports[`Html Shadow DOM 1`] = `
8484
});
8585
8686
document.querySelectorAll(\\"[data-name='show']\\").forEach((el, index) => {
87-
el.style.display = state.showReviewPrompt ? \\"inline\\" : \\"none\\";
87+
if (state.showReviewPrompt) {
88+
const clonedElement = el.content.cloneNode(true);
89+
const endTag = document.createElement(\\"template\\");
90+
endTag.setAttribute(\\"data-show\\", \\"show-end\\");
91+
92+
el.after(endTag);
93+
el.after(clonedElement);
94+
} else {
95+
if (this.querySelector(\\"[data-show='show-end']\\") === null) {
96+
return;
97+
}
98+
99+
let sibling = el.nextSibling;
100+
const toBeRemoved = [];
101+
while (sibling) {
102+
toBeRemoved.push(sibling);
103+
if (sibling?.dataset?.show === \\"show-end\\") {
104+
toBeRemoved.forEach((sib) => sib.remove());
105+
return;
106+
}
107+
sibling = sibling.nextSibling;
108+
}
109+
}
88110
});
89111
90112
document
@@ -188,7 +210,7 @@ exports[`Html Stamped 1`] = `
188210
"<div data-name=\\"div-1\\">
189211
<button data-name=\\"button-1\\">Write a review</button>
190212
191-
<span data-name=\\"show\\">
213+
<template data-name=\\"show\\">
192214
<input placeholder=\\"Email\\" />
193215
194216
<input placeholder=\\"Title\\" class=\\"input\\" />
@@ -199,7 +221,7 @@ exports[`Html Stamped 1`] = `
199221
></textarea>
200222
201223
<button class=\\"button\\" data-name=\\"button-2\\">Submit</button>
202-
</span>
224+
</template>
203225
204226
<span data-name=\\"for\\"></span>
205227
<template data-template-for=\\"for\\">
@@ -278,7 +300,29 @@ exports[`Html Stamped 1`] = `
278300
});
279301
280302
document.querySelectorAll(\\"[data-name='show']\\").forEach((el, index) => {
281-
el.style.display = state.showReviewPrompt ? \\"inline\\" : \\"none\\";
303+
if (state.showReviewPrompt) {
304+
const clonedElement = el.content.cloneNode(true);
305+
const endTag = document.createElement(\\"template\\");
306+
endTag.setAttribute(\\"data-show\\", \\"show-end\\");
307+
308+
el.after(endTag);
309+
el.after(clonedElement);
310+
} else {
311+
if (this.querySelector(\\"[data-show='show-end']\\") === null) {
312+
return;
313+
}
314+
315+
let sibling = el.nextSibling;
316+
const toBeRemoved = [];
317+
while (sibling) {
318+
toBeRemoved.push(sibling);
319+
if (sibling?.dataset?.show === \\"show-end\\") {
320+
toBeRemoved.forEach((sib) => sib.remove());
321+
return;
322+
}
323+
sibling = sibling.nextSibling;
324+
}
325+
}
282326
});
283327
284328
document

packages/core/src/__tests__/__snapshots__/webcomponent.test.ts.snap

+25-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SmileReviews extends HTMLElement {
5757
<div data-name=\\"div-1\\">
5858
<button data-name=\\"button-1\\">Write a review</button>
5959
60-
<span data-name=\\"show\\">
60+
<template data-name=\\"show\\">
6161
<input placeholder=\\"Email\\" />
6262
6363
<input placeholder=\\"Title\\" class=\\"input\\" />
@@ -68,7 +68,7 @@ class SmileReviews extends HTMLElement {
6868
></textarea>
6969
7070
<button class=\\"button\\" data-name=\\"button-2\\">Submit</button>
71-
</span>
71+
</template>
7272
7373
<span data-name=\\"for\\"></span>
7474
<template data-template-for=\\"for\\">
@@ -154,7 +154,29 @@ class SmileReviews extends HTMLElement {
154154
});
155155
156156
this._root.querySelectorAll(\\"[data-name='show']\\").forEach((el, index) => {
157-
el.style.display = this.state.showReviewPrompt ? \\"inline\\" : \\"none\\";
157+
if (this.state.showReviewPrompt) {
158+
const clonedElement = el.content.cloneNode(true);
159+
const endTag = document.createElement(\\"template\\");
160+
endTag.setAttribute(\\"data-show\\", \\"show-end\\");
161+
162+
el.after(endTag);
163+
el.after(clonedElement);
164+
} else {
165+
if (this.querySelector(\\"[data-show='show-end']\\") === null) {
166+
return;
167+
}
168+
169+
let sibling = el.nextSibling;
170+
const toBeRemoved = [];
171+
while (sibling) {
172+
toBeRemoved.push(sibling);
173+
if (sibling?.dataset?.show === \\"show-end\\") {
174+
toBeRemoved.forEach((sib) => sib.remove());
175+
return;
176+
}
177+
sibling = sibling.nextSibling;
178+
}
179+
}
158180
});
159181
160182
this._root

packages/core/src/generators/html.ts

+27-6
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,39 @@ const blockToHtml = (json: MitosisNode, options: InternalToHtmlOptions) => {
237237
addOnChangeJs(
238238
elId,
239239
options,
240-
`el.style.display = ${(json.bindings.when as string).replace(
241-
/;$/,
242-
'',
243-
)} ? 'inline' : 'none'`,
240+
`
241+
if(${(json.bindings.when as string).replace(/;$/, '')}) {
242+
const clonedElement = el.content.cloneNode(true);
243+
const endTag = document.createElement('template');
244+
endTag.setAttribute('data-show', '${elId}-end');
245+
246+
el.after(endTag);
247+
el.after(clonedElement)
248+
} else {
249+
if (this.querySelector("[data-show='${elId}-end']") === null) {
250+
return;
251+
}
252+
253+
let sibling = el.nextSibling;
254+
const toBeRemoved = [];
255+
while (sibling) {
256+
toBeRemoved.push(sibling);
257+
if (sibling?.dataset?.show === '${elId}-end') {
258+
toBeRemoved.forEach(sib => sib.remove());
259+
return;
260+
}
261+
sibling = sibling.nextSibling;
262+
}
263+
}
264+
`,
244265
);
245266

246-
str += `<span data-name="${elId}">`;
267+
str += `<template data-name="${elId}">`;
247268
if (json.children) {
248269
str += json.children.map((item) => blockToHtml(item, options)).join('\n');
249270
}
250271

251-
str += '</span>';
272+
str += '</template>';
252273
} else {
253274
str += `<${json.name} `;
254275

0 commit comments

Comments
 (0)