// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using Microsoft.AspNetCore.Razor.Language.Extensions; using Xunit; namespace Microsoft.AspNetCore.Razor.Language.Legacy { public class HtmlToCodeSwitchTest : CsHtmlMarkupParserTestBase { [Fact] public void SwitchesWhenCharacterBeforeSwapIsNonAlphanumeric() { ParseBlockTest("

foo#@i

"); } [Fact] public void SwitchesToCodeWhenSwapCharacterEncounteredMidTag() { ParseBlockTest(""); } [Fact] public void SwitchesToCodeWhenSwapCharacterEncounteredInAttributeValue() { ParseBlockTest(""); } [Fact] public void SwitchesToCodeWhenSwapCharacterEncounteredInTagContent() { ParseBlockTest("@bar@boz"); } [Fact] public void ParsesCodeWithinSingleLineMarkup() { // TODO: Fix at a later date, HTML should be a tag block: https://github.com/aspnet/Razor/issues/101 ParseBlockTest("@:
  • Foo @Bar Baz" + Environment.NewLine + "bork"); } [Fact] public void SupportsCodeWithinComment() { ParseBlockTest(""); } [Fact] public void SupportsCodeWithinSGMLDeclaration() { ParseBlockTest(""); } [Fact] public void SupportsCodeWithinCDataDeclaration() { ParseBlockTest(""); } [Fact] public void SupportsCodeWithinXMLProcessingInstruction() { ParseBlockTest(""); } [Fact] public void ParseBlockDoesNotSwitchToCodeOnEmailAddressInText() { ParseBlockTest("anurse@microsoft.com"); } [Fact] public void DoesNotSwitchToCodeOnEmailAddressInAttribute() { ParseBlockTest("Email me"); } [Fact] public void GivesWhitespacePreceedingAtToCodeIfThereIsNoMarkupOnThatLine() { ParseBlockTest("
      " + Environment.NewLine + " @foreach(var p in Products) {" + Environment.NewLine + "
    • Product: @p.Name
    • " + Environment.NewLine + " }" + Environment.NewLine + "
    "); } [Fact] public void ParseDocumentGivesWhitespacePreceedingAtToCodeIfThereIsNoMarkupOnThatLine() { ParseDocumentTest("
      " + Environment.NewLine + " @foreach(var p in Products) {" + Environment.NewLine + "
    • Product: @p.Name
    • " + Environment.NewLine + " }" + Environment.NewLine + "
    "); } [Fact] public void SectionContextGivesWhitespacePreceedingAtToCodeIfThereIsNoMarkupOnThatLine() { var sectionDescriptor = SectionDirective.Directive; ParseDocumentTest("@section foo {" + Environment.NewLine + "
      " + Environment.NewLine + " @foreach(var p in Products) {" + Environment.NewLine + "
    • Product: @p.Name
    • " + Environment.NewLine + " }" + Environment.NewLine + "
    " + Environment.NewLine + "}", new[] { SectionDirective.Directive, }); } [Fact] public void CSharpCodeParserDoesNotAcceptLeadingOrTrailingWhitespaceInDesignMode() { ParseBlockTest("
      " + Environment.NewLine + " @foreach(var p in Products) {" + Environment.NewLine + "
    • Product: @p.Name
    • " + Environment.NewLine + " }" + Environment.NewLine + "
    ", designTime: true); } // Tests for "@@" escape sequence: [Fact] public void TreatsTwoAtSignsAsEscapeSequence() { ParseBlockTest("@@bar"); } [Fact] public void TreatsPairsOfAtSignsAsEscapeSequence() { ParseBlockTest("@@@@@bar"); } [Fact] public void ParseDocumentTreatsTwoAtSignsAsEscapeSequence() { ParseDocumentTest("@@bar"); } [Fact] public void ParseDocumentTreatsPairsOfAtSignsAsEscapeSequence() { var factory = new SpanFactory(); ParseDocumentTest("@@@@@bar"); } [Fact] public void SectionBodyTreatsTwoAtSignsAsEscapeSequence() { ParseDocumentTest( "@section Foo { @@bar }", new[] { SectionDirective.Directive, }); } [Fact] public void SectionBodyTreatsPairsOfAtSignsAsEscapeSequence() { ParseDocumentTest( "@section Foo { @@@@@bar }", new[] { SectionDirective.Directive, }); } } }