Skip to content

Commit 70328b1

Browse files
w1redch4daeschli
andauthored
fix problems with html style comments in embedded code (microsoft#160981)
* fix problems with html style comments in embedded code * add spacings to the replaced characters add same amount of characters, in the replacements so that all offsets stays the same * revert javascriptMode.ts * add replace characters in embeddedSupport.ts instead * added few more tests with javascript more testcases with js code before and after the comments * polish Co-authored-by: UwUeeb <[email protected]> Co-authored-by: Martin Aeschlimann <[email protected]>
1 parent a887d72 commit 70328b1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

extensions/html-language-features/server/src/modes/embeddedSupport.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function getEmbeddedDocument(document: TextDocument, contents: EmbeddedRegion[],
168168
for (const c of contents) {
169169
if (c.languageId === languageId && (!ignoreAttributeValues || !c.attributeValue)) {
170170
result = substituteWithWhitespace(result, currentPos, c.start, oldContent, lastSuffix, getPrefix(c));
171-
result += oldContent.substring(c.start, c.end);
171+
result += updateContent(c, oldContent.substring(c.start, c.end));
172172
currentPos = c.end;
173173
lastSuffix = getSuffix(c);
174174
}
@@ -194,6 +194,12 @@ function getSuffix(c: EmbeddedRegion) {
194194
}
195195
return '';
196196
}
197+
function updateContent(c: EmbeddedRegion, content: string): string {
198+
if (!c.attributeValue && c.languageId === 'javascript') {
199+
return content.replace(`<!--`, `/* `).replace(`-->`, ` */`);
200+
}
201+
return content;
202+
}
197203

198204
function substituteWithWhitespace(result: string, start: number, end: number, oldContent: string, before: string, after: string) {
199205
result += before;

extensions/html-language-features/server/src/test/embedded.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ suite('HTML Embedded Support', () => {
119119
test('Script content', function (): any {
120120
assertEmbeddedLanguageContent('<html><script>var i = 0;</script></html>', 'javascript', ' var i = 0; ');
121121
assertEmbeddedLanguageContent('<script type="text/javascript">var i = 0;</script>', 'javascript', ' var i = 0; ');
122+
assertEmbeddedLanguageContent('<script><!--this comment should not give error--></script>', 'javascript', ' /* this comment should not give error */ ');
123+
assertEmbeddedLanguageContent('<script><!--this comment should not give error--> console.log("logging");</script>', 'javascript', ' /* this comment should not give error */ console.log("logging"); ');
122124

125+
assertEmbeddedLanguageContent('<script>var data=100; <!--this comment should not give error--> </script>', 'javascript', ' var data=100; /* this comment should not give error */ ');
123126
assertEmbeddedLanguageContent('<div onKeyUp="foo()" onkeydown="bar()"/>', 'javascript', ' foo(); bar(); ');
124127
assertEmbeddedLanguageContent('<div onKeyUp="return"/>', 'javascript', ' return; ');
125128
assertEmbeddedLanguageContent('<div onKeyUp=return\n/><script>foo();</script>', 'javascript', ' return;\n foo(); ');

0 commit comments

Comments
 (0)