fix problems with html style comments in embedded code (#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 <106700035+UwUeeb@users.noreply.github.com>
Co-authored-by: Martin Aeschlimann <martinae@microsoft.com>
This commit is contained in:
w1redch4d 2022-10-26 14:11:05 +05:30 committed by GitHub
parent a887d72e8d
commit 70328b1b48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View file

@ -168,7 +168,7 @@ function getEmbeddedDocument(document: TextDocument, contents: EmbeddedRegion[],
for (const c of contents) {
if (c.languageId === languageId && (!ignoreAttributeValues || !c.attributeValue)) {
result = substituteWithWhitespace(result, currentPos, c.start, oldContent, lastSuffix, getPrefix(c));
result += oldContent.substring(c.start, c.end);
result += updateContent(c, oldContent.substring(c.start, c.end));
currentPos = c.end;
lastSuffix = getSuffix(c);
}
@ -194,6 +194,12 @@ function getSuffix(c: EmbeddedRegion) {
}
return '';
}
function updateContent(c: EmbeddedRegion, content: string): string {
if (!c.attributeValue && c.languageId === 'javascript') {
return content.replace(`<!--`, `/* `).replace(`-->`, ` */`);
}
return content;
}
function substituteWithWhitespace(result: string, start: number, end: number, oldContent: string, before: string, after: string) {
result += before;

View file

@ -119,7 +119,10 @@ suite('HTML Embedded Support', () => {
test('Script content', function (): any {
assertEmbeddedLanguageContent('<html><script>var i = 0;</script></html>', 'javascript', ' var i = 0; ');
assertEmbeddedLanguageContent('<script type="text/javascript">var i = 0;</script>', 'javascript', ' var i = 0; ');
assertEmbeddedLanguageContent('<script><!--this comment should not give error--></script>', 'javascript', ' /* this comment should not give error */ ');
assertEmbeddedLanguageContent('<script><!--this comment should not give error--> console.log("logging");</script>', 'javascript', ' /* this comment should not give error */ console.log("logging"); ');
assertEmbeddedLanguageContent('<script>var data=100; <!--this comment should not give error--> </script>', 'javascript', ' var data=100; /* this comment should not give error */ ');
assertEmbeddedLanguageContent('<div onKeyUp="foo()" onkeydown="bar()"/>', 'javascript', ' foo(); bar(); ');
assertEmbeddedLanguageContent('<div onKeyUp="return"/>', 'javascript', ' return; ');
assertEmbeddedLanguageContent('<div onKeyUp=return\n/><script>foo();</script>', 'javascript', ' return;\n foo(); ');