Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix perserveComments on ssr (#4730) #4736

Merged
merged 3 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/compiler/compile/render_ssr/handlers/Comment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import Renderer, { RenderOptions } from '../Renderer';
import Comment from '../../nodes/Comment';

export default function(_node: Comment, _renderer: Renderer, _options: RenderOptions) {
// TODO preserve comments

// if (options.preserveComments) {
// renderer.append(`<!--${node.data}-->`);
// }
export default function(node: Comment, renderer: Renderer, options: RenderOptions) {
if (options.preserveComments) {
renderer.add_string(`<!--${node.data}-->`);
}
}
17 changes: 14 additions & 3 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync';
import * as path from 'path';
import * as fs from 'fs';
import * as colors from 'kleur';
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void };
export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void, htmlEqualWithComments: (actual, expected, message?) => void };

// for coverage purposes, we need to test source files,
// but for sanity purposes, we need to test dist files
Expand Down Expand Up @@ -118,6 +118,9 @@ function cleanChildren(node) {
node.removeChild(child);
child = previous;
}
} else if (child.nodeType === 8) {
// comment
// do nothing
} else {
cleanChildren(child);
}
Expand All @@ -137,11 +140,11 @@ function cleanChildren(node) {
}
}

export function normalizeHtml(window, html) {
export function normalizeHtml(window, html, preserveComments = false) {
try {
const node = window.document.createElement('div');
node.innerHTML = html
.replace(/<!--.*?-->/g, '')
.replace(/(<!--.*?-->)/g, preserveComments ? '$1' : '')
.replace(/>[\s\r\n]+</g, '><')
.trim();
cleanChildren(node);
Expand All @@ -162,6 +165,14 @@ export function setupHtmlEqual() {
message
);
};
// eslint-disable-next-line no-import-assign
assert.htmlEqualWithComments = (actual, expected, message) => {
assert.deepEqual(
normalizeHtml(window, actual, true),
normalizeHtml(window, expected, true),
message
);
};
}

export function loadConfig(file) {
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/ssr-preserve-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { create_ssr_component } from "svelte/internal";

const Component = create_ssr_component(($$result, $$props, $$bindings, slots) => {
return `<div>content</div>

<!-- comment -->
<div>more content</div>`;
});

export default Component;
export default Component;
4 changes: 3 additions & 1 deletion test/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ describe('ssr', () => {
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);

try {
assert.htmlEqual(html, expectedHtml);
(compileOptions.preserveComments
? assert.htmlEqualWithComments
: assert.htmlEqual)(html, expectedHtml);
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(`${dir}/_expected.html`, html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compileOptions: {
preserveComments: true
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>before</p>
<!-- a comment -->
<p>after</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>before</p>
<!-- a comment -->
<p>after</p>