Jeff-Lewis / nhaml

Automatically exported from code.google.com/p/nhaml
0 stars 0 forks source link

Syntax exception when #id declared, and first attribute is "class" #35

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Append to /Tests/Templates/CSharp2/AttributeEval.haml :

- b = "bar"
%p#foo{title="Pip", class=b} Bum
%p#foo{class=b} Bum

2. Append to /Tests/Expected/AttributeEval.xhtml :

<p id="foo" title="Pip" class="bar">Bum</p>
<p id="foo" class="bar">Bum</p>

3. Run test: NHaml.Tests.CSharp2FunctionalTests.AttributeEval

What is the expected output? What do you see instead?

Expect all tests to pass. However, only the first of the two new lines 
pass. The second throws an NHaml.Exceptions.SyntaxException.

What version of the product are you using? On what operating system?

Trunk, revision 82

Please provide any additional information below.

at NHaml.Exceptions.SyntaxException.Throw(InputLine inputLine, String 
errorFormat, Object[] values) in M:\nhaml\src\NHaml\Exceptions
\SyntaxException.cs:line 53
at NHaml.Compilers.CSharp2.CSharp2TemplateCompiler.RenderAttributesCore
(TemplateParser templateParser, String attributes) in M:\nhaml\src\NHaml
\Compilers\CSharp2\CSharp2TemplateCompiler.cs:line 95
at NHaml.Compilers.CSharp2.CSharp2TemplateCompiler.RenderAttributes
(TemplateParser templateParser, String attributes) in M:\nhaml\src\NHaml
\Compilers\CSharp2\CSharp2TemplateCompiler.cs:line 82
at NHaml.Rules.TagMarkupRule.ParseAndRenderAttributes(TemplateParser 
templateParser, Match tagMatch) in M:\nhaml\src\NHaml\Rules
\TagMarkupRule.cs:line 132
at NHaml.Rules.TagMarkupRule.Render(TemplateParser templateParser) in M:
\nhaml\src\NHaml\Rules\TagMarkupRule.cs:line 65
at NHaml.Rules.MarkupRule.Process(TemplateParser templateParser) in M:
\nhaml\src\NHaml\Rules\MarkupRule.cs:line 11
at NHaml.TemplateParser.Parse() in M:\nhaml\src\NHaml
\TemplateParser.cs:line 130
at NHaml.CompiledTemplate.Compile() in M:\nhaml\src\NHaml
\CompiledTemplate.cs:line 65
at NHaml.CompiledTemplate..ctor(TemplateEngine templateEngine, String 
templatePath, String layoutTemplatePath, Type templateBaseType) in M:\nhaml
\src\NHaml\CompiledTemplate.cs:line 33
at NHaml.TemplateEngine.Compile(String templatePath, String 
layoutTemplatePath, Type templateBaseType) in M:\nhaml\src\NHaml
\TemplateEngine.cs:line 276
at NHaml.TemplateEngine.Compile(String templatePath, String 
layoutTemplatePath) in M:\nhaml\src\NHaml\TemplateEngine.cs:line 254
at NHaml.Tests.TestFixtureBase.AssertRender(String templateName, String 
layoutName, String expectedName) in M:\nhaml\src\Tests
\TestFixtureBase.cs:line 67
at NHaml.Tests.TestFixtureBase.AssertRender(String templateName, String 
layoutName) in M:\nhaml\src\Tests\TestFixtureBase.cs:line 48
at NHaml.Tests.FunctionalTestFixture.AttributeEval() in M:\nhaml\src\Tests
\FunctionalTestFixture.cs:line 193

Original issue reported on code.google.com by jsr%mala...@gtempaccount.com on 6 Dec 2008 at 9:57

GoogleCodeExporter commented 9 years ago
This is because class is an Keyword in C#. Use @class instead or use 

%p#foo{class=b} Bum

instead of

%p#foo.b Bum

Original comment by lanwin...@gmail.com on 6 Dec 2008 at 10:54

GoogleCodeExporter commented 9 years ago
I am aware that class is a keyword in C#. However, it seems to work under some 
circumstances. Please review the following examples:

This works:
%p{class=b} Bum

This works:
%p#foo{title="Pip", class=b} Bum

This does not work:
%p#foo{class=b} Bum

Original comment by jsr%mala...@gtempaccount.com on 6 Dec 2008 at 11:30

GoogleCodeExporter commented 9 years ago
As the examples show, the class attribute is only a problem if a declare a 
literal 
id (using the # operator) AND the class attribute is the first attribute in the 
{} 
block. This is very inconsistent and most likely due to a implementation bug.

Original comment by jsr%mala...@gtempaccount.com on 6 Dec 2008 at 11:33

GoogleCodeExporter commented 9 years ago
Ok there is there is also another problem with this #34. There are currently two
strategies to parse the attributes. One for simple attributes which work with 
class
and one for dynamic attributes which uses the c# compiler.

Original comment by lanwin...@gmail.com on 6 Dec 2008 at 3:38

GoogleCodeExporter commented 9 years ago
But still, this works:
%p#foo{title="Pip", class=b} Bum

And this does not work:
%p#foo{class=b} Bum

In both cases the class attribute is assigned to a dynamic value (the variable 
named 
b). It seems that we might be talking at cross-purposes. I don't think that the 
problem I am describing here is related to the one described in #34.

Original comment by jsr%mala...@gtempaccount.com on 6 Dec 2008 at 9:04

GoogleCodeExporter commented 9 years ago
Hello, guys!

I've found how to fix this issue:

CSharp2TemplateCompiler.cs ->

Change

private static readonly Regex _keywordEscaperRegex = new Regex(
      @"((^|\s|,)(class\s*=))|((^|\s|,)(for\s*=))",
      RegexOptions.Compiled | RegexOptions.Singleline);

Change 

public void RenderAttributes(TemplateParser templateParser, string attributes)
{
    attributes = _keywordEscaperRegex.Replace(attributes, "$2$5@$3$6");
    RenderAttributesCore(templateParser, attributes);
}

Hope it helps.

Original comment by meres...@gmail.com on 14 Jan 2009 at 3:22

GoogleCodeExporter commented 9 years ago
fixed in c# (thanks mereskin)

checked in broken tests for boo and iron ruby

Original comment by simon.cropp@gmail.com on 12 Apr 2009 at 10:39