Skip to content

Commit dc47633

Browse files
authored
Don't show HTML snippets in style, script, or html comment blocks (#12021)
* Don't show HTML snippets in style, script, or html comment blocks * CR feedback - optimize tree walk looking for script, style and html comments
1 parent 1ddd58e commit dc47633

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Completion/Delegation/DelegatedCompletionHelper.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ public static bool ShouldIncludeSnippets(RazorCodeDocument razorCodeDocument, in
230230

231231
if (startOrEndTag is null)
232232
{
233+
if (IsInScriptOrStyleOrHtmlComment(node))
234+
{
235+
// If we're in a style, script, or HTML comment block, we don't want to include HTML snippets.
236+
return false;
237+
}
238+
233239
return token.Kind is not (SyntaxKind.OpenAngle or SyntaxKind.CloseAngle);
234240
}
235241

@@ -241,6 +247,29 @@ public static bool ShouldIncludeSnippets(RazorCodeDocument razorCodeDocument, in
241247
}
242248

243249
return !startOrEndTag.Span.Contains(absoluteIndex);
250+
251+
static bool IsInScriptOrStyleOrHtmlComment(AspNetCore.Razor.Language.Syntax.SyntaxNode? initialNode)
252+
{
253+
for (var node = initialNode; node != null; node = node.Parent)
254+
{
255+
if (node is MarkupElementSyntax elementNode)
256+
{
257+
if (RazorSyntaxFacts.IsStyleBlock(elementNode) || RazorSyntaxFacts.IsScriptBlock(elementNode))
258+
{
259+
return true;
260+
}
261+
262+
// If we're in an element but it's not a script or style block, stop looking
263+
break;
264+
}
265+
else if (node is MarkupCommentBlockSyntax commentNode)
266+
{
267+
return true;
268+
}
269+
}
270+
271+
return false;
272+
}
244273
}
245274

246275
public static object? GetOriginalCompletionItemData(

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/RazorSyntaxFacts.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,19 @@ internal static bool IsInUsingDirective(RazorSyntaxNode node)
207207

208208
return false;
209209
}
210+
211+
internal static bool IsElementWithName(MarkupElementSyntax? element, string name)
212+
{
213+
return string.Equals(element?.StartTag.Name.Content, name, StringComparison.OrdinalIgnoreCase);
214+
}
215+
216+
internal static bool IsStyleBlock(MarkupElementSyntax? node)
217+
{
218+
return IsElementWithName(node, "style");
219+
}
220+
221+
internal static bool IsScriptBlock(MarkupElementSyntax? node)
222+
{
223+
return IsElementWithName(node, "script");
224+
}
210225
}

src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/CompletionIntegrationTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,28 @@ await TestServices.SolutionExplorer.AddFileAsync(
438438
[InlineData("</PageTitle")]
439439
[InlineData("<div")]
440440
[InlineData("</div")]
441+
[InlineData("// script block ")]
442+
[InlineData("/* style block ")]
443+
[InlineData("<!-- comment block ")]
441444
[WorkItem("https://github.com/dotnet/razor/issues/9427")]
442-
public async Task Snippets_DoNotTrigger_InsideTag(string tag)
445+
// Do not trigger snippets in start tags, end tags, script blocks, style blocks, or comments
446+
public async Task Snippets_DoNotTrigger_InDisallowedContext(string tag)
443447
{
444448
await TestServices.SolutionExplorer.AddFileAsync(
445449
RazorProjectConstants.BlazorProjectName,
446450
"Test.razor",
447451
"""
448-
@page "Test"
452+
@page "/Test"
453+
454+
<script>
455+
// script block
456+
</script>
457+
458+
<style>
459+
/* style block */
460+
</style>
461+
462+
<!-- comment block -->
449463
450464
<PageTitle>Test</PageTitle>
451465

0 commit comments

Comments
 (0)