Skip to content

Commit 208df84

Browse files
committed
separate comment and doctype for proper parsing
1 parent 278b0c8 commit 208df84

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

packages/render/src/shared/utils/pretty.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ interface HtmlTag {
1717
/**
1818
* Something like the DOCTYPE for the document, or comments.
1919
*/
20-
interface HtmlDeclaration {
21-
type: 'declaration';
20+
interface HtmlDoctype {
21+
type: 'doctype';
22+
content: string;
23+
}
24+
25+
interface HtmlComment {
26+
type: 'comment';
2227
content: string;
2328
}
2429

@@ -27,7 +32,7 @@ interface HtmlText {
2732
content: string;
2833
}
2934

30-
type HtmlNode = HtmlTag | HtmlDeclaration | HtmlText;
35+
type HtmlNode = HtmlTag | HtmlDoctype | HtmlComment | HtmlText;
3136

3237
export const lenientParse = (html: string): HtmlNode[] => {
3338
const result: HtmlNode[] = [];
@@ -59,19 +64,33 @@ export const lenientParse = (html: string): HtmlNode[] => {
5964
index = htmlObjectStart;
6065
}
6166

62-
if (html.startsWith('<!', index)) {
63-
// an HTML declaration, i.e. a comment or a DOCTYPE
64-
const declEnd = html.indexOf('>', index + 2);
67+
if (html.startsWith('<!--', index)) {
68+
const commentEnd = html.indexOf('-->', index + '<!--'.length);
69+
if (commentEnd === -1) {
70+
// Assumes the rest of the document is part of this comment
71+
const content = html.slice(index + '<!--'.length);
72+
addToTree({ type: 'comment', content });
73+
break;
74+
}
75+
76+
const content = html.substring(index + '<!--'.length, commentEnd);
77+
addToTree({ type: 'comment', content });
78+
index = commentEnd + '-->'.length;
79+
continue;
80+
}
81+
82+
if (html.startsWith('<!DOCTYPE', index)) {
83+
const declEnd = html.indexOf('>', index + '<!DOCTYPE'.length);
6584
if (declEnd === -1) {
66-
// Assumes the rest of the document is part of this declaration
67-
const content = html.slice(index);
68-
addToTree({ type: 'declaration', content });
85+
// Assumes the rest of the document is part of this doctype
86+
const content = html.slice(index + '<!DOCTYPE'.length);
87+
addToTree({ type: 'doctype', content });
6988
break;
7089
}
7190

72-
const content = html.substring(index, declEnd + 1);
73-
addToTree({ type: 'declaration', content });
74-
index = declEnd + 1;
91+
const content = html.substring(index + '<!DOCTYPE'.length, declEnd);
92+
addToTree({ type: 'doctype', content });
93+
index = declEnd + '>'.length;
7594
continue;
7695
}
7796

@@ -270,8 +289,10 @@ const prettyNodes = (
270289

271290
formatted += `</${node.name}>${lineBreak}`;
272291
}
273-
} else if (node.type === 'declaration') {
274-
formatted += `${indentation}${node.content}${lineBreak}`;
292+
} else if (node.type === 'comment') {
293+
formatted += `${indentation}<!--${node.content}-->${lineBreak}`;
294+
} else if (node.type === 'doctype') {
295+
formatted += `${indentation}<!DOCTYPE${node.content}>${lineBreak}`;
275296
}
276297
}
277298
return formatted;

0 commit comments

Comments
 (0)