axuno / SmartFormat

A lightweight text templating library written in C# which can be a drop-in replacement for string.Format
Other
1.1k stars 105 forks source link

Different behavior in 2.6.2 vs 2.5.0 for `ErrorAction.MaintainTokens` #148

Closed nikita-starostin closed 3 years ago

nikita-starostin commented 3 years ago

Hi,

in the past I had to parse an html string where CSS-styles had been inlined, e.g.

<style>
    .className {

     }
</style>
<body>
{propertyName}
</body>

In 2.5.0 I have used next snippet to support { for CSS-styles.and tokens at same time:

        private static string PrepareHtml(string template, object values)
        {
            var substitutions = new[] { values };
            var smart = Smart.CreateDefaultSmartFormat();
            smart.Settings.ParseErrorAction = ErrorAction.MaintainTokens;
            smart.Settings.FormatErrorAction = ErrorAction.MaintainTokens;

            return smart.Format(template, substitutions);
        }

The behavior for PrepareHtml is different for 2.6.2 and 2.5.0

// 2.6.2
PrepareHtml("    .p1 { asd }    \r\n<P class=\"p1 ft3\">First Name: <SPAN class=\"ft0\">{info.firstName}</SPAN></P>", new object[] {new { info = new { firstName = "Zz" }}}); 
=>     .p1 { asd }    \r\n<P class=\"p1 ft3\">First Name: <SPAN class=\"ft0\">{info.firstName}</SPAN></P>

// 2.5.0
PrepareHtml("    .p1 { asd }    \r\n<P class=\"p1 ft3\">First Name: <SPAN class=\"ft0\">{info.firstName}</SPAN></P>", new object[] {new { info = new { firstName = "Zz" }}}); 
=>     .p1 { asd }    \r\n<P class=\"p1 ft3\">First Name: <SPAN class=\"ft0\">Zz</SPAN></P>

not sure is it a bug, or not, for desired behavior I have rollbacked to 2.5.0

axunonb commented 3 years ago

Hi Just had a look, but I cannot reproduce your result for v2.5.0:

SmartFormat, Version=2.5.0.0, Culture=neutral, PublicKeyToken=568866805651201f My result:

    .p1 { asd }
<P class="p1 ft3">First Name: <SPAN class="ft0">{info.firstName}</SPAN></P>

SmartFormat, Version=2.6.2.0, Culture=neutral, PublicKeyToken=568866805651201f My result:

    .p1 { asd }
<P class="p1 ft3">First Name: <SPAN class="ft0">{info.firstName}</SPAN></P>

A complete sample, which could be run in a unit test would be helpful.

Parsing HTML works fine, except - like in your case - when it includes CSS styles and JavaScript. Both have curly braces in their syntax, which Smart.Format interprets as { placeholders }.

axunonb commented 3 years ago

Verified as a bug, will be closed by PR #149 - see PR comments for details