Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 8c9a5b8

Browse files
committed
Parse the whitespace surrounding equals in attribute correctly
1 parent 39dda01 commit 8c9a5b8

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Microsoft.AspNet.Razor/Parser/HtmlMarkupParser.Block.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ private void BeforeAttribute()
453453
// http://dev.w3.org/html5/spec/tokenization.html#attribute-name-state
454454
// Read the 'name' (i.e. read until the '=' or whitespace/newline)
455455
var name = Enumerable.Empty<HtmlSymbol>();
456+
var whitespaceAfterAttributeName = Enumerable.Empty<HtmlSymbol>();
456457
if (At(HtmlSymbolType.Text))
457458
{
458459
name = ReadWhile(sym =>
@@ -462,6 +463,11 @@ private void BeforeAttribute()
462463
sym.Type != HtmlSymbolType.CloseAngle &&
463464
sym.Type != HtmlSymbolType.OpenAngle &&
464465
(sym.Type != HtmlSymbolType.ForwardSlash || !NextIs(HtmlSymbolType.CloseAngle)));
466+
467+
// capture whitespace after attribute name (if any)
468+
whitespaceAfterAttributeName = ReadWhile(
469+
sym => sym.Type == HtmlSymbolType.WhiteSpace ||
470+
sym.Type == HtmlSymbolType.NewLine);
465471
}
466472
else
467473
{
@@ -484,6 +490,7 @@ private void BeforeAttribute()
484490
{
485491
Accept(whitespace);
486492
Accept(name);
493+
Accept(whitespaceAfterAttributeName);
487494
Output(SpanKind.Markup);
488495
}
489496

@@ -497,11 +504,14 @@ private void BeforeAttribute()
497504
// Start a new markup block for the attribute
498505
using (Context.StartBlock(BlockType.Markup))
499506
{
500-
AttributePrefix(whitespace, name);
507+
AttributePrefix(whitespace, name, whitespaceAfterAttributeName);
501508
}
502509
}
503510

504-
private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<HtmlSymbol> nameSymbols)
511+
private void AttributePrefix(
512+
IEnumerable<HtmlSymbol> whitespace,
513+
IEnumerable<HtmlSymbol> nameSymbols,
514+
IEnumerable<HtmlSymbol> whitespaceAfterAttributeName)
505515
{
506516
// First, determine if this is a 'data-' attribute (since those can't use conditional attributes)
507517
var name = nameSymbols.GetContent(Span.Start);
@@ -510,8 +520,13 @@ private void AttributePrefix(IEnumerable<HtmlSymbol> whitespace, IEnumerable<Htm
510520
// Accept the whitespace and name
511521
Accept(whitespace);
512522
Accept(nameSymbols);
523+
Accept(whitespaceAfterAttributeName);
513524
Assert(HtmlSymbolType.Equals); // We should be at "="
514525
AcceptAndMoveNext();
526+
527+
// Accept the whitespace after Equals
528+
AcceptWhile(sym => sym.Type == HtmlSymbolType.WhiteSpace || sym.Type == HtmlSymbolType.NewLine);
529+
515530
var quote = HtmlSymbolType.Unknown;
516531
if (At(HtmlSymbolType.SingleQuote) || At(HtmlSymbolType.DoubleQuote))
517532
{

test/Microsoft.AspNet.Razor.Test/Parser/Html/HtmlAttributeTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ public void SimpleLiteralAttribute()
2828
Factory.Markup(" />").Accepts(AcceptedCharacters.None))));
2929
}
3030

31+
[Fact]
32+
public void SimpleLiteralAttributeWithWhitespaceSurroundingEquals()
33+
{
34+
ParseBlockTest($"<a href {Environment.NewLine}= {Environment.NewLine}'Foo' />",
35+
new MarkupBlock(
36+
new MarkupTagBlock(
37+
Factory.Markup("<a"),
38+
new MarkupBlock(new AttributeBlockChunkGenerator(name: "href", prefix: new LocationTagged<string>($" href {Environment.NewLine}= {Environment.NewLine}'", 2, 0, 2), suffix: new LocationTagged<string>("'", 18, 2, 4)),
39+
Factory.Markup($" href {Environment.NewLine}= {Environment.NewLine}'").With(SpanChunkGenerator.Null),
40+
Factory.Markup("Foo").With(new LiteralAttributeChunkGenerator(prefix: new LocationTagged<string>(string.Empty, 15, 2, 1), value: new LocationTagged<string>("Foo", 15, 2, 1))),
41+
Factory.Markup("'").With(SpanChunkGenerator.Null)),
42+
Factory.Markup(" />").Accepts(AcceptedCharacters.None))));
43+
}
44+
3145
[Fact]
3246
public void MultiPartLiteralAttribute()
3347
{

0 commit comments

Comments
 (0)