Fix CSS errors when using HTML escaped quotes

This commit is contained in:
Derek Yang
2024-12-04 23:48:25 -05:00
parent 309454fe80
commit 4685d75964
2 changed files with 18 additions and 0 deletions

View File

@@ -198,6 +198,17 @@ function updateContent(c: EmbeddedRegion, content: string): string {
if (!c.attributeValue && c.languageId === 'javascript') {
return content.replace(`<!--`, `/* `).replace(`-->`, ` */`);
}
if (c.languageId === 'css') {
const quoteEscape = /(&quot;|&#34;)/g;
return content.replace(quoteEscape, (match, _, offset) => {
const spaces = ' '.repeat(match.length - 1);
const afterChar = content[offset + match.length];
if (!afterChar || afterChar.includes(' ')) {
return `${spaces}"`;
}
return `"${spaces}`;
});
}
return content;
}

View File

@@ -128,4 +128,11 @@ suite('HTML Embedded Support', () => {
assertEmbeddedLanguageContent('<div onKeyUp=return\n/><script>foo();</script>', 'javascript', ' return;\n foo(); ');
});
test('Script content - HTML escape characters', function (): any {
assertEmbeddedLanguageContent('<div style="font-family: &quot;Arial&quot;"></div>', 'css', ' __{font-family: " Arial "} ');
assertEmbeddedLanguageContent('<div style="font-family: &#34;Arial&#34;"></div>', 'css', ' __{font-family: " Arial "} ');
assertEmbeddedLanguageContent('<div style="font-family: &quot;Arial&#34;"></div>', 'css', ' __{font-family: " Arial "} ');
assertEmbeddedLanguageContent('<div style="font-family:&quot; Arial &quot; "></div>', 'css', ' __{font-family: " Arial " } ');
});
});