From 25ebfc47f2da1662254630371adecf76ad68d1a0 Mon Sep 17 00:00:00 2001 From: Keith Hall Date: Wed, 12 Feb 2025 21:58:39 +0200 Subject: [PATCH 1/2] wip support C# code inside HTML attributes without lookbehinds --- Razor/Razor.sublime-syntax | 11 +- ...or C# Razor HTML attribute).sublime-syntax | 18 +++ Razor/tests/syntax_test_cshtml.cshtml | 139 ++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax diff --git a/Razor/Razor.sublime-syntax b/Razor/Razor.sublime-syntax index e28c255..eb00a86 100644 --- a/Razor/Razor.sublime-syntax +++ b/Razor/Razor.sublime-syntax @@ -274,6 +274,10 @@ contexts: - meta_prepend: true - include: csharp-interpolations + tag-generic-attribute-value-content: + - meta_prepend: true + - include: csharp-interpolations + strings-common-content: - meta_prepend: true - include: csharp-interpolations @@ -303,8 +307,11 @@ contexts: - meta_include_prototype: false - match: '@@' scope: constant.character.escape.cs-razor - - match: (?={{csharp_tag_interpolation_bare}}) - push: csharp-interpolation-body + - match: '\w+@\w+' # prevent email addresses being caught etc + - match: '{{csharp_tag_interpolation_bare}}' + scope: punctuation.section.interpolation.begin.razor + push: Packages/HTML (C#)/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax + #push: csharp-interpolation-body csharp-interpolation-body: # - clear_scopes: 1 diff --git a/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax b/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax new file mode 100644 index 0000000..d37be7e --- /dev/null +++ b/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax @@ -0,0 +1,18 @@ +%YAML 1.2 +--- +scope: source.cs.embedded.html-attribute-string.razor +version: 1 +hidden: true + +extends: Packages/C#/C#.sublime-syntax + +contexts: + main: + - meta_prepend: true + - match: (?=") + pop: 1 + + line_of_code_in: + - meta_prepend: true + - match: (?=") + pop: 1 diff --git a/Razor/tests/syntax_test_cshtml.cshtml b/Razor/tests/syntax_test_cshtml.cshtml index 4bf3877..47654d4 100644 --- a/Razor/tests/syntax_test_cshtml.cshtml +++ b/Razor/tests/syntax_test_cshtml.cshtml @@ -157,6 +157,145 @@ else

@quote

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  • + « + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  • +
  • + "quotes" everywhere + + + + + + + + + + + + + + + + + + + + + + + + + +
  • + @using (Html.BeginForm()) {
    From c3570b1b7a5da18747ba9034705d00a0062a628e Mon Sep 17 00:00:00 2001 From: Michael Lyons Date: Sun, 17 Aug 2025 16:28:58 -0400 Subject: [PATCH 2/2] Pop 3 times to escape HTML attributes in Razor Yes, this is suspicious. --- Razor/Razor.sublime-syntax | 10 +--------- .../C# (for C# Razor HTML attribute).sublime-syntax | 4 ++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/Razor/Razor.sublime-syntax b/Razor/Razor.sublime-syntax index eb00a86..96b625f 100644 --- a/Razor/Razor.sublime-syntax +++ b/Razor/Razor.sublime-syntax @@ -307,17 +307,9 @@ contexts: - meta_include_prototype: false - match: '@@' scope: constant.character.escape.cs-razor - - match: '\w+@\w+' # prevent email addresses being caught etc - - match: '{{csharp_tag_interpolation_bare}}' + - match: '\B{{csharp_tag_interpolation_bare}}' scope: punctuation.section.interpolation.begin.razor push: Packages/HTML (C#)/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax - #push: csharp-interpolation-body - - csharp-interpolation-body: - # - clear_scopes: 1 - - meta_include_prototype: false - - include: csharp-embedded - - include: immediately-pop csharp-embedded: - meta_include_prototype: false diff --git a/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax b/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax index d37be7e..2cea750 100644 --- a/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax +++ b/Razor/embeddings/C# (for C# Razor HTML attribute).sublime-syntax @@ -10,9 +10,9 @@ contexts: main: - meta_prepend: true - match: (?=") - pop: 1 + pop: 3 line_of_code_in: - meta_prepend: true - match: (?=") - pop: 1 + pop: 3