Skip to content

Commit 554d5dd

Browse files
confrariatanhauhau
andauthored
Fix preserveComments on ssr (#4736)
Fixes #4730 Co-authored-by: tanhauhau <[email protected]>
1 parent 40f4615 commit 554d5dd

File tree

7 files changed

+34
-12
lines changed

7 files changed

+34
-12
lines changed
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Renderer, { RenderOptions } from '../Renderer';
22
import Comment from '../../nodes/Comment';
33

4-
export default function(_node: Comment, _renderer: Renderer, _options: RenderOptions) {
5-
// TODO preserve comments
6-
7-
// if (options.preserveComments) {
8-
// renderer.append(`<!--${node.data}-->`);
9-
// }
4+
export default function(node: Comment, renderer: Renderer, options: RenderOptions) {
5+
if (options.preserveComments) {
6+
renderer.add_string(`<!--${node.data}-->`);
7+
}
108
}

test/helpers.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync';
44
import * as path from 'path';
55
import * as fs from 'fs';
66
import * as colors from 'kleur';
7-
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void };
7+
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void, htmlEqualWithComments: (actual, expected, message?) => void };
88

99
// for coverage purposes, we need to test source files,
1010
// but for sanity purposes, we need to test dist files
@@ -118,6 +118,9 @@ function cleanChildren(node) {
118118
node.removeChild(child);
119119
child = previous;
120120
}
121+
} else if (child.nodeType === 8) {
122+
// comment
123+
// do nothing
121124
} else {
122125
cleanChildren(child);
123126
}
@@ -137,11 +140,11 @@ function cleanChildren(node) {
137140
}
138141
}
139142

140-
export function normalizeHtml(window, html) {
143+
export function normalizeHtml(window, html, preserveComments = false) {
141144
try {
142145
const node = window.document.createElement('div');
143146
node.innerHTML = html
144-
.replace(/<!--.*?-->/g, '')
147+
.replace(/(<!--.*?-->)/g, preserveComments ? '$1' : '')
145148
.replace(/>[\s\r\n]+</g, '><')
146149
.trim();
147150
cleanChildren(node);
@@ -162,6 +165,14 @@ export function setupHtmlEqual() {
162165
message
163166
);
164167
};
168+
// eslint-disable-next-line no-import-assign
169+
assert.htmlEqualWithComments = (actual, expected, message) => {
170+
assert.deepEqual(
171+
normalizeHtml(window, actual, true),
172+
normalizeHtml(window, expected, true),
173+
message
174+
);
175+
};
165176
}
166177

167178
export function loadConfig(file) {

test/js/samples/ssr-preserve-comments/expected.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { create_ssr_component } from "svelte/internal";
33

44
const Component = create_ssr_component(($$result, $$props, $$bindings, slots) => {
55
return `<div>content</div>
6-
6+
<!-- comment -->
77
<div>more content</div>`;
88
});
99

10-
export default Component;
10+
export default Component;

test/server-side-rendering/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ describe('ssr', () => {
8181
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);
8282

8383
try {
84-
assert.htmlEqual(html, expectedHtml);
84+
(compileOptions.preserveComments
85+
? assert.htmlEqualWithComments
86+
: assert.htmlEqual)(html, expectedHtml);
8587
} catch (error) {
8688
if (shouldUpdateExpected()) {
8789
fs.writeFileSync(`${dir}/_expected.html`, html);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
compileOptions: {
3+
preserveComments: true
4+
}
5+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>before</p>
2+
<!-- a comment -->
3+
<p>after</p>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>before</p>
2+
<!-- a comment -->
3+
<p>after</p>

0 commit comments

Comments
 (0)