diff --git a/README.md b/README.md index 371eb9f3..edd22bfe 100644 --- a/README.md +++ b/README.md @@ -468,10 +468,9 @@ public void LegacyStandaloneCaret() ``` #### `BlockScopedInlinePartials` -If `true`, an inline partial (`{{#*inline}}`) declared inside a block (`{{#with}}`, `{{#each}}`, `{{#> layout}}`, a custom block helper) or inside an included partial is visible only in that block and its children, as in Handlebarsjs. After the block it can no longer be called, and an outer partial with the same name is visible again. -By default (`false`) the earlier Handlebars.Net behavior is kept: such partials stay registered for the rest of the enclosing template. - -Before enabling, move any inline partial you call after its block to the top level of the template, or register it with `RegisterTemplate`. Otherwise those calls throw `Referenced partial name '…' could not be resolved`, or produce `MissingPartialTemplateHandler` output if a handler is configured. +By default (`true`) an inline partial (`{{#*inline}}`) declared inside a block (`{{#with}}`, `{{#each}}`, `{{#> layout}}`, a custom block helper) or inside an included partial is visible only in that block and its children, as in Handlebarsjs. After the block it can no longer be called, and an outer partial with the same name is visible again. Calling it after the block throws `Referenced partial name '…' could not be resolved` (or produces `MissingPartialTemplateHandler` output if a handler is configured). +If `false`, restores the earlier Handlebars.Net behavior where such partials stay registered for the rest of the enclosing template, including partials declared inside an included partial (`{{> macros}}{{> button}}`). This was the default up to 2.4.x. +Use `false` only as a temporary escape hatch; the long-term fix is to declare shared inline partials at the top level of the template, or register them with `RegisterTemplate`. ##### Areas - `Compile-time` @@ -479,15 +478,15 @@ Before enabling, move any inline partial you call after its block to the top lev ##### Example ```c# [Fact] -public void BlockScopedInlinePartials() +public void LegacyInlinePartialScoping() { var handlebars = Handlebars.Create(); - handlebars.Configuration.Compatibility.BlockScopedInlinePartials = true; + handlebars.Configuration.Compatibility.BlockScopedInlinePartials = false; var template = handlebars.Compile( "{{#*inline \"p\"}}outer{{/inline}}{{#with .}}{{#*inline \"p\"}}inner{{/inline}}{{> p}}{{/with}} {{> p}}"); - Assert.Equal("inner outer", template(new { })); // "inner inner" with the default behavior + Assert.Equal("inner inner", template(new { })); // "inner outer" with the default behavior } ``` diff --git a/source/Handlebars.Test/HandlebarsSpecCoverageTests.cs b/source/Handlebars.Test/HandlebarsSpecCoverageTests.cs index d8e59c86..1a472846 100644 --- a/source/Handlebars.Test/HandlebarsSpecCoverageTests.cs +++ b/source/Handlebars.Test/HandlebarsSpecCoverageTests.cs @@ -1024,6 +1024,30 @@ public void InvertedSection_InlineCaretSyntaxIsDefaultBehavior() Assert.False(new HandlebarsConfiguration().Compatibility.LegacyStandaloneCaretBehavior); } + // https://github.com/Handlebars-Net/Handlebars.Net/issues/690 + [Theory] + [InlineData("{{#if x}}a{{^}}b{{^}}c{{/if}}")] + [InlineData("{{#unless x}}a{{else}}b{{^}}c{{/unless}}")] + public void InvertedSection_RepeatedInlineCaretErrorNamesLegacyOptOut(string source) + { + var exception = Assert.Throws(() => Handlebars.Create().Compile(source)); + Assert.Contains("cannot follow another '{{else}}'", exception.Message); + Assert.Contains("Configuration.Compatibility.LegacyStandaloneCaretBehavior = true", exception.Message); + + var legacy = Handlebars.Create(); + legacy.Configuration.Compatibility.LegacyStandaloneCaretBehavior = true; + legacy.Compile(source); + } + + [Fact] + public void InvertedSection_RepeatedElseErrorOmitsCaretOptOut() + { + var exception = Assert.Throws( + () => Handlebars.Create().Compile("{{#if x}}a{{^}}b{{else}}c{{/if}}")); + Assert.Contains("cannot follow another '{{else}}'", exception.Message); + Assert.DoesNotContain("LegacyStandaloneCaretBehavior", exception.Message); + } + [Fact] public void InvertedSection_InlineCaretSyntaxIsLiteralInsideRawBlock() { diff --git a/source/Handlebars.Test/InlinePartialTests.cs b/source/Handlebars.Test/InlinePartialTests.cs index 4e28f254..66848c92 100644 --- a/source/Handlebars.Test/InlinePartialTests.cs +++ b/source/Handlebars.Test/InlinePartialTests.cs @@ -486,18 +486,25 @@ public void BlockInlinePartialWithInlinePartials() Assert.Equal("Hello Pete", result4); } - // Compatibility.BlockScopedInlinePartials (opt-in) — https://github.com/Handlebars-Net/Handlebars.Net/issues/673 - private static IHandlebars CreateWithBlockScopedInlinePartials() + // Inline partial scoping (handlebars.js semantics by default; Compatibility.BlockScopedInlinePartials = false opts out) + // https://github.com/Handlebars-Net/Handlebars.Net/issues/673, https://github.com/Handlebars-Net/Handlebars.Net/issues/690 + private static IHandlebars CreateWithLegacyInlinePartialScoping() { var handlebars = Handlebars.Create(); - handlebars.Configuration.Compatibility.BlockScopedInlinePartials = true; + handlebars.Configuration.Compatibility.BlockScopedInlinePartials = false; return handlebars; } [Fact] - public void InlinePartialInBlockStaysRegisteredAfterBlockByDefault() + public void BlockScopedInlinePartialsIsDefaultBehavior() { - var handlebars = Handlebars.Create(); + Assert.True(new HandlebarsConfiguration().Compatibility.BlockScopedInlinePartials); + } + + [Fact] + public void InlinePartialInBlockStaysRegisteredAfterBlockWithLegacyScoping() + { + var handlebars = CreateWithLegacyInlinePartialScoping(); var template = handlebars.Compile( "{{#*inline \"myPartial\"}}outer{{/inline}}" + "{{#with .}}{{#*inline \"myPartial\"}}inner{{/inline}}{{> myPartial}}{{/with}} " + @@ -510,9 +517,9 @@ public void InlinePartialInBlockStaysRegisteredAfterBlockByDefault() } [Fact] - public void InlinePartialsDeclaredInIncludedPartialRemainAvailableByDefault() + public void InlinePartialsDeclaredInIncludedPartialRemainAvailableWithLegacyScoping() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithLegacyInlinePartialScoping(); handlebars.RegisterTemplate("macros", "{{#*inline \"button\"}}{{/inline}}"); var template = handlebars.Compile("{{> macros}}{{> button label=\"OK\"}}"); @@ -520,11 +527,45 @@ public void InlinePartialsDeclaredInIncludedPartialRemainAvailableByDefault() Assert.Equal("", template(new { })); } + [Fact] + public void InlinePartialsDeclaredInIncludedPartialAreScopedToThatPartial() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("macros", "{{#*inline \"button\"}}{{/inline}}"); + + var template = handlebars.Compile("{{> macros}}{{> button label=\"OK\"}}"); + + var exception = Assert.Throws(() => template(new { })); + Assert.Contains("'button' could not be resolved", exception.Message); + } + + [Fact] + public void UnresolvedPartialMessageNamesBlockScopedInlinePartialsOptOut() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{/with}}{{> myPartial}}"); + + var exception = Assert.Throws(() => template(new { })); + Assert.Contains("{{#*inline}} inside a block or another partial", exception.Message); + Assert.Contains("Configuration.Compatibility.BlockScopedInlinePartials = false", exception.Message); + } + + [Fact] + public void UnresolvedPartialMessageOmitsOptOutWithLegacyScoping() + { + var handlebars = CreateWithLegacyInlinePartialScoping(); + var template = handlebars.Compile("{{> missing}}"); + + var exception = Assert.Throws(() => template(new { })); + Assert.Contains("'missing' could not be resolved", exception.Message); + Assert.DoesNotContain("BlockScopedInlinePartials", exception.Message); + } + // handlebars.js spec/partials.js: "should define inline partials for block" [Fact] public void InlinePartialIsScopedToDeclaringBlock() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var inside = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{> myPartial}}{{/with}}"); Assert.Equal("success", inside(new { })); @@ -537,7 +578,7 @@ public void InlinePartialIsScopedToDeclaringBlock() [Fact] public void InlinePartialInBlockOverridesOuterOnlyInsideBlock() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var template = handlebars.Compile( "{{#*inline \"myPartial\"}}outer{{/inline}}" + "{{#with .}}{{#*inline \"myPartial\"}}inner{{/inline}}{{> myPartial}}{{/with}} " + @@ -549,7 +590,7 @@ public void InlinePartialInBlockOverridesOuterOnlyInsideBlock() [Fact] public void InlinePartialInBlockIsVisibleToNestedBlocks() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var template = handlebars.Compile( "{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}" + "{{#with .}}{{#if true}}{{> myPartial}}{{/if}}{{/with}}{{/with}}"); @@ -560,7 +601,7 @@ public void InlinePartialInBlockIsVisibleToNestedBlocks() [Fact] public void InlinePartialInEachIsScopedToLoop() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var template = handlebars.Compile( "{{#*inline \"item\"}}outer{{/inline}}" + "{{#each items}}{{#*inline \"item\"}}[{{.}}]{{/inline}}{{> item}}{{/each}} {{> item}}"); @@ -571,7 +612,7 @@ public void InlinePartialInEachIsScopedToLoop() [Fact] public void LastInlinePartialDefinitionInBlockWins() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var template = handlebars.Compile( "{{#with .}}{{#*inline \"p\"}}first{{/inline}}{{#*inline \"p\"}}second{{/inline}}{{> p}}{{/with}}"); @@ -581,7 +622,7 @@ public void LastInlinePartialDefinitionInBlockWins() [Fact] public void InlinePartialsInBlockWithCustomDecoratorAreScopedToBlock() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); handlebars.RegisterDecorator("noop", (TemplateDelegate function, in DecoratorOptions options, in Context context, in Arguments arguments) => function); @@ -599,7 +640,7 @@ public void InlinePartialsInBlockWithCustomDecoratorAreScopedToBlock() [Fact] public void InlinePartialInPartialBlockIsScopedToPartialBlock() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); handlebars.RegisterTemplate("layout", "<{{> content}}>"); var template = handlebars.Compile( @@ -615,7 +656,7 @@ public void InlinePartialInPartialBlockIsScopedToPartialBlock() [Fact] public void InlinePartialScopeIsRestoredOnRepeatedRenders() { - var handlebars = CreateWithBlockScopedInlinePartials(); + var handlebars = Handlebars.Create(); var template = handlebars.Compile( "{{#*inline \"p\"}}outer{{/inline}}{{#with .}}{{#*inline \"p\"}}inner{{/inline}}{{> p}}{{/with}}{{> p}}"); diff --git a/source/Handlebars/Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs b/source/Handlebars/Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs index cbd87559..316592b8 100644 --- a/source/Handlebars/Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs +++ b/source/Handlebars/Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs @@ -71,9 +71,12 @@ public override void HandleElement(Expression item) { if (_currentCondition == null) { - throw new HandlebarsCompilerException( - $"'{{{{else}}}}' cannot follow another '{{{{else}}}}' in the same '{BlockName}' block", - GetReaderContext(item)); + var message = $"'{{{{else}}}}' cannot follow another '{{{{else}}}}' in the same '{BlockName}' block"; + if (UnwrapStatement(item) is HelperExpression { IsStandaloneCaret: true }) + { + message += $". A bare '{{{{^}}}}' is the inverse separator, equivalent to '{{{{else}}}}'; to restore the previous behavior, where it rendered the current context, set Configuration.Compatibility.{nameof(Compatibility.LegacyStandaloneCaretBehavior)} = true"; + } + throw new HandlebarsCompilerException(message, GetReaderContext(item)); } _conditionalBlock.Add(Expression.IfThen(_currentCondition, SinglifyExpressions(_bodyBuffer))); diff --git a/source/Handlebars/Compiler/Lexer/Converter/HelperArgumentAccumulator.cs b/source/Handlebars/Compiler/Lexer/Converter/HelperArgumentAccumulator.cs index 10bed2bf..604c985e 100644 --- a/source/Handlebars/Compiler/Lexer/Converter/HelperArgumentAccumulator.cs +++ b/source/Handlebars/Compiler/Lexer/Converter/HelperArgumentAccumulator.cs @@ -29,12 +29,12 @@ public override IEnumerable ConvertTokens(IEnumerable sequence) case HelperExpression helper: { var helperArguments = AccumulateArguments(enumerator); - yield return HandlebarsExpression.Helper( + yield return new HelperExpression( helper.HelperName, helper.IsBlock, helperArguments, helper.IsRaw, - helper.Context); + helper.Context) { IsStandaloneCaret = helper.IsStandaloneCaret }; yield return enumerator.Current!; break; } diff --git a/source/Handlebars/Compiler/Lexer/Converter/InverseSeparatorConverter.cs b/source/Handlebars/Compiler/Lexer/Converter/InverseSeparatorConverter.cs index 6cf9f83b..4e0a6e4c 100644 --- a/source/Handlebars/Compiler/Lexer/Converter/InverseSeparatorConverter.cs +++ b/source/Handlebars/Compiler/Lexer/Converter/InverseSeparatorConverter.cs @@ -38,7 +38,7 @@ public override IEnumerable ConvertTokens(IEnumerable sequence) && word.Value == "^" && items[index + 2] is EndExpressionToken) { - yield return HandlebarsExpression.Helper("else", false, false, word.Context); + yield return new HelperExpression("else", false, false, word.Context) { IsStandaloneCaret = true }; index++; } } diff --git a/source/Handlebars/Compiler/Structure/HelperExpression.cs b/source/Handlebars/Compiler/Structure/HelperExpression.cs index be0f4f69..3a32a77a 100644 --- a/source/Handlebars/Compiler/Structure/HelperExpression.cs +++ b/source/Handlebars/Compiler/Structure/HelperExpression.cs @@ -34,6 +34,12 @@ public HelperExpression(string helperName, bool isBlock, bool isRaw = false, IRe public bool IsBlock { get; set; } + /// + /// for an else marker produced from a bare {{^}} by , + /// so errors about it can point at . + /// + public bool IsStandaloneCaret { get; set; } + public IEnumerable Arguments { get; } public IReaderContext? Context { get; } } diff --git a/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs b/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs index 6e20973f..399e9c96 100644 --- a/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs +++ b/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs @@ -107,7 +107,14 @@ private static void InvokePartialWithFallback( if (context.PartialBlockTemplate == null) { if (configuration.MissingPartialTemplateHandler == null) - throw new HandlebarsRuntimeException($"Referenced partial name '{partialName}' could not be resolved. If you registered the partial on the static Handlebars class, make sure to also compile and render using the same static class (or register the partial on the IHandlebars instance you are using to compile)."); + { + var message = $"Referenced partial name '{partialName}' could not be resolved. If you registered the partial on the static Handlebars class, make sure to also compile and render using the same static class (or register the partial on the IHandlebars instance you are using to compile)."; + if (configuration.Compatibility.BlockScopedInlinePartials) + { + message += $" If '{partialName}' is declared with {{{{#*inline}}}} inside a block or another partial, it is only visible within that block; move the declaration to the top level of the template, or set Configuration.Compatibility.{nameof(Compatibility.BlockScopedInlinePartials)} = false to restore the previous behavior."; + } + throw new HandlebarsRuntimeException(message); + } configuration.MissingPartialTemplateHandler.Handle(configuration, partialName, writer); return; diff --git a/source/Handlebars/Configuration/Compatibility.cs b/source/Handlebars/Configuration/Compatibility.cs index 76a6a95d..2659edba 100644 --- a/source/Handlebars/Configuration/Compatibility.cs +++ b/source/Handlebars/Configuration/Compatibility.cs @@ -12,7 +12,7 @@ public class Compatibility /// [Obsolete("@last is supported on Handlebarsjs, so it is always enabled, and the setting should be removed.")] public bool SupportLastInObjectIterations { get; set; } = true; - + /// /// If enables support for Handlebars.Net helper naming rules. /// This enables helper names to be not-valid Handlebars identifiers (e.g. {{ one.two }}) @@ -33,14 +33,16 @@ public class Compatibility public bool LegacyStandaloneCaretBehavior { get; set; } = false; /// - /// If limits an inline partial ({{#*inline}}) declared inside a block to that block, as in Handlebarsjs. - /// The partial is visible in the declaring block and its children only; once the block has rendered it can no longer be - /// called, and an outer partial with the same name becomes visible again: - /// {{#*inline "p"}}outer{{/inline}}{{#with .}}{{#*inline "p"}}inner{{/inline}}{{> p}}{{/with}} {{> p}} renders inner outer. - /// By default () the earlier Handlebars.Net behavior is kept: inline partials declared inside a block - /// (or inside a partial) stay registered on the enclosing scope after it ends, so the template above renders inner inner. - /// Templates that call a block's inline partial after the block will throw "could not be resolved" when this is enabled. + /// If (the default) limits an inline partial ({{#*inline}}) declared inside a block + /// ({{#with}}, {{#each}}, {{#> layout}}, a custom block helper) or inside an included partial + /// to that block and its children, as in Handlebarsjs. Once the block has rendered it can no longer be called, and an + /// outer partial with the same name becomes visible again: + /// {{#*inline "p"}}outer{{/inline}}{{#with .}}{{#*inline "p"}}inner{{/inline}}{{> p}}{{/with}} {{> p}} renders inner outer. + /// Set to to restore the earlier Handlebars.Net behavior: such partials stay registered on the + /// enclosing scope after the block ends, so the template above renders inner inner, and a partial can declare inline + /// partials for its caller to use ({{> macros}}{{> button}}). + /// Setting it to is intended only as a temporary escape hatch for templates that depend on the old behavior. /// - public bool BlockScopedInlinePartials { get; set; } = false; + public bool BlockScopedInlinePartials { get; set; } = true; } -} \ No newline at end of file +}